2022-09-28 09:01:17 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2022-09-30 00:42:19 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetValuesFromCli updates the entity values from a CLI context and validates them.
|
|
|
|
func (m *User) SetValuesFromCli(ctx *cli.Context) error {
|
|
|
|
frm := form.NewUserFromCli(ctx)
|
|
|
|
|
|
|
|
// Email address.
|
|
|
|
if ctx.IsSet("email") {
|
|
|
|
m.UserEmail = frm.Email()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display name.
|
2022-09-30 00:42:19 +02:00
|
|
|
if ctx.IsSet("name") {
|
|
|
|
m.DisplayName = clean.Name(frm.DisplayName)
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// User role.
|
|
|
|
if ctx.IsSet("role") {
|
|
|
|
m.UserRole = frm.Role()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Super-admin status.
|
|
|
|
if ctx.IsSet("superadmin") {
|
|
|
|
m.SuperAdmin = frm.SuperAdmin
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Disable login (Web UI)?
|
2022-09-30 00:42:19 +02:00
|
|
|
if ctx.IsSet("no-login") {
|
2022-09-28 09:01:17 +02:00
|
|
|
m.CanLogin = frm.CanLogin
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Allow the use of WebDAV?
|
|
|
|
if ctx.IsSet("webdav") {
|
|
|
|
m.WebDAV = frm.WebDAV
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Set custom attributes?
|
2022-09-30 00:42:19 +02:00
|
|
|
if ctx.IsSet("attr") {
|
|
|
|
m.UserAttr = frm.Attr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Originals base folder.
|
|
|
|
if ctx.IsSet("base-path") {
|
|
|
|
m.SetBasePath(frm.BasePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub-folder for uploads.
|
|
|
|
if ctx.IsSet("upload-path") {
|
|
|
|
m.SetUploadPath(frm.UploadPath)
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return m.Validate()
|
|
|
|
}
|
2023-02-20 20:24:04 +01:00
|
|
|
|
|
|
|
// RestoreFromCli restored the account from a CLI context.
|
|
|
|
func (m *User) RestoreFromCli(ctx *cli.Context, newPassword string) (err error) {
|
|
|
|
m.DeletedAt = nil
|
|
|
|
|
|
|
|
// Set values.
|
|
|
|
if err = m.SetValuesFromCli(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save values.
|
|
|
|
if err = m.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if newPassword == "" {
|
|
|
|
return nil
|
|
|
|
} else if err = m.SetPassword(newPassword); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|