user_mgmt.go 7.3 KB

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