filebrowser/cmd/users_update.go
1138-4EB 9a54abfe62 chore: style and gofmt (#622)
Former-commit-id: 669963fd1d51d7e2d32520ffb0c1ecd5e4224c78 [formerly 8ef320b07699c842ae0325e4665d570fa4399481] [formerly d171df23332dff1d33f00a7407f5dc3f6dde1ef7 [formerly d0fd97d943]]
Former-commit-id: dc9a921b032e1acee0845186e7a009badbe768c5 [formerly 0c8a0f739bd1d984ca0db2b0623da5fdb813f4e0]
Former-commit-id: 3005cd15261952387fde1adf13770e2c1ee52749
2019-01-08 19:40:14 +01:00

73 lines
1.7 KiB
Go

package cmd
import (
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra"
)
func init() {
usersCmd.AddCommand(usersUpdateCmd)
usersUpdateCmd.Flags().StringP("password", "p", "", "new password")
usersUpdateCmd.Flags().StringP("username", "u", "", "new username")
addUserFlags(usersUpdateCmd.Flags())
}
var usersUpdateCmd = &cobra.Command{
Use: "update <id|username>",
Short: "Updates an existing user",
Long: `Updates an existing user. Set the flags for the
options you want to change.`,
Args: cobra.ExactArgs(1),
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
username, id := parseUsernameOrID(args[0])
flags := cmd.Flags()
password := mustGetString(flags, "password")
newUsername := mustGetString(flags, "username")
var (
err error
user *users.User
)
if id != 0 {
user, err = d.store.Users.Get("", id)
} else {
user, err = d.store.Users.Get("", username)
}
checkErr(err)
defaults := settings.UserDefaults{
Scope: user.Scope,
Locale: user.Locale,
ViewMode: user.ViewMode,
Perm: user.Perm,
Sorting: user.Sorting,
Commands: user.Commands,
}
getUserDefaults(flags, &defaults, false)
user.Scope = defaults.Scope
user.Locale = defaults.Locale
user.ViewMode = defaults.ViewMode
user.Perm = defaults.Perm
user.Commands = defaults.Commands
user.Sorting = defaults.Sorting
user.LockPassword = mustGetBool(flags, "lockPassword")
if newUsername != "" {
user.Username = newUsername
}
if password != "" {
user.Password, err = users.HashPwd(password)
checkErr(err)
}
err = d.store.Users.Update(user)
checkErr(err)
printUsers([]*users.User{user})
}, pythonConfig{}),
}