2023-03-27 16:22:49 +03:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2023-07-02 09:27:23 +03:00
|
|
|
"github.com/usememos/memos/store"
|
2023-03-27 16:22:49 +03:00
|
|
|
)
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
func Execute(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error {
|
2023-03-27 16:22:49 +03:00
|
|
|
s := setupService{store: store}
|
|
|
|
return s.Setup(ctx, hostUsername, hostPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
type setupService struct {
|
2023-07-02 09:27:23 +03:00
|
|
|
store *store.Store
|
2023-03-27 16:22:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
func (s setupService) Setup(ctx context.Context, hostUsername, hostPassword string) error {
|
2023-03-27 16:22:49 +03:00
|
|
|
if err := s.makeSureHostUserNotExists(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.createUser(ctx, hostUsername, hostPassword); err != nil {
|
|
|
|
return fmt.Errorf("create user: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s setupService) makeSureHostUserNotExists(ctx context.Context) error {
|
|
|
|
hostUserType := api.Host
|
|
|
|
existedHostUsers, err := s.store.FindUserList(ctx, &api.UserFind{
|
|
|
|
Role: &hostUserType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("find user list: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(existedHostUsers) != 0 {
|
|
|
|
return errors.New("host user already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
func (s setupService) createUser(ctx context.Context, hostUsername, hostPassword string) error {
|
|
|
|
userCreate := &store.User{
|
2023-03-27 16:22:49 +03:00
|
|
|
Username: hostUsername,
|
|
|
|
// The new signup user should be normal user by default.
|
2023-07-02 09:27:23 +03:00
|
|
|
Role: store.Host,
|
2023-03-27 16:22:49 +03:00
|
|
|
Nickname: hostUsername,
|
|
|
|
OpenID: common.GenUUID(),
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
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")
|
|
|
|
}
|
2023-03-27 16:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(hostPassword), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2023-07-02 09:27:23 +03:00
|
|
|
return fmt.Errorf("failed to hash password: %w", err)
|
2023-03-27 16:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
userCreate.PasswordHash = string(passwordHash)
|
2023-07-02 09:27:23 +03:00
|
|
|
if _, err := s.store.CreateUserV1(ctx, userCreate); err != nil {
|
|
|
|
return fmt.Errorf("failed to create user: %w", err)
|
2023-03-27 16:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|