2022-09-28 09:01:17 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/manifoldco/promptui"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2023-06-19 17:24:02 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// UsersAddCommand configures the command name, flags, and action.
|
|
|
|
var UsersAddCommand = cli.Command{
|
|
|
|
Name: "add",
|
|
|
|
Usage: "Adds a new user account",
|
|
|
|
ArgsUsage: "[username]",
|
|
|
|
Flags: UserFlags,
|
|
|
|
Action: usersAddAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
// usersAddAction adds a new user account.
|
|
|
|
func usersAddAction(ctx *cli.Context) error {
|
|
|
|
return CallWithDependencies(ctx, func(conf *config.Config) error {
|
2022-10-07 19:30:30 +02:00
|
|
|
conf.MigrateDb(false, nil)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
frm := form.NewUserFromCli(ctx)
|
|
|
|
|
|
|
|
interactive := true
|
|
|
|
|
|
|
|
if frm.UserName != "" && frm.Password != "" {
|
|
|
|
log.Debugf("user will be added in non-interactive mode")
|
|
|
|
interactive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && frm.UserName == "" {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Username",
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := prompt.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
frm.UserName = clean.Username(res)
|
2023-02-20 20:24:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if account exists but is deleted.
|
|
|
|
if frm.UserName == "" {
|
|
|
|
return fmt.Errorf("username is required")
|
|
|
|
} else if m := entity.FindUserByName(frm.UserName); m != nil {
|
|
|
|
if !m.Deleted() {
|
|
|
|
return fmt.Errorf("user already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: fmt.Sprintf("Restore user %s?", m.String()),
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := prompt.Run(); err != nil {
|
|
|
|
return fmt.Errorf("user already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.RestoreFromCli(ctx, frm.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("user %s has been restored", m.String())
|
|
|
|
|
|
|
|
return nil
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && frm.UserEmail == "" {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Email",
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := prompt.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
frm.UserEmail = clean.Email(res)
|
|
|
|
}
|
|
|
|
|
2023-06-19 17:24:02 +02:00
|
|
|
if interactive && len([]rune(ctx.String("password"))) < entity.PasswordLength {
|
2022-09-28 09:01:17 +02:00
|
|
|
validate := func(input string) error {
|
2023-06-19 17:24:02 +02:00
|
|
|
if len([]rune(input)) < entity.PasswordLength {
|
2022-11-22 22:14:34 +01:00
|
|
|
return fmt.Errorf("password must have at least %d characters", entity.PasswordLength)
|
2023-06-19 17:24:02 +02:00
|
|
|
} else if len(input) > txt.ClipPassword {
|
|
|
|
return fmt.Errorf("password must have less than %d characters", txt.ClipPassword)
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Password",
|
|
|
|
Validate: validate,
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
resPasswd, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
validateRetype := func(input string) error {
|
|
|
|
if input != resPasswd {
|
|
|
|
return errors.New("passwords do not match")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
confirm := promptui.Prompt{
|
|
|
|
Label: "Retype Password",
|
|
|
|
Validate: validateRetype,
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
resConfirm, err := confirm.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resConfirm != resPasswd {
|
|
|
|
return errors.New("password is invalid, please try again")
|
|
|
|
} else {
|
|
|
|
frm.Password = resPasswd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := entity.AddUser(frm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|