user_mgmt.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. DnsRsrc RsrcType = "dns"
  56. FailOverRsrc RsrcType = "fail_over"
  57. MetricRsrc RsrcType = "metrics"
  58. )
  59. const (
  60. AllHostRsrcID RsrcID = "all_host"
  61. AllRelayRsrcID RsrcID = "all_relay"
  62. AllRemoteAccessGwRsrcID RsrcID = "all_remote_access_gw"
  63. AllExtClientsRsrcID RsrcID = "all_extclients"
  64. AllInetGwRsrcID RsrcID = "all_inet_gw"
  65. AllEgressGwRsrcID RsrcID = "all_egress"
  66. AllNetworkRsrcID RsrcID = "all_network"
  67. AllEnrollmentKeysRsrcID RsrcID = "all_enrollment_key"
  68. AllUserRsrcID RsrcID = "all_user"
  69. AllDnsRsrcID RsrcID = "all_dns"
  70. AllFailOverRsrcID RsrcID = "all_fail_over"
  71. AllAclsRsrcID RsrcID = "all_acls"
  72. )
  73. // Pre-Defined User Roles
  74. const (
  75. SuperAdminRole UserRoleID = "super-admin"
  76. AdminRole UserRoleID = "admin"
  77. ServiceUser UserRoleID = "service-user"
  78. PlatformUser UserRoleID = "platform-user"
  79. NetworkAdmin UserRoleID = "network-admin"
  80. NetworkUser UserRoleID = "network-user"
  81. )
  82. func (r UserRoleID) String() string {
  83. return string(r)
  84. }
  85. func (g UserGroupID) String() string {
  86. return string(g)
  87. }
  88. func (n NetworkID) String() string {
  89. return string(n)
  90. }
  91. type RsrcPermissionScope struct {
  92. Create bool `json:"create"`
  93. Read bool `json:"read"`
  94. Update bool `json:"update"`
  95. Delete bool `json:"delete"`
  96. VPNaccess bool `json:"vpn_access"`
  97. SelfOnly bool `json:"self_only"`
  98. }
  99. type UserRolePermissionTemplate struct {
  100. ID UserRoleID `json:"id"`
  101. UiName string `json:"ui_name"`
  102. Default bool `json:"default"`
  103. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  104. FullAccess bool `json:"full_access"`
  105. NetworkID NetworkID `json:"network_id"`
  106. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  107. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  108. }
  109. type CreateGroupReq struct {
  110. Group UserGroup `json:"user_group"`
  111. Members []string `json:"members"`
  112. }
  113. type UserGroup struct {
  114. ID UserGroupID `json:"id"`
  115. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  116. MetaData string `json:"meta_data"`
  117. }
  118. // User struct - struct for Users
  119. type User struct {
  120. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  121. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  122. Password string `json:"password" bson:"password" validate:"required,min=5"`
  123. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  124. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  125. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  126. AuthType AuthType `json:"auth_type"`
  127. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  128. PlatformRoleID UserRoleID `json:"platform_role_id"`
  129. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  130. LastLoginTime time.Time `json:"last_login_time"`
  131. }
  132. type ReturnUserWithRolesAndGroups struct {
  133. ReturnUser
  134. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  135. }
  136. // ReturnUser - return user struct
  137. type ReturnUser struct {
  138. UserName string `json:"username"`
  139. IsAdmin bool `json:"isadmin"`
  140. IsSuperAdmin bool `json:"issuperadmin"`
  141. AuthType AuthType `json:"auth_type"`
  142. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  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. // UserAuthParams - user auth params struct
  149. type UserAuthParams struct {
  150. UserName string `json:"username"`
  151. Password string `json:"password"`
  152. }
  153. // UserClaims - user claims struct
  154. type UserClaims struct {
  155. Role UserRoleID
  156. UserName string
  157. jwt.RegisteredClaims
  158. }
  159. type InviteUsersReq struct {
  160. UserEmails []string `json:"user_emails"`
  161. PlatformRoleID string `json:"platform_role_id"`
  162. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  163. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  164. }
  165. // UserInvite - model for user invite
  166. type UserInvite struct {
  167. Email string `json:"email"`
  168. PlatformRoleID string `json:"platform_role_id"`
  169. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  170. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  171. InviteCode string `json:"invite_code"`
  172. InviteURL string `json:"invite_url"`
  173. }