photoprism/internal/commands/users_mod.go
Michael Mayer f5a8c5a45d Auth: Session and ACL enhancements #98 #1746
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-28 09:01:17 +02:00

64 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 {
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
})
}