user.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. UserSettingList []*UserSetting `json:"userSettingList"`
  41. }
  42. type UserCreate struct {
  43. // Domain specific fields
  44. Username string `json:"username"`
  45. Role Role `json:"role"`
  46. Email string `json:"email"`
  47. Nickname string `json:"nickname"`
  48. Password string `json:"password"`
  49. PasswordHash string
  50. OpenID string
  51. }
  52. func (create UserCreate) Validate() error {
  53. if len(create.Username) < 4 {
  54. return fmt.Errorf("username is too short, minimum length is 4")
  55. }
  56. if len(create.Username) > 32 {
  57. return fmt.Errorf("username is too long, maximum length is 32")
  58. }
  59. if len(create.Password) < 4 {
  60. return fmt.Errorf("password is too short, minimum length is 4")
  61. }
  62. if len(create.Nickname) > 64 {
  63. return fmt.Errorf("nickname is too long, maximum length is 64")
  64. }
  65. if create.Email != "" {
  66. if len(create.Email) > 256 {
  67. return fmt.Errorf("email is too long, maximum length is 256")
  68. }
  69. if !common.ValidateEmail(create.Email) {
  70. return fmt.Errorf("invalid email format")
  71. }
  72. }
  73. return nil
  74. }
  75. type UserPatch struct {
  76. ID int `json:"-"`
  77. // Standard fields
  78. UpdatedTs *int64
  79. RowStatus *RowStatus `json:"rowStatus"`
  80. // Domain specific fields
  81. Username *string `json:"username"`
  82. Email *string `json:"email"`
  83. Nickname *string `json:"nickname"`
  84. Password *string `json:"password"`
  85. ResetOpenID *bool `json:"resetOpenId"`
  86. PasswordHash *string
  87. OpenID *string
  88. }
  89. func (patch UserPatch) Validate() error {
  90. if patch.Username != nil && len(*patch.Username) < 4 {
  91. return fmt.Errorf("username is too short, minimum length is 4")
  92. }
  93. if patch.Username != nil && len(*patch.Username) > 32 {
  94. return fmt.Errorf("username is too long, maximum length is 32")
  95. }
  96. if patch.Password != nil && len(*patch.Password) < 4 {
  97. return fmt.Errorf("password is too short, minimum length is 4")
  98. }
  99. if patch.Nickname != nil && len(*patch.Nickname) > 64 {
  100. return fmt.Errorf("nickname is too long, maximum length is 64")
  101. }
  102. if patch.Email != nil {
  103. if len(*patch.Email) > 256 {
  104. return fmt.Errorf("email is too long, maximum length is 256")
  105. }
  106. if !common.ValidateEmail(*patch.Email) {
  107. return fmt.Errorf("invalid email format")
  108. }
  109. }
  110. return nil
  111. }
  112. type UserFind struct {
  113. ID *int `json:"id"`
  114. // Standard fields
  115. RowStatus *RowStatus `json:"rowStatus"`
  116. // Domain specific fields
  117. Username *string `json:"username"`
  118. Role *Role
  119. Email *string `json:"email"`
  120. Nickname *string `json:"nickname"`
  121. OpenID *string
  122. }
  123. type UserDelete struct {
  124. ID int
  125. }