user_mgmt.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 UserRoleID string
  11. type UserGroupID string
  12. type AuthType string
  13. type TokenType string
  14. var (
  15. BasicAuth AuthType = "basic_auth"
  16. OAuth AuthType = "oauth"
  17. )
  18. func (r RsrcType) String() string {
  19. return string(r)
  20. }
  21. func (rid RsrcID) String() string {
  22. return string(rid)
  23. }
  24. func GetRAGRoleName(netID, hostName string) string {
  25. return fmt.Sprintf("netID-%s-rag-%s", netID, hostName)
  26. }
  27. func GetRAGRoleID(netID, hostID string) UserRoleID {
  28. return UserRoleID(fmt.Sprintf("netID-%s-rag-%s", netID, hostID))
  29. }
  30. func (t TokenType) String() string {
  31. return string(t)
  32. }
  33. var (
  34. UserIDTokenType TokenType = "user_id_token"
  35. AccessTokenType TokenType = "access_token"
  36. )
  37. var RsrcTypeMap = map[RsrcType]struct{}{
  38. HostRsrc: {},
  39. RelayRsrc: {},
  40. RemoteAccessGwRsrc: {},
  41. ExtClientsRsrc: {},
  42. InetGwRsrc: {},
  43. EgressGwRsrc: {},
  44. NetworkRsrc: {},
  45. EnrollmentKeysRsrc: {},
  46. UserRsrc: {},
  47. AclRsrc: {},
  48. DnsRsrc: {},
  49. FailOverRsrc: {},
  50. }
  51. const AllNetworks NetworkID = "all_networks"
  52. const (
  53. HostRsrc RsrcType = "hosts"
  54. RelayRsrc RsrcType = "relays"
  55. RemoteAccessGwRsrc RsrcType = "remote_access_gw"
  56. ExtClientsRsrc RsrcType = "extclients"
  57. InetGwRsrc RsrcType = "inet_gw"
  58. EgressGwRsrc RsrcType = "egress"
  59. NetworkRsrc RsrcType = "networks"
  60. EnrollmentKeysRsrc RsrcType = "enrollment_key"
  61. UserRsrc RsrcType = "users"
  62. AclRsrc RsrcType = "acl"
  63. TagRsrc RsrcType = "tag"
  64. DnsRsrc RsrcType = "dns"
  65. FailOverRsrc RsrcType = "fail_over"
  66. MetricRsrc RsrcType = "metrics"
  67. )
  68. const (
  69. AllHostRsrcID RsrcID = "all_host"
  70. AllRelayRsrcID RsrcID = "all_relay"
  71. AllRemoteAccessGwRsrcID RsrcID = "all_remote_access_gw"
  72. AllExtClientsRsrcID RsrcID = "all_extclients"
  73. AllInetGwRsrcID RsrcID = "all_inet_gw"
  74. AllEgressGwRsrcID RsrcID = "all_egress"
  75. AllNetworkRsrcID RsrcID = "all_network"
  76. AllEnrollmentKeysRsrcID RsrcID = "all_enrollment_key"
  77. AllUserRsrcID RsrcID = "all_user"
  78. AllDnsRsrcID RsrcID = "all_dns"
  79. AllFailOverRsrcID RsrcID = "all_fail_over"
  80. AllAclsRsrcID RsrcID = "all_acl"
  81. AllTagsRsrcID RsrcID = "all_tag"
  82. )
  83. // Pre-Defined User Roles
  84. const (
  85. SuperAdminRole UserRoleID = "super-admin"
  86. AdminRole UserRoleID = "admin"
  87. ServiceUser UserRoleID = "service-user"
  88. PlatformUser UserRoleID = "platform-user"
  89. NetworkAdmin UserRoleID = "network-admin"
  90. NetworkUser UserRoleID = "network-user"
  91. )
  92. func (r UserRoleID) String() string {
  93. return string(r)
  94. }
  95. func (g UserGroupID) String() string {
  96. return string(g)
  97. }
  98. func (n NetworkID) String() string {
  99. return string(n)
  100. }
  101. type RsrcPermissionScope struct {
  102. Create bool `json:"create"`
  103. Read bool `json:"read"`
  104. Update bool `json:"update"`
  105. Delete bool `json:"delete"`
  106. VPNaccess bool `json:"vpn_access"`
  107. SelfOnly bool `json:"self_only"`
  108. }
  109. type UserRolePermissionTemplate struct {
  110. ID UserRoleID `json:"id"`
  111. Name string `json:"name"`
  112. Default bool `json:"default"`
  113. MetaData string `json:"meta_data"`
  114. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  115. FullAccess bool `json:"full_access"`
  116. NetworkID NetworkID `json:"network_id"`
  117. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  118. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  119. }
  120. type CreateGroupReq struct {
  121. Group UserGroup `json:"user_group"`
  122. Members []string `json:"members"`
  123. }
  124. type UserGroup struct {
  125. ID UserGroupID `json:"id"`
  126. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  127. Default bool `json:"default"`
  128. Name string `json:"name"`
  129. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  130. MetaData string `json:"meta_data"`
  131. }
  132. // User struct - struct for Users
  133. type User struct {
  134. UserName string `json:"username" bson:"username" validate:"min=3,in_charset|email"`
  135. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  136. DisplayName string `json:"display_name"`
  137. AccountDisabled bool `json:"account_disabled"`
  138. Password string `json:"password" bson:"password" validate:"required,min=5"`
  139. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  140. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  141. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  142. AuthType AuthType `json:"auth_type"`
  143. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  144. PlatformRoleID UserRoleID `json:"platform_role_id"`
  145. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  146. LastLoginTime time.Time `json:"last_login_time"`
  147. }
  148. type ReturnUserWithRolesAndGroups struct {
  149. ReturnUser
  150. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  151. UserGroups map[UserGroupID]UserGroup `json:"user_group_ids"`
  152. }
  153. // ReturnUser - return user struct
  154. type ReturnUser struct {
  155. UserName string `json:"username"`
  156. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  157. DisplayName string `json:"display_name"`
  158. AccountDisabled bool `json:"account_disabled"`
  159. IsAdmin bool `json:"isadmin"`
  160. IsSuperAdmin bool `json:"issuperadmin"`
  161. AuthType AuthType `json:"auth_type"`
  162. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  163. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  164. PlatformRoleID UserRoleID `json:"platform_role_id"`
  165. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  166. LastLoginTime time.Time `json:"last_login_time"`
  167. }
  168. // UserAuthParams - user auth params struct
  169. type UserAuthParams struct {
  170. UserName string `json:"username"`
  171. Password string `json:"password"`
  172. }
  173. // UserClaims - user claims struct
  174. type UserClaims struct {
  175. Role UserRoleID
  176. UserName string
  177. Api string
  178. TokenType TokenType
  179. RacAutoDisable bool
  180. jwt.RegisteredClaims
  181. }
  182. type InviteUsersReq struct {
  183. UserEmails []string `json:"user_emails"`
  184. PlatformRoleID string `json:"platform_role_id"`
  185. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  186. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  187. }
  188. // UserInvite - model for user invite
  189. type UserInvite struct {
  190. Email string `json:"email"`
  191. PlatformRoleID string `json:"platform_role_id"`
  192. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  193. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  194. InviteCode string `json:"invite_code"`
  195. InviteURL string `json:"invite_url"`
  196. }