user_mgmt.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  106. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  107. }
  108. type CreateGroupReq struct {
  109. Group UserGroup `json:"user_group"`
  110. Members []string `json:"members"`
  111. }
  112. type UserGroup struct {
  113. ID UserGroupID `json:"id"`
  114. PlatformRoleID UserRoleID `json:"platform_role_id"`
  115. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  116. RoleOnNetworks map[UserRoleID]map[NetworkID]struct{} `json:"role_on_networks"`
  117. MetaData string `json:"meta_data"`
  118. }
  119. // User struct - struct for Users
  120. type User struct {
  121. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  122. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  123. Password string `json:"password" bson:"password" validate:"required,min=5"`
  124. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  125. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  126. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  127. AuthType AuthType `json:"auth_type"`
  128. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  129. PlatformRoleID UserRoleID `json:"platform_role_id"`
  130. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  131. LastLoginTime time.Time `json:"last_login_time"`
  132. }
  133. type ReturnUserWithRolesAndGroups struct {
  134. ReturnUser
  135. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  136. }
  137. // ReturnUser - return user struct
  138. type ReturnUser struct {
  139. UserName string `json:"username"`
  140. IsAdmin bool `json:"isadmin"`
  141. IsSuperAdmin bool `json:"issuperadmin"`
  142. AuthType AuthType `json:"auth_type"`
  143. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  144. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  145. PlatformRoleID UserRoleID `json:"platform_role_id"`
  146. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  147. LastLoginTime time.Time `json:"last_login_time"`
  148. }
  149. // UserAuthParams - user auth params struct
  150. type UserAuthParams struct {
  151. UserName string `json:"username"`
  152. Password string `json:"password"`
  153. }
  154. // UserClaims - user claims struct
  155. type UserClaims struct {
  156. Role UserRoleID
  157. UserName string
  158. jwt.RegisteredClaims
  159. }
  160. type InviteUsersReq struct {
  161. UserEmails []string `json:"user_emails"`
  162. PlatformRoleID string `json:"platform_role_id"`
  163. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  164. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  165. }
  166. // UserInvite - model for user invite
  167. type UserInvite struct {
  168. Email string `json:"email"`
  169. PlatformRoleID string `json:"platform_role_id"`
  170. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  171. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  172. InviteCode string `json:"invite_code"`
  173. InviteURL string `json:"invite_url"`
  174. }