user.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. Password string `json:"password" binding:"Required"`
  44. IsAdmin bool `json:"-"`
  45. Result User `json:"-"`
  46. }
  47. type UpdateUserCommand struct {
  48. Name string `json:"name"`
  49. Email string `json:"email"`
  50. Login string `json:"login"`
  51. Theme string `json:"theme"`
  52. UserId int64 `json:"-"`
  53. }
  54. type ChangeUserPasswordCommand struct {
  55. OldPassword string `json:"oldPassword"`
  56. NewPassword string `json:"newPassword"`
  57. UserId int64 `json:"-"`
  58. }
  59. type UpdateUserPermissionsCommand struct {
  60. IsGrafanaAdmin bool
  61. UserId int64 `json:"-"`
  62. }
  63. type DeleteUserCommand struct {
  64. UserId int64
  65. }
  66. type SetUsingOrgCommand struct {
  67. UserId int64
  68. OrgId int64
  69. }
  70. // ----------------------
  71. // QUERIES
  72. type GetUserByLoginQuery struct {
  73. LoginOrEmail string
  74. Result *User
  75. }
  76. type GetUserByIdQuery struct {
  77. Id int64
  78. Result *User
  79. }
  80. type GetSignedInUserQuery struct {
  81. UserId int64
  82. Login string
  83. Email string
  84. Result *SignedInUser
  85. }
  86. type GetUserProfileQuery struct {
  87. UserId int64
  88. Result UserProfileDTO
  89. }
  90. type SearchUsersQuery struct {
  91. Query string
  92. Page int
  93. Limit int
  94. Result []*UserSearchHitDTO
  95. }
  96. type GetUserOrgListQuery struct {
  97. UserId int64
  98. Result []*UserOrgDTO
  99. }
  100. // ------------------------
  101. // DTO & Projections
  102. type SignedInUser struct {
  103. UserId int64
  104. OrgId int64
  105. OrgName string
  106. OrgRole RoleType
  107. Login string
  108. Name string
  109. Email string
  110. Theme string
  111. ApiKeyId int64
  112. IsGrafanaAdmin bool
  113. }
  114. type UserProfileDTO struct {
  115. Email string `json:"email"`
  116. Name string `json:"name"`
  117. Login string `json:"login"`
  118. Theme string `json:"theme"`
  119. OrgId int64 `json:"orgId"`
  120. IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
  121. }
  122. type UserSearchHitDTO struct {
  123. Id int64 `json:"id"`
  124. Name string `json:"name"`
  125. Login string `json:"login"`
  126. Email string `json:"email"`
  127. IsAdmin bool `json:"isAdmin"`
  128. }