user_mgmt.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package models
  2. import (
  3. "time"
  4. jwt "github.com/golang-jwt/jwt/v4"
  5. )
  6. type NetworkID string
  7. type RsrcType string
  8. type RsrcID string
  9. type UserRole string
  10. type UserGroupID string
  11. func (r RsrcType) String() string {
  12. return string(r)
  13. }
  14. func (rid RsrcID) String() string {
  15. return string(rid)
  16. }
  17. var RsrcTypeMap = map[RsrcType]struct{}{
  18. HostRsrc: {},
  19. RelayRsrc: {},
  20. RemoteAccessGwRsrc: {},
  21. ExtClientsRsrc: {},
  22. InetGwRsrc: {},
  23. EgressGwRsrc: {},
  24. NetworkRsrc: {},
  25. EnrollmentKeysRsrc: {},
  26. UserRsrc: {},
  27. AclRsrc: {},
  28. DnsRsrc: {},
  29. FailOverRsrc: {},
  30. }
  31. const (
  32. HostRsrc RsrcType = "hosts"
  33. RelayRsrc RsrcType = "relays"
  34. RemoteAccessGwRsrc RsrcType = "remote_access_gw"
  35. ExtClientsRsrc RsrcType = "extclients"
  36. InetGwRsrc RsrcType = "inet_gw"
  37. EgressGwRsrc RsrcType = "egress"
  38. NetworkRsrc RsrcType = "networks"
  39. EnrollmentKeysRsrc RsrcType = "enrollment_key"
  40. UserRsrc RsrcType = "users"
  41. AclRsrc RsrcType = "acl"
  42. DnsRsrc RsrcType = "dns"
  43. FailOverRsrc RsrcType = "fail_over"
  44. )
  45. const (
  46. AllHostRsrcID RsrcID = "all_host"
  47. AllRelayRsrcID RsrcID = "all_relay"
  48. AllRemoteAccessGwRsrcID RsrcID = "all_remote_access_gw"
  49. AllExtClientsRsrcID RsrcID = "all_extclients"
  50. AllInetGwRsrcID RsrcID = "all_inet_gw"
  51. AllEgressGwRsrcID RsrcID = "all_egress"
  52. AllNetworkRsrcID RsrcID = "all_network"
  53. AllEnrollmentKeysRsrcID RsrcID = "all_enrollment_key"
  54. AllUserRsrcID RsrcID = "all_user"
  55. AllDnsRsrcID RsrcID = "all_dns"
  56. AllFailOverRsrcID RsrcID = "all_fail_over"
  57. AllAclsRsrcID RsrcID = "all_acls"
  58. )
  59. // Pre-Defined User Roles
  60. const (
  61. SuperAdminRole UserRole = "super_admin"
  62. AdminRole UserRole = "admin"
  63. ServiceUser UserRole = "service_user"
  64. PlatformUser UserRole = "platform_user"
  65. NetworkAdmin UserRole = "network_admin"
  66. NetworkUser UserRole = "network_user"
  67. )
  68. func (r UserRole) String() string {
  69. return string(r)
  70. }
  71. func (g UserGroupID) String() string {
  72. return string(g)
  73. }
  74. type RsrcPermissionScope struct {
  75. Create bool `json:"create"`
  76. Read bool `json:"read"`
  77. Update bool `json:"update"`
  78. Delete bool `json:"delete"`
  79. VPNaccess bool `json:"vpn_access"`
  80. }
  81. type UserRolePermissionTemplate struct {
  82. ID UserRole `json:"id"`
  83. Default bool `json:"default"`
  84. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  85. FullAccess bool `json:"full_access"`
  86. NetworkID string `json:"network_id"`
  87. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  88. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  89. }
  90. type UserGroup struct {
  91. ID UserGroupID `json:"id"`
  92. PlatformRole UserRole `json:"platform_role"`
  93. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  94. MetaData string `json:"meta_data"`
  95. }
  96. // User struct - struct for Users
  97. type User struct {
  98. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  99. Password string `json:"password" bson:"password" validate:"required,min=5"`
  100. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  101. IsSuperAdmin bool `json:"issuperadmin"`
  102. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  103. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  104. PlatformRoleID UserRole `json:"platform_role_id"`
  105. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  106. LastLoginTime time.Time `json:"last_login_time"`
  107. }
  108. // ReturnUser - return user struct
  109. type ReturnUser struct {
  110. UserName string `json:"username"`
  111. IsAdmin bool `json:"isadmin"`
  112. IsSuperAdmin bool `json:"issuperadmin"`
  113. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  114. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  115. PlatformRoleID UserRole `json:"platform_role_id"`
  116. NetworkRoles map[NetworkID]map[UserRole]struct{} `json:"network_roles"`
  117. LastLoginTime time.Time `json:"last_login_time"`
  118. }
  119. // UserAuthParams - user auth params struct
  120. type UserAuthParams struct {
  121. UserName string `json:"username"`
  122. Password string `json:"password"`
  123. }
  124. // UserClaims - user claims struct
  125. type UserClaims struct {
  126. Role UserRole
  127. UserName string
  128. jwt.RegisteredClaims
  129. }
  130. type InviteUsersReq struct {
  131. UserEmails []string `json:"user_emails"`
  132. Groups []UserGroupID
  133. }
  134. // UserInvite - model for user invite
  135. type UserInvite struct {
  136. Email string `json:"email"`
  137. Groups []UserGroupID `json:"groups"`
  138. InviteCode string `json:"invite_code"`
  139. }