user.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. // Typed errors
  7. var (
  8. ErrUserNotFound = errors.New("User not found")
  9. ErrLastGrafanaAdmin = errors.New("Cannot remove last grafana admin")
  10. )
  11. type Password string
  12. func (p Password) IsWeak() bool {
  13. return len(p) <= 4
  14. }
  15. type User struct {
  16. Id int64
  17. Version int
  18. Email string
  19. Name string
  20. Login string
  21. Password string
  22. Salt string
  23. Rands string
  24. Company string
  25. EmailVerified bool
  26. Theme string
  27. HelpFlags1 HelpFlags1
  28. IsDisabled bool
  29. IsAdmin bool
  30. OrgId int64
  31. Created time.Time
  32. Updated time.Time
  33. LastSeenAt time.Time
  34. }
  35. func (u *User) NameOrFallback() string {
  36. if u.Name != "" {
  37. return u.Name
  38. } else if u.Login != "" {
  39. return u.Login
  40. } else {
  41. return u.Email
  42. }
  43. }
  44. // ---------------------
  45. // COMMANDS
  46. type CreateUserCommand struct {
  47. Email string
  48. Login string
  49. Name string
  50. Company string
  51. OrgName string
  52. Password string
  53. EmailVerified bool
  54. IsAdmin bool
  55. SkipOrgSetup bool
  56. DefaultOrgRole string
  57. Result User
  58. }
  59. type UpdateUserCommand struct {
  60. Name string `json:"name"`
  61. Email string `json:"email"`
  62. Login string `json:"login"`
  63. Theme string `json:"theme"`
  64. UserId int64 `json:"-"`
  65. }
  66. type ChangeUserPasswordCommand struct {
  67. OldPassword string `json:"oldPassword"`
  68. NewPassword string `json:"newPassword"`
  69. UserId int64 `json:"-"`
  70. }
  71. type UpdateUserPermissionsCommand struct {
  72. IsGrafanaAdmin bool
  73. UserId int64 `json:"-"`
  74. }
  75. type DisableUserCommand struct {
  76. UserId int64
  77. IsDisabled bool
  78. }
  79. type BatchDisableUsersCommand struct {
  80. UserIds []int64
  81. IsDisabled bool
  82. }
  83. type DeleteUserCommand struct {
  84. UserId int64
  85. }
  86. type SetUsingOrgCommand struct {
  87. UserId int64
  88. OrgId int64
  89. }
  90. // ----------------------
  91. // QUERIES
  92. type GetUserByLoginQuery struct {
  93. LoginOrEmail string
  94. Result *User
  95. }
  96. type GetUserByEmailQuery struct {
  97. Email string
  98. Result *User
  99. }
  100. type GetUserByIdQuery struct {
  101. Id int64
  102. Result *User
  103. }
  104. type GetSignedInUserQuery struct {
  105. UserId int64
  106. Login string
  107. Email string
  108. OrgId int64
  109. Result *SignedInUser
  110. }
  111. type GetUserProfileQuery struct {
  112. UserId int64
  113. Result UserProfileDTO
  114. }
  115. type SearchUsersQuery struct {
  116. OrgId int64
  117. Query string
  118. Page int
  119. Limit int
  120. AuthModule string
  121. Result SearchUserQueryResult
  122. }
  123. type SearchUserQueryResult struct {
  124. TotalCount int64 `json:"totalCount"`
  125. Users []*UserSearchHitDTO `json:"users"`
  126. Page int `json:"page"`
  127. PerPage int `json:"perPage"`
  128. }
  129. type GetUserOrgListQuery struct {
  130. UserId int64
  131. Result []*UserOrgDTO
  132. }
  133. // ------------------------
  134. // DTO & Projections
  135. type SignedInUser struct {
  136. UserId int64
  137. OrgId int64
  138. OrgName string
  139. OrgRole RoleType
  140. Login string
  141. Name string
  142. Email string
  143. ApiKeyId int64
  144. OrgCount int
  145. IsGrafanaAdmin bool
  146. IsAnonymous bool
  147. HelpFlags1 HelpFlags1
  148. LastSeenAt time.Time
  149. Teams []int64
  150. }
  151. func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
  152. return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
  153. }
  154. func (u *SignedInUser) NameOrFallback() string {
  155. if u.Name != "" {
  156. return u.Name
  157. } else if u.Login != "" {
  158. return u.Login
  159. } else {
  160. return u.Email
  161. }
  162. }
  163. type UpdateUserLastSeenAtCommand struct {
  164. UserId int64
  165. }
  166. func (user *SignedInUser) HasRole(role RoleType) bool {
  167. if user.IsGrafanaAdmin {
  168. return true
  169. }
  170. return user.OrgRole.Includes(role)
  171. }
  172. type UserProfileDTO struct {
  173. Id int64 `json:"id"`
  174. Email string `json:"email"`
  175. Name string `json:"name"`
  176. Login string `json:"login"`
  177. Theme string `json:"theme"`
  178. OrgId int64 `json:"orgId"`
  179. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  180. IsDisabled bool `json:"isDisabled"`
  181. AuthModule []string `json:"authModule"`
  182. }
  183. type UserSearchHitDTO struct {
  184. Id int64 `json:"id"`
  185. Name string `json:"name"`
  186. Login string `json:"login"`
  187. Email string `json:"email"`
  188. AvatarUrl string `json:"avatarUrl"`
  189. IsAdmin bool `json:"isAdmin"`
  190. IsDisabled bool `json:"isDisabled"`
  191. LastSeenAt time.Time `json:"lastSeenAt"`
  192. LastSeenAtAge string `json:"lastSeenAtAge"`
  193. AuthModule AuthModuleConversion `json:"authModule"`
  194. }
  195. type UserIdDTO struct {
  196. Id int64 `json:"id"`
  197. Message string `json:"message"`
  198. }
  199. // implement Conversion interface to define custom field mapping (xorm feature)
  200. type AuthModuleConversion []string
  201. func (auth *AuthModuleConversion) FromDB(data []byte) error {
  202. auth_module := string(data)
  203. *auth = []string{auth_module}
  204. return nil
  205. }
  206. // Just a stub, we don't wanna write to database
  207. func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
  208. return []byte{}, nil
  209. }