photoprism/internal/commands/users_mod.go
Michael Mayer 929e5ebe50 CLI: Migrate database before adding or deleting users
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-10-07 19:30:30 +02:00

66 lines
1.4 KiB
Go

package commands
import (
"fmt"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/pkg/clean"
)
// UsersModCommand configures the command name, flags, and action.
var UsersModCommand = cli.Command{
Name: "mod",
Usage: "Modifies an existing user account",
ArgsUsage: "[username]",
Flags: UserFlags,
Action: usersModAction,
}
// usersModAction modifies an existing user account.
func usersModAction(ctx *cli.Context) error {
return CallWithDependencies(ctx, func(conf *config.Config) error {
conf.MigrateDb(false, nil)
username := clean.Username(ctx.Args().First())
// Username provided?
if username == "" {
return cli.ShowSubcommandHelp(ctx)
}
// Find user by name.
user := entity.FindUserByName(username)
if user == nil {
return fmt.Errorf("user %s not found", clean.LogQuote(username))
}
// Set values.
if err := user.SetValuesFromCli(ctx); err != nil {
return err
}
// Change password?
if val := clean.Password(ctx.String("password")); ctx.IsSet("password") && val != "" {
err := user.SetPassword(val)
if err != nil {
return err
}
log.Warnf("password has been changed")
}
// Save values.
if err := user.Save(); err != nil {
return err
}
log.Infof("user %s has been updated", clean.LogQuote(user.Name()))
return nil
})
}