user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Result SearchUserQueryResult
  121. }
  122. type SearchUserQueryResult struct {
  123. TotalCount int64 `json:"totalCount"`
  124. Users []*UserSearchHitDTO `json:"users"`
  125. Page int `json:"page"`
  126. PerPage int `json:"perPage"`
  127. }
  128. type GetUserOrgListQuery struct {
  129. UserId int64
  130. Result []*UserOrgDTO
  131. }
  132. // ------------------------
  133. // DTO & Projections
  134. type SignedInUser struct {
  135. UserId int64
  136. OrgId int64
  137. OrgName string
  138. OrgRole RoleType
  139. Login string
  140. Name string
  141. Email string
  142. ApiKeyId int64
  143. OrgCount int
  144. IsGrafanaAdmin bool
  145. IsAnonymous bool
  146. HelpFlags1 HelpFlags1
  147. LastSeenAt time.Time
  148. Teams []int64
  149. }
  150. func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
  151. return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
  152. }
  153. func (u *SignedInUser) NameOrFallback() string {
  154. if u.Name != "" {
  155. return u.Name
  156. } else if u.Login != "" {
  157. return u.Login
  158. } else {
  159. return u.Email
  160. }
  161. }
  162. type UpdateUserLastSeenAtCommand struct {
  163. UserId int64
  164. }
  165. func (user *SignedInUser) HasRole(role RoleType) bool {
  166. if user.IsGrafanaAdmin {
  167. return true
  168. }
  169. return user.OrgRole.Includes(role)
  170. }
  171. type UserProfileDTO struct {
  172. Id int64 `json:"id"`
  173. Email string `json:"email"`
  174. Name string `json:"name"`
  175. Login string `json:"login"`
  176. Theme string `json:"theme"`
  177. OrgId int64 `json:"orgId"`
  178. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  179. IsDisabled bool `json:"isDisabled"`
  180. }
  181. type UserSearchHitDTO struct {
  182. Id int64 `json:"id"`
  183. Name string `json:"name"`
  184. Login string `json:"login"`
  185. Email string `json:"email"`
  186. AvatarUrl string `json:"avatarUrl"`
  187. IsAdmin bool `json:"isAdmin"`
  188. IsDisabled bool `json:"isDisabled"`
  189. LastSeenAt time.Time `json:"lastSeenAt"`
  190. LastSeenAtAge string `json:"lastSeenAtAge"`
  191. }
  192. type UserIdDTO struct {
  193. Id int64 `json:"id"`
  194. Message string `json:"message"`
  195. }