chore: update user store names (#1877)

* chore: update user store names

* chore: update
This commit is contained in:
boojack
2023-07-02 14:27:23 +08:00
committed by GitHub
parent ca770c87d6
commit 9a8d43bf88
12 changed files with 187 additions and 275 deletions

View File

@@ -9,30 +9,19 @@ import (
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/store"
)
func Execute(
ctx context.Context,
store store,
hostUsername, hostPassword string,
) error {
func Execute(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error {
s := setupService{store: store}
return s.Setup(ctx, hostUsername, hostPassword)
}
type store interface {
FindUserList(ctx context.Context, find *api.UserFind) ([]*api.User, error)
CreateUser(ctx context.Context, create *api.UserCreate) (*api.User, error)
}
type setupService struct {
store store
store *store.Store
}
func (s setupService) Setup(
ctx context.Context,
hostUsername, hostPassword string,
) error {
func (s setupService) Setup(ctx context.Context, hostUsername, hostPassword string) error {
if err := s.makeSureHostUserNotExists(ctx); err != nil {
return err
}
@@ -59,31 +48,47 @@ func (s setupService) makeSureHostUserNotExists(ctx context.Context) error {
return nil
}
func (s setupService) createUser(
ctx context.Context,
hostUsername, hostPassword string,
) error {
userCreate := &api.UserCreate{
func (s setupService) createUser(ctx context.Context, hostUsername, hostPassword string) error {
userCreate := &store.User{
Username: hostUsername,
// The new signup user should be normal user by default.
Role: api.Host,
Role: store.Host,
Nickname: hostUsername,
Password: hostPassword,
OpenID: common.GenUUID(),
}
if err := userCreate.Validate(); err != nil {
return fmt.Errorf("validate: %w", err)
if len(userCreate.Username) < 3 {
return fmt.Errorf("username is too short, minimum length is 3")
}
if len(userCreate.Username) > 32 {
return fmt.Errorf("username is too long, maximum length is 32")
}
if len(hostPassword) < 3 {
return fmt.Errorf("password is too short, minimum length is 3")
}
if len(hostPassword) > 512 {
return fmt.Errorf("password is too long, maximum length is 512")
}
if len(userCreate.Nickname) > 64 {
return fmt.Errorf("nickname is too long, maximum length is 64")
}
if userCreate.Email != "" {
if len(userCreate.Email) > 256 {
return fmt.Errorf("email is too long, maximum length is 256")
}
if !common.ValidateEmail(userCreate.Email) {
return fmt.Errorf("invalid email format")
}
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(hostPassword), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("hash password: %w", err)
return fmt.Errorf("failed to hash password: %w", err)
}
userCreate.PasswordHash = string(passwordHash)
if _, err := s.store.CreateUser(ctx, userCreate); err != nil {
return fmt.Errorf("create user: %w", err)
if _, err := s.store.CreateUserV1(ctx, userCreate); err != nil {
return fmt.Errorf("failed to create user: %w", err)
}
return nil