2021-12-08 18:43:14 +03:00
|
|
|
package api
|
|
|
|
|
2022-08-20 16:03:15 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-27 16:51:43 +03:00
|
|
|
|
|
|
|
"github.com/usememos/memos/common"
|
2022-08-20 16:03:15 +03:00
|
|
|
)
|
|
|
|
|
2022-05-15 05:57:54 +03:00
|
|
|
// Role is the type of a role.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
const (
|
2022-07-08 17:16:18 +03:00
|
|
|
// Host is the HOST role.
|
|
|
|
Host Role = "HOST"
|
2022-11-23 17:27:21 +03:00
|
|
|
// Admin is the ADMIN role.
|
|
|
|
Admin Role = "ADMIN"
|
2022-05-15 05:57:54 +03:00
|
|
|
// NormalUser is the USER role.
|
|
|
|
NormalUser Role = "USER"
|
|
|
|
)
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
func (e Role) String() string {
|
|
|
|
switch e {
|
2022-07-08 17:16:18 +03:00
|
|
|
case Host:
|
|
|
|
return "HOST"
|
2022-11-23 17:27:21 +03:00
|
|
|
case Admin:
|
|
|
|
return "ADMIN"
|
2022-05-19 13:32:04 +03:00
|
|
|
case NormalUser:
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
type User struct {
|
2022-05-03 06:49:10 +03:00
|
|
|
ID int `json:"id"`
|
|
|
|
|
|
|
|
// Standard fields
|
2022-05-19 13:32:04 +03:00
|
|
|
RowStatus RowStatus `json:"rowStatus"`
|
|
|
|
CreatedTs int64 `json:"createdTs"`
|
|
|
|
UpdatedTs int64 `json:"updatedTs"`
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-05-03 06:49:10 +03:00
|
|
|
// Domain specific fields
|
2022-11-23 17:27:21 +03:00
|
|
|
Username string `json:"username"`
|
2022-08-13 09:35:33 +03:00
|
|
|
Role Role `json:"role"`
|
2022-11-23 17:27:21 +03:00
|
|
|
Email string `json:"email"`
|
|
|
|
Nickname string `json:"nickname"`
|
2022-08-13 09:35:33 +03:00
|
|
|
PasswordHash string `json:"-"`
|
|
|
|
OpenID string `json:"openId"`
|
2023-02-17 18:55:56 +03:00
|
|
|
AvatarURL string `json:"avatarUrl"`
|
2022-08-13 09:35:33 +03:00
|
|
|
UserSettingList []*UserSetting `json:"userSettingList"`
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
type UserCreate struct {
|
2022-05-03 06:49:10 +03:00
|
|
|
// Domain specific fields
|
2022-11-23 17:27:21 +03:00
|
|
|
Username string `json:"username"`
|
2022-05-16 17:19:39 +03:00
|
|
|
Role Role `json:"role"`
|
2022-11-23 17:27:21 +03:00
|
|
|
Email string `json:"email"`
|
|
|
|
Nickname string `json:"nickname"`
|
2022-05-16 17:19:39 +03:00
|
|
|
Password string `json:"password"`
|
2022-02-06 11:19:20 +03:00
|
|
|
PasswordHash string
|
2022-05-15 05:57:54 +03:00
|
|
|
OpenID string
|
2021-12-09 17:02:57 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:03:15 +03:00
|
|
|
func (create UserCreate) Validate() error {
|
2023-02-11 10:15:56 +03:00
|
|
|
if len(create.Username) < 3 {
|
|
|
|
return fmt.Errorf("username is too short, minimum length is 3")
|
2022-08-20 16:03:15 +03:00
|
|
|
}
|
2022-12-27 16:51:43 +03:00
|
|
|
if len(create.Username) > 32 {
|
|
|
|
return fmt.Errorf("username is too long, maximum length is 32")
|
|
|
|
}
|
2023-02-25 09:59:29 +03:00
|
|
|
if len(create.Password) < 3 {
|
|
|
|
return fmt.Errorf("password is too short, minimum length is 6")
|
|
|
|
}
|
|
|
|
if len(create.Password) > 512 {
|
|
|
|
return fmt.Errorf("password is too long, maximum length is 512")
|
|
|
|
}
|
2022-12-27 16:51:43 +03:00
|
|
|
if len(create.Nickname) > 64 {
|
|
|
|
return fmt.Errorf("nickname is too long, maximum length is 64")
|
|
|
|
}
|
|
|
|
if create.Email != "" {
|
|
|
|
if len(create.Email) > 256 {
|
|
|
|
return fmt.Errorf("email is too long, maximum length is 256")
|
|
|
|
}
|
2022-12-30 08:10:52 +03:00
|
|
|
if !common.ValidateEmail(create.Email) {
|
2022-12-27 16:51:43 +03:00
|
|
|
return fmt.Errorf("invalid email format")
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 16:03:15 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
type UserPatch struct {
|
2022-12-23 13:38:24 +03:00
|
|
|
ID int `json:"-"`
|
2021-12-14 15:40:24 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
// Standard fields
|
2022-10-26 18:00:09 +03:00
|
|
|
UpdatedTs *int64
|
2022-05-19 13:32:04 +03:00
|
|
|
RowStatus *RowStatus `json:"rowStatus"`
|
|
|
|
|
2022-05-03 06:49:10 +03:00
|
|
|
// Domain specific fields
|
2022-11-23 17:27:21 +03:00
|
|
|
Username *string `json:"username"`
|
2022-05-15 05:57:54 +03:00
|
|
|
Email *string `json:"email"`
|
2022-11-23 17:27:21 +03:00
|
|
|
Nickname *string `json:"nickname"`
|
2022-05-03 06:49:10 +03:00
|
|
|
Password *string `json:"password"`
|
|
|
|
ResetOpenID *bool `json:"resetOpenId"`
|
2023-02-17 18:55:56 +03:00
|
|
|
AvatarURL *string `json:"avatarUrl"`
|
2022-05-15 05:57:54 +03:00
|
|
|
PasswordHash *string
|
|
|
|
OpenID *string
|
2021-12-14 15:40:24 +03:00
|
|
|
}
|
|
|
|
|
2022-12-27 16:51:43 +03:00
|
|
|
func (patch UserPatch) Validate() error {
|
2023-02-11 10:15:56 +03:00
|
|
|
if patch.Username != nil && len(*patch.Username) < 3 {
|
|
|
|
return fmt.Errorf("username is too short, minimum length is 3")
|
2022-12-27 16:51:43 +03:00
|
|
|
}
|
|
|
|
if patch.Username != nil && len(*patch.Username) > 32 {
|
|
|
|
return fmt.Errorf("username is too long, maximum length is 32")
|
|
|
|
}
|
2023-03-06 14:13:09 +03:00
|
|
|
if patch.Password != nil && len(*patch.Password) < 3 {
|
2023-02-25 09:59:29 +03:00
|
|
|
return fmt.Errorf("password is too short, minimum length is 6")
|
|
|
|
}
|
2023-03-06 14:13:09 +03:00
|
|
|
if patch.Password != nil && len(*patch.Password) > 512 {
|
2023-02-25 09:59:29 +03:00
|
|
|
return fmt.Errorf("password is too long, maximum length is 512")
|
|
|
|
}
|
2022-12-27 16:51:43 +03:00
|
|
|
if patch.Nickname != nil && len(*patch.Nickname) > 64 {
|
|
|
|
return fmt.Errorf("nickname is too long, maximum length is 64")
|
|
|
|
}
|
2023-02-18 05:48:31 +03:00
|
|
|
if patch.AvatarURL != nil {
|
|
|
|
if len(*patch.AvatarURL) > 2<<20 {
|
|
|
|
return fmt.Errorf("avatar is too large, maximum is 2MB")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if patch.Email != nil && *patch.Email != "" {
|
2022-12-27 16:51:43 +03:00
|
|
|
if len(*patch.Email) > 256 {
|
|
|
|
return fmt.Errorf("email is too long, maximum length is 256")
|
|
|
|
}
|
2022-12-30 08:10:52 +03:00
|
|
|
if !common.ValidateEmail(*patch.Email) {
|
2022-12-27 16:51:43 +03:00
|
|
|
return fmt.Errorf("invalid email format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
type UserFind struct {
|
2022-05-02 21:05:43 +03:00
|
|
|
ID *int `json:"id"`
|
2021-12-09 17:02:57 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
// Standard fields
|
|
|
|
RowStatus *RowStatus `json:"rowStatus"`
|
|
|
|
|
2022-05-03 06:49:10 +03:00
|
|
|
// Domain specific fields
|
2022-11-23 17:27:21 +03:00
|
|
|
Username *string `json:"username"`
|
|
|
|
Role *Role
|
|
|
|
Email *string `json:"email"`
|
|
|
|
Nickname *string `json:"nickname"`
|
|
|
|
OpenID *string
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
2022-07-26 16:41:20 +03:00
|
|
|
|
|
|
|
type UserDelete struct {
|
|
|
|
ID int
|
|
|
|
}
|