2
0

user_mgmt.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_acls"
  73. )
  74. // Pre-Defined User Roles
  75. const (
  76. SuperAdminRole UserRoleID = "super-admin"
  77. AdminRole UserRoleID = "admin"
  78. ServiceUser UserRoleID = "service-user"
  79. PlatformUser UserRoleID = "platform-user"
  80. NetworkAdmin UserRoleID = "network-admin"
  81. NetworkUser UserRoleID = "network-user"
  82. )
  83. func (r UserRoleID) String() string {
  84. return string(r)
  85. }
  86. func (g UserGroupID) String() string {
  87. return string(g)
  88. }
  89. func (n NetworkID) String() string {
  90. return string(n)
  91. }
  92. type RsrcPermissionScope struct {
  93. Create bool `json:"create"`
  94. Read bool `json:"read"`
  95. Update bool `json:"update"`
  96. Delete bool `json:"delete"`
  97. VPNaccess bool `json:"vpn_access"`
  98. SelfOnly bool `json:"self_only"`
  99. }
  100. type UserRolePermissionTemplate struct {
  101. ID UserRoleID `json:"id"`
  102. Name string `json:"name"`
  103. Default bool `json:"default"`
  104. MetaData string `json:"meta_data"`
  105. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  106. FullAccess bool `json:"full_access"`
  107. NetworkID NetworkID `json:"network_id"`
  108. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  109. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  110. }
  111. type CreateGroupReq struct {
  112. Group UserGroup `json:"user_group"`
  113. Members []string `json:"members"`
  114. }
  115. type UserGroup struct {
  116. ID UserGroupID `json:"id"`
  117. Default bool `json:"default"`
  118. Name string `json:"name"`
  119. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  120. MetaData string `json:"meta_data"`
  121. }
  122. // User struct - struct for Users
  123. type User struct {
  124. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  125. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  126. Password string `json:"password" bson:"password" validate:"required,min=5"`
  127. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  128. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  129. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  130. AuthType AuthType `json:"auth_type"`
  131. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  132. PlatformRoleID UserRoleID `json:"platform_role_id"`
  133. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  134. LastLoginTime time.Time `json:"last_login_time"`
  135. }
  136. type ReturnUserWithRolesAndGroups struct {
  137. ReturnUser
  138. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  139. }
  140. // ReturnUser - return user struct
  141. type ReturnUser struct {
  142. UserName string `json:"username"`
  143. IsAdmin bool `json:"isadmin"`
  144. IsSuperAdmin bool `json:"issuperadmin"`
  145. AuthType AuthType `json:"auth_type"`
  146. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  147. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  148. PlatformRoleID UserRoleID `json:"platform_role_id"`
  149. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  150. LastLoginTime time.Time `json:"last_login_time"`
  151. }
  152. // UserAuthParams - user auth params struct
  153. type UserAuthParams struct {
  154. UserName string `json:"username"`
  155. Password string `json:"password"`
  156. }
  157. // UserClaims - user claims struct
  158. type UserClaims struct {
  159. Role UserRoleID
  160. UserName string
  161. RacAutoDisable bool
  162. jwt.RegisteredClaims
  163. }
  164. type InviteUsersReq struct {
  165. UserEmails []string `json:"user_emails"`
  166. PlatformRoleID string `json:"platform_role_id"`
  167. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  168. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  169. }
  170. // UserInvite - model for user invite
  171. type UserInvite struct {
  172. Email string `json:"email"`
  173. PlatformRoleID string `json:"platform_role_id"`
  174. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  175. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  176. InviteCode string `json:"invite_code"`
  177. InviteURL string `json:"invite_url"`
  178. }