user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/usememos/memos/common"
  5. )
  6. // Role is the type of a role.
  7. type Role string
  8. const (
  9. // Host is the HOST role.
  10. Host Role = "HOST"
  11. // Admin is the ADMIN role.
  12. Admin Role = "ADMIN"
  13. // NormalUser is the USER role.
  14. NormalUser Role = "USER"
  15. )
  16. func (e Role) String() string {
  17. switch e {
  18. case Host:
  19. return "HOST"
  20. case Admin:
  21. return "ADMIN"
  22. case NormalUser:
  23. return "USER"
  24. }
  25. return "USER"
  26. }
  27. type User struct {
  28. ID int `json:"id"`
  29. // Standard fields
  30. RowStatus RowStatus `json:"rowStatus"`
  31. CreatedTs int64 `json:"createdTs"`
  32. UpdatedTs int64 `json:"updatedTs"`
  33. // Domain specific fields
  34. Username string `json:"username"`
  35. Role Role `json:"role"`
  36. Email string `json:"email"`
  37. Nickname string `json:"nickname"`
  38. PasswordHash string `json:"-"`
  39. OpenID string `json:"openId"`
  40. AvatarURL string `json:"avatarUrl"`
  41. UserSettingList []*UserSetting `json:"userSettingList"`
  42. }
  43. type UserCreate struct {
  44. // Domain specific fields
  45. Username string `json:"username"`
  46. Role Role `json:"role"`
  47. Email string `json:"email"`
  48. Nickname string `json:"nickname"`
  49. Password string `json:"password"`
  50. PasswordHash string
  51. OpenID string
  52. }
  53. func (create UserCreate) Validate() error {
  54. if len(create.Username) < 3 {
  55. return fmt.Errorf("username is too short, minimum length is 3")
  56. }
  57. if len(create.Username) > 32 {
  58. return fmt.Errorf("username is too long, maximum length is 32")
  59. }
  60. if len(create.Password) < 3 {
  61. return fmt.Errorf("password is too short, minimum length is 6")
  62. }
  63. if len(create.Password) > 512 {
  64. return fmt.Errorf("password is too long, maximum length is 512")
  65. }
  66. if len(create.Nickname) > 64 {
  67. return fmt.Errorf("nickname is too long, maximum length is 64")
  68. }
  69. if create.Email != "" {
  70. if len(create.Email) > 256 {
  71. return fmt.Errorf("email is too long, maximum length is 256")
  72. }
  73. if !common.ValidateEmail(create.Email) {
  74. return fmt.Errorf("invalid email format")
  75. }
  76. }
  77. return nil
  78. }
  79. type UserPatch struct {
  80. ID int `json:"-"`
  81. // Standard fields
  82. UpdatedTs *int64
  83. RowStatus *RowStatus `json:"rowStatus"`
  84. // Domain specific fields
  85. Username *string `json:"username"`
  86. Email *string `json:"email"`
  87. Nickname *string `json:"nickname"`
  88. Password *string `json:"password"`
  89. ResetOpenID *bool `json:"resetOpenId"`
  90. AvatarURL *string `json:"avatarUrl"`
  91. PasswordHash *string
  92. OpenID *string
  93. }
  94. func (patch UserPatch) Validate() error {
  95. if patch.Username != nil && len(*patch.Username) < 3 {
  96. return fmt.Errorf("username is too short, minimum length is 3")
  97. }
  98. if patch.Username != nil && len(*patch.Username) > 32 {
  99. return fmt.Errorf("username is too long, maximum length is 32")
  100. }
  101. if patch.Password != nil && len(*patch.Password) < 3 {
  102. return fmt.Errorf("password is too short, minimum length is 6")
  103. }
  104. if patch.Password != nil && len(*patch.Password) > 512 {
  105. return fmt.Errorf("password is too long, maximum length is 512")
  106. }
  107. if patch.Nickname != nil && len(*patch.Nickname) > 64 {
  108. return fmt.Errorf("nickname is too long, maximum length is 64")
  109. }
  110. if patch.AvatarURL != nil {
  111. if len(*patch.AvatarURL) > 2<<20 {
  112. return fmt.Errorf("avatar is too large, maximum is 2MB")
  113. }
  114. }
  115. if patch.Email != nil && *patch.Email != "" {
  116. if len(*patch.Email) > 256 {
  117. return fmt.Errorf("email is too long, maximum length is 256")
  118. }
  119. if !common.ValidateEmail(*patch.Email) {
  120. return fmt.Errorf("invalid email format")
  121. }
  122. }
  123. return nil
  124. }
  125. type UserFind struct {
  126. ID *int `json:"id"`
  127. // Standard fields
  128. RowStatus *RowStatus `json:"rowStatus"`
  129. // Domain specific fields
  130. Username *string `json:"username"`
  131. Role *Role
  132. Email *string `json:"email"`
  133. Nickname *string `json:"nickname"`
  134. OpenID *string
  135. }
  136. type UserDelete struct {
  137. ID int
  138. }