user.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. // Typed errors
  7. var (
  8. ErrUserNotFound = errors.New("User not found")
  9. )
  10. type User struct {
  11. Id int64
  12. Version int
  13. Email string
  14. Name string
  15. Login string
  16. Password string
  17. Salt string
  18. Rands string
  19. Company string
  20. EmailVerified bool
  21. Theme string
  22. IsAdmin bool
  23. OrgId int64
  24. Created time.Time
  25. Updated time.Time
  26. }
  27. func (u *User) NameOrFallback() string {
  28. if u.Name != "" {
  29. return u.Name
  30. } else if u.Login != "" {
  31. return u.Login
  32. } else {
  33. return u.Email
  34. }
  35. }
  36. // ---------------------
  37. // COMMANDS
  38. type CreateUserCommand struct {
  39. Email string `json:"email" binding:"Required"`
  40. Login string `json:"login"`
  41. Name string `json:"name"`
  42. Company string `json:"compay"`
  43. OrgName string `json:"orgName"`
  44. Password string `json:"password" binding:"Required"`
  45. IsAdmin bool `json:"-"`
  46. Result User `json:"-"`
  47. }
  48. type UpdateUserCommand struct {
  49. Name string `json:"name"`
  50. Email string `json:"email"`
  51. Login string `json:"login"`
  52. Theme string `json:"theme"`
  53. UserId int64 `json:"-"`
  54. }
  55. type ChangeUserPasswordCommand struct {
  56. OldPassword string `json:"oldPassword"`
  57. NewPassword string `json:"newPassword"`
  58. UserId int64 `json:"-"`
  59. }
  60. type UpdateUserPermissionsCommand struct {
  61. IsGrafanaAdmin bool
  62. UserId int64 `json:"-"`
  63. }
  64. type DeleteUserCommand struct {
  65. UserId int64
  66. }
  67. type SetUsingOrgCommand struct {
  68. UserId int64
  69. OrgId int64
  70. }
  71. // ----------------------
  72. // QUERIES
  73. type GetUserByLoginQuery struct {
  74. LoginOrEmail string
  75. Result *User
  76. }
  77. type GetUserByIdQuery struct {
  78. Id int64
  79. Result *User
  80. }
  81. type GetSignedInUserQuery struct {
  82. UserId int64
  83. Login string
  84. Email string
  85. Result *SignedInUser
  86. }
  87. type GetUserProfileQuery struct {
  88. UserId int64
  89. Result UserProfileDTO
  90. }
  91. type SearchUsersQuery struct {
  92. Query string
  93. Page int
  94. Limit int
  95. Result []*UserSearchHitDTO
  96. }
  97. type GetUserOrgListQuery struct {
  98. UserId int64
  99. Result []*UserOrgDTO
  100. }
  101. // ------------------------
  102. // DTO & Projections
  103. type SignedInUser struct {
  104. UserId int64
  105. OrgId int64
  106. OrgName string
  107. OrgRole RoleType
  108. Login string
  109. Name string
  110. Email string
  111. Theme string
  112. ApiKeyId int64
  113. IsGrafanaAdmin bool
  114. }
  115. type UserProfileDTO struct {
  116. Email string `json:"email"`
  117. Name string `json:"name"`
  118. Login string `json:"login"`
  119. Theme string `json:"theme"`
  120. OrgId int64 `json:"orgId"`
  121. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  122. }
  123. type UserSearchHitDTO struct {
  124. Id int64 `json:"id"`
  125. Name string `json:"name"`
  126. Login string `json:"login"`
  127. Email string `json:"email"`
  128. IsAdmin bool `json:"isAdmin"`
  129. }