user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. IsDisabled *bool
  123. Result SearchUserQueryResult
  124. }
  125. type SearchUserQueryResult struct {
  126. TotalCount int64 `json:"totalCount"`
  127. Users []*UserSearchHitDTO `json:"users"`
  128. Page int `json:"page"`
  129. PerPage int `json:"perPage"`
  130. }
  131. type GetUserOrgListQuery struct {
  132. UserId int64
  133. Result []*UserOrgDTO
  134. }
  135. // ------------------------
  136. // DTO & Projections
  137. type SignedInUser struct {
  138. UserId int64
  139. OrgId int64
  140. OrgName string
  141. OrgRole RoleType
  142. Login string
  143. Name string
  144. Email string
  145. ApiKeyId int64
  146. OrgCount int
  147. IsGrafanaAdmin bool
  148. IsAnonymous bool
  149. HelpFlags1 HelpFlags1
  150. LastSeenAt time.Time
  151. Teams []int64
  152. }
  153. func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
  154. return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
  155. }
  156. func (u *SignedInUser) NameOrFallback() string {
  157. if u.Name != "" {
  158. return u.Name
  159. } else if u.Login != "" {
  160. return u.Login
  161. } else {
  162. return u.Email
  163. }
  164. }
  165. type UpdateUserLastSeenAtCommand struct {
  166. UserId int64
  167. }
  168. func (user *SignedInUser) HasRole(role RoleType) bool {
  169. if user.IsGrafanaAdmin {
  170. return true
  171. }
  172. return user.OrgRole.Includes(role)
  173. }
  174. func (user *SignedInUser) IsRealUser() bool {
  175. return user.UserId != 0
  176. }
  177. type UserProfileDTO struct {
  178. Id int64 `json:"id"`
  179. Email string `json:"email"`
  180. Name string `json:"name"`
  181. Login string `json:"login"`
  182. Theme string `json:"theme"`
  183. OrgId int64 `json:"orgId"`
  184. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  185. IsDisabled bool `json:"isDisabled"`
  186. IsExternal bool `json:"isExternal"`
  187. AuthLabels []string `json:"authLabels"`
  188. }
  189. type UserSearchHitDTO struct {
  190. Id int64 `json:"id"`
  191. Name string `json:"name"`
  192. Login string `json:"login"`
  193. Email string `json:"email"`
  194. AvatarUrl string `json:"avatarUrl"`
  195. IsAdmin bool `json:"isAdmin"`
  196. IsDisabled bool `json:"isDisabled"`
  197. LastSeenAt time.Time `json:"lastSeenAt"`
  198. LastSeenAtAge string `json:"lastSeenAtAge"`
  199. AuthLabels []string `json:"authLabels"`
  200. AuthModule AuthModuleConversion `json:"-"`
  201. }
  202. type UserIdDTO struct {
  203. Id int64 `json:"id"`
  204. Message string `json:"message"`
  205. }
  206. // implement Conversion interface to define custom field mapping (xorm feature)
  207. type AuthModuleConversion []string
  208. func (auth *AuthModuleConversion) FromDB(data []byte) error {
  209. auth_module := string(data)
  210. *auth = []string{auth_module}
  211. return nil
  212. }
  213. // Just a stub, we don't wanna write to database
  214. func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
  215. return []byte{}, nil
  216. }