user_mgmt.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. jwt "github.com/golang-jwt/jwt/v4"
  6. )
  7. type NetworkID string
  8. type RsrcType string
  9. type RsrcID string
  10. type UserRole string
  11. type UserGroupID string
  12. type AuthType string
  13. var (
  14. BasicAuth AuthType = "basic_auth"
  15. OAuth AuthType = "oauth"
  16. )
  17. func (r RsrcType) String() string {
  18. return string(r)
  19. }
  20. func (rid RsrcID) String() string {
  21. return string(rid)
  22. }
  23. func GetRAGRoleName(netID, hostName string) UserRole {
  24. return UserRole(fmt.Sprintf("netID-%s-rag-%s", netID, hostName))
  25. }
  26. var RsrcTypeMap = map[RsrcType]struct{}{
  27. HostRsrc: {},
  28. RelayRsrc: {},
  29. RemoteAccessGwRsrc: {},
  30. ExtClientsRsrc: {},
  31. InetGwRsrc: {},
  32. EgressGwRsrc: {},
  33. NetworkRsrc: {},
  34. EnrollmentKeysRsrc: {},
  35. UserRsrc: {},
  36. AclRsrc: {},
  37. DnsRsrc: {},
  38. FailOverRsrc: {},
  39. }
  40. const (
  41. HostRsrc RsrcType = "hosts"
  42. RelayRsrc RsrcType = "relays"
  43. RemoteAccessGwRsrc RsrcType = "remote_access_gw"
  44. ExtClientsRsrc RsrcType = "extclients"
  45. InetGwRsrc RsrcType = "inet_gw"
  46. EgressGwRsrc RsrcType = "egress"
  47. NetworkRsrc RsrcType = "networks"
  48. EnrollmentKeysRsrc RsrcType = "enrollment_key"
  49. UserRsrc RsrcType = "users"
  50. AclRsrc RsrcType = "acl"
  51. DnsRsrc RsrcType = "dns"
  52. FailOverRsrc RsrcType = "fail_over"
  53. MetricRsrc RsrcType = "metrics"
  54. )
  55. const (
  56. AllHostRsrcID RsrcID = "all_host"
  57. AllRelayRsrcID RsrcID = "all_relay"
  58. AllRemoteAccessGwRsrcID RsrcID = "all_remote_access_gw"
  59. AllExtClientsRsrcID RsrcID = "all_extclients"
  60. AllInetGwRsrcID RsrcID = "all_inet_gw"
  61. AllEgressGwRsrcID RsrcID = "all_egress"
  62. AllNetworkRsrcID RsrcID = "all_network"
  63. AllEnrollmentKeysRsrcID RsrcID = "all_enrollment_key"
  64. AllUserRsrcID RsrcID = "all_user"
  65. AllDnsRsrcID RsrcID = "all_dns"
  66. AllFailOverRsrcID RsrcID = "all_fail_over"
  67. AllAclsRsrcID RsrcID = "all_acls"
  68. )
  69. // Pre-Defined User Roles
  70. const (
  71. SuperAdminRole UserRole = "super_admin"
  72. AdminRole UserRole = "admin"
  73. ServiceUser UserRole = "service_user"
  74. PlatformUser UserRole = "platform_user"
  75. NetworkAdmin UserRole = "network_admin"
  76. NetworkUser UserRole = "network_user"
  77. )
  78. func (r UserRole) String() string {
  79. return string(r)
  80. }
  81. func (g UserGroupID) String() string {
  82. return string(g)
  83. }
  84. type RsrcPermissionScope struct {
  85. Create bool `json:"create"`
  86. Read bool `json:"read"`
  87. Update bool `json:"update"`
  88. Delete bool `json:"delete"`
  89. VPNaccess bool `json:"vpn_access"`
  90. SelfOnly bool `json:"self_only"`
  91. }
  92. type UserRolePermissionTemplate struct {
  93. ID UserRole `json:"id"`
  94. Default bool `json:"default"`
  95. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  96. FullAccess bool `json:"full_access"`
  97. NetworkID string `json:"network_id"`
  98. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  99. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  100. }
  101. type CreateGroupReq struct {
  102. Group UserGroup `json:"user_group"`
  103. Members []string `json:"members"`
  104. }
  105. type UserGroup struct {
  106. ID UserGroupID `json:"id"`
  107. PlatformRole UserRole `json:"platform_role"`
  108. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  109. MetaData string `json:"meta_data"`
  110. }
  111. // User struct - struct for Users
  112. type User struct {
  113. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  114. Password string `json:"password" bson:"password" validate:"required,min=5"`
  115. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  116. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  117. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  118. AuthType AuthType `json:"auth_type"`
  119. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  120. PlatformRoleID UserRole `json:"platform_role_id"`
  121. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  122. LastLoginTime time.Time `json:"last_login_time"`
  123. }
  124. type ReturnUserWithRolesAndGroups struct {
  125. ReturnUser
  126. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  127. }
  128. // ReturnUser - return user struct
  129. type ReturnUser struct {
  130. UserName string `json:"username"`
  131. IsAdmin bool `json:"isadmin"`
  132. IsSuperAdmin bool `json:"issuperadmin"`
  133. AuthType AuthType `json:"auth_type"`
  134. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  135. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  136. PlatformRoleID UserRole `json:"platform_role_id"`
  137. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  138. LastLoginTime time.Time `json:"last_login_time"`
  139. }
  140. // UserAuthParams - user auth params struct
  141. type UserAuthParams struct {
  142. UserName string `json:"username"`
  143. Password string `json:"password"`
  144. }
  145. // UserClaims - user claims struct
  146. type UserClaims struct {
  147. Role UserRole
  148. UserName string
  149. jwt.RegisteredClaims
  150. }
  151. type InviteUsersReq struct {
  152. UserEmails []string `json:"user_emails"`
  153. Groups []UserGroupID
  154. }
  155. // UserInvite - model for user invite
  156. type UserInvite struct {
  157. Email string `json:"email"`
  158. Groups []UserGroupID `json:"groups"`
  159. InviteCode string `json:"invite_code"`
  160. InviteURL string `json:"invite_url"`
  161. }