memos/api/user.go

146 lines
3.4 KiB
Go
Raw Normal View History

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