user.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. IsDisabled bool
  56. SkipOrgSetup bool
  57. DefaultOrgRole string
  58. Result User
  59. }
  60. type UpdateUserCommand struct {
  61. Name string `json:"name"`
  62. Email string `json:"email"`
  63. Login string `json:"login"`
  64. Theme string `json:"theme"`
  65. UserId int64 `json:"-"`
  66. }
  67. type ChangeUserPasswordCommand struct {
  68. OldPassword string `json:"oldPassword"`
  69. NewPassword string `json:"newPassword"`
  70. UserId int64 `json:"-"`
  71. }
  72. type UpdateUserPermissionsCommand struct {
  73. IsGrafanaAdmin bool
  74. UserId int64 `json:"-"`
  75. }
  76. type DisableUserCommand struct {
  77. UserId int64
  78. IsDisabled bool
  79. }
  80. type BatchDisableUsersCommand struct {
  81. UserIds []int64
  82. IsDisabled bool
  83. }
  84. type DeleteUserCommand struct {
  85. UserId int64
  86. }
  87. type SetUsingOrgCommand struct {
  88. UserId int64
  89. OrgId int64
  90. }
  91. // ----------------------
  92. // QUERIES
  93. type GetUserByLoginQuery struct {
  94. LoginOrEmail string
  95. Result *User
  96. }
  97. type GetUserByEmailQuery struct {
  98. Email string
  99. Result *User
  100. }
  101. type GetUserByIdQuery struct {
  102. Id int64
  103. Result *User
  104. }
  105. type GetSignedInUserQuery struct {
  106. UserId int64
  107. Login string
  108. Email string
  109. OrgId int64
  110. Result *SignedInUser
  111. }
  112. type GetUserProfileQuery struct {
  113. UserId int64
  114. Result UserProfileDTO
  115. }
  116. type SearchUsersQuery struct {
  117. OrgId int64
  118. Query string
  119. Page int
  120. Limit int
  121. AuthModule string
  122. // We have to use string not bool, since there is cases when
  123. // we don't care if user is disabled or not
  124. IsDisabled string
  125. Result SearchUserQueryResult
  126. }
  127. type SearchUserQueryResult struct {
  128. TotalCount int64 `json:"totalCount"`
  129. Users []*UserSearchHitDTO `json:"users"`
  130. Page int `json:"page"`
  131. PerPage int `json:"perPage"`
  132. }
  133. type GetUserOrgListQuery struct {
  134. UserId int64
  135. Result []*UserOrgDTO
  136. }
  137. // ------------------------
  138. // DTO & Projections
  139. type SignedInUser struct {
  140. UserId int64
  141. OrgId int64
  142. OrgName string
  143. OrgRole RoleType
  144. Login string
  145. Name string
  146. Email string
  147. ApiKeyId int64
  148. OrgCount int
  149. IsGrafanaAdmin bool
  150. IsAnonymous bool
  151. HelpFlags1 HelpFlags1
  152. LastSeenAt time.Time
  153. Teams []int64
  154. }
  155. func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
  156. return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
  157. }
  158. func (u *SignedInUser) NameOrFallback() string {
  159. if u.Name != "" {
  160. return u.Name
  161. } else if u.Login != "" {
  162. return u.Login
  163. } else {
  164. return u.Email
  165. }
  166. }
  167. type UpdateUserLastSeenAtCommand struct {
  168. UserId int64
  169. }
  170. func (user *SignedInUser) HasRole(role RoleType) bool {
  171. if user.IsGrafanaAdmin {
  172. return true
  173. }
  174. return user.OrgRole.Includes(role)
  175. }
  176. type UserProfileDTO struct {
  177. Id int64 `json:"id"`
  178. Email string `json:"email"`
  179. Name string `json:"name"`
  180. Login string `json:"login"`
  181. Theme string `json:"theme"`
  182. OrgId int64 `json:"orgId"`
  183. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  184. IsDisabled bool `json:"isDisabled"`
  185. IsExternal bool `json:"isExternal"`
  186. AuthLabels []string `json:"authLabels"`
  187. }
  188. type UserSearchHitDTO struct {
  189. Id int64 `json:"id"`
  190. Name string `json:"name"`
  191. Login string `json:"login"`
  192. Email string `json:"email"`
  193. AvatarUrl string `json:"avatarUrl"`
  194. IsAdmin bool `json:"isAdmin"`
  195. IsDisabled bool `json:"isDisabled"`
  196. LastSeenAt time.Time `json:"lastSeenAt"`
  197. LastSeenAtAge string `json:"lastSeenAtAge"`
  198. AuthLabels []string `json:"authLabels"`
  199. AuthModule AuthModuleConversion `json:"-"`
  200. }
  201. type UserIdDTO struct {
  202. Id int64 `json:"id"`
  203. Message string `json:"message"`
  204. }
  205. // implement Conversion interface to define custom field mapping (xorm feature)
  206. type AuthModuleConversion []string
  207. func (auth *AuthModuleConversion) FromDB(data []byte) error {
  208. auth_module := string(data)
  209. *auth = []string{auth_module}
  210. return nil
  211. }
  212. // Just a stub, we don't wanna write to database
  213. func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
  214. return []byte{}, nil
  215. }