2021-08-18 20:19:14 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-08-20 00:10:26 +02:00
|
|
|
"github.com/manifoldco/promptui"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2021-10-06 08:31:35 +02:00
|
|
|
// UsersCommand registers user management subcommands.
|
2021-08-20 00:10:26 +02:00
|
|
|
var UsersCommand = cli.Command{
|
2021-08-18 20:19:14 +02:00
|
|
|
Name: "users",
|
2021-10-06 08:31:35 +02:00
|
|
|
Usage: "User management subcommands",
|
2021-08-18 20:19:14 +02:00
|
|
|
Subcommands: []cli.Command{
|
2021-08-20 00:10:26 +02:00
|
|
|
{
|
|
|
|
Name: "list",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Lists registered users",
|
2021-08-20 00:10:26 +02:00
|
|
|
Action: usersListAction,
|
|
|
|
},
|
2021-08-18 20:19:14 +02:00
|
|
|
{
|
|
|
|
Name: "add",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Adds a new user",
|
2021-08-20 00:10:26 +02:00
|
|
|
Action: usersAddAction,
|
2021-08-18 20:19:14 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "fullname, n",
|
|
|
|
Usage: "full name of the new user",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username, u",
|
|
|
|
Usage: "unique username",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password, p",
|
|
|
|
Usage: "sets the users password",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email, m",
|
|
|
|
Usage: "sets the users email",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-08-20 00:10:26 +02:00
|
|
|
Name: "update",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Updates user information",
|
2021-08-23 12:28:07 +02:00
|
|
|
Action: usersUpdateAction,
|
2021-08-18 20:19:14 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "fullname, n",
|
|
|
|
Usage: "full name of the new user",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password, p",
|
|
|
|
Usage: "sets the users password",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email, m",
|
|
|
|
Usage: "sets the users email",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "delete",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Removes an existing user",
|
2021-08-20 00:10:26 +02:00
|
|
|
Action: usersDeleteAction,
|
|
|
|
ArgsUsage: "[username]",
|
2021-08-18 20:19:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
func usersAddAction(ctx *cli.Context) error {
|
|
|
|
return callWithDependencies(ctx, func(conf *config.Config) error {
|
2021-08-18 20:19:14 +02:00
|
|
|
|
|
|
|
uc := form.UserCreate{
|
|
|
|
UserName: strings.TrimSpace(ctx.String("username")),
|
|
|
|
FullName: strings.TrimSpace(ctx.String("fullname")),
|
|
|
|
Email: strings.TrimSpace(ctx.String("email")),
|
|
|
|
Password: strings.TrimSpace(ctx.String("password")),
|
|
|
|
}
|
|
|
|
|
|
|
|
interactive := true
|
|
|
|
|
|
|
|
if uc.UserName != "" && uc.Password != "" {
|
|
|
|
log.Debugf("creating user in non-interactive mode")
|
|
|
|
interactive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && uc.FullName == "" {
|
2021-08-20 15:45:21 +02:00
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Full Name",
|
|
|
|
}
|
|
|
|
res, err := prompt.Run()
|
2021-08-18 20:19:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 15:45:21 +02:00
|
|
|
uc.FullName = strings.TrimSpace(res)
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && uc.UserName == "" {
|
2021-08-20 15:45:21 +02:00
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Username",
|
|
|
|
}
|
|
|
|
res, err := prompt.Run()
|
2021-08-18 20:19:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 15:45:21 +02:00
|
|
|
uc.UserName = strings.TrimSpace(res)
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && uc.Email == "" {
|
2021-08-20 15:45:21 +02:00
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "E-Mail",
|
|
|
|
}
|
|
|
|
res, err := prompt.Run()
|
2021-08-18 20:19:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 15:45:21 +02:00
|
|
|
uc.Email = strings.TrimSpace(res)
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && len(ctx.String("password")) < 4 {
|
2021-08-20 15:45:21 +02:00
|
|
|
validate := func(input string) error {
|
|
|
|
if len(input) < 4 {
|
|
|
|
return errors.New("password must have min. 4 characters")
|
|
|
|
}
|
|
|
|
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("password does not match")
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
2021-08-20 15:45:21 +02:00
|
|
|
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("passwords did not match or too short. please try again")
|
|
|
|
} else {
|
|
|
|
uc.Password = resPasswd
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := entity.CreateWithPassword(uc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
func usersDeleteAction(ctx *cli.Context) error {
|
|
|
|
return callWithDependencies(ctx, func(conf *config.Config) error {
|
|
|
|
userName := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if userName == "" {
|
|
|
|
return errors.New("please provide a username")
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
|
|
|
actionPrompt := promptui.Prompt{
|
|
|
|
Label: fmt.Sprintf("Delete %s?", txt.Quote(userName)),
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := actionPrompt.Run(); err == nil {
|
|
|
|
if m := entity.FindUserByName(userName); m == nil {
|
|
|
|
return errors.New("user not found")
|
|
|
|
} else if err := m.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
log.Infof("%s deleted", txt.Quote(userName))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("keeping user")
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
func usersListAction(ctx *cli.Context) error {
|
|
|
|
return callWithDependencies(ctx, func(conf *config.Config) error {
|
|
|
|
users := query.RegisteredUsers()
|
2021-10-01 00:05:49 +02:00
|
|
|
log.Infof("found %s", english.Plural(len(users), "user", "users"))
|
2021-08-20 00:10:26 +02:00
|
|
|
|
|
|
|
fmt.Printf("%-4s %-16s %-16s %-16s\n", "ID", "LOGIN", "NAME", "EMAIL")
|
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
for _, user := range users {
|
2021-11-12 09:10:15 +01:00
|
|
|
fmt.Printf("%-4d %-16s %-16s %-16s", user.ID, user.Username(), user.FullName, user.PrimaryEmail)
|
2021-08-18 20:19:14 +02:00
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-23 12:28:07 +02:00
|
|
|
func usersUpdateAction(ctx *cli.Context) error {
|
2021-08-20 00:10:26 +02:00
|
|
|
return callWithDependencies(ctx, func(conf *config.Config) error {
|
2021-08-18 20:19:14 +02:00
|
|
|
username := ctx.Args().First()
|
|
|
|
if username == "" {
|
|
|
|
return errors.New("pass username as argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
u := entity.FindUserByName(username)
|
|
|
|
if u == nil {
|
|
|
|
return errors.New("user not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
uc := form.UserCreate{
|
|
|
|
FullName: strings.TrimSpace(ctx.String("fullname")),
|
2021-08-19 15:53:23 +02:00
|
|
|
Email: strings.TrimSpace(ctx.String("email")),
|
2021-08-18 20:19:14 +02:00
|
|
|
Password: strings.TrimSpace(ctx.String("password")),
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("password") {
|
|
|
|
err := u.SetPassword(uc.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-12 09:10:15 +01:00
|
|
|
fmt.Printf("password successfully changed: %v\n", u.Username())
|
2021-08-18 20:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("fullname") {
|
|
|
|
u.FullName = uc.FullName
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:53:23 +02:00
|
|
|
if ctx.IsSet("email") && len(uc.Email) > 0 {
|
2021-08-18 20:19:14 +02:00
|
|
|
u.PrimaryEmail = uc.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := u.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
if err := u.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-11-12 09:10:15 +01:00
|
|
|
fmt.Printf("user successfully updated: %v\n", u.Username())
|
2021-08-18 20:19:14 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
func callWithDependencies(ctx *cli.Context, f func(conf *config.Config) error) error {
|
2021-08-18 20:19:14 +02:00
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
defer conf.Shutdown()
|
|
|
|
|
2021-08-20 00:10:26 +02:00
|
|
|
// Run command.
|
2021-08-18 20:19:14 +02:00
|
|
|
if err := f(conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 00:10:26 +02:00
|
|
|
|
2021-08-18 20:19:14 +02:00
|
|
|
return nil
|
|
|
|
}
|