filebrowser/cmd/users_add.go
荒野無燈 65a53514d5 global settings: add createUserDir option. new feature: auto create user home dir while adding user.
Former-commit-id: 331a76abdc611236ccc761d0fd9a814ed1ee0c35 [formerly 0c1024a5b8109c84d213e0cbdbe05e10eb5793d4] [formerly 467e1789f55c410ff2ca9e9ef125d9fe28410bc9 [formerly e8570e4dba]]
Former-commit-id: 1eed58870b6e009d84806db6b55efc5fc3983e2a [formerly 3e9083f7758e72bd307ed23c4b512a8ab5adc523]
Former-commit-id: 5023ef77eb92636e62fde511ea609114e667a7d7
2019-02-19 11:55:18 +08:00

52 lines
1.3 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(usersAddCmd)
addUserFlags(usersAddCmd.Flags())
}
var usersAddCmd = &cobra.Command{
Use: "add <username> <password>",
Short: "Create a new user",
Long: `Create a new user and add it to the database.`,
Args: cobra.ExactArgs(2),
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
s, err := d.store.Settings.Get()
checkErr(err)
getUserDefaults(cmd.Flags(), &s.Defaults, false)
password, err := users.HashPwd(args[1])
checkErr(err)
user := &users.User{
Username: args[0],
Password: password,
LockPassword: mustGetBool(cmd.Flags(), "lockPassword"),
}
s.Defaults.Apply(user)
servSettings, err := d.store.Settings.GetServer()
checkErr(err)
//since getUserDefaults() polluted s.Defaults.Scope
//which makes the Scope not the one saved in the db
//we need the right s.Defaults.Scope here
s2, err := d.store.Settings.Get()
checkErr(err)
userHome, err := settings.CreateUserDir(user.Username, user.Scope, servSettings.Root, s2)
checkErr(err)
user.Scope = userHome
err = d.store.Users.Save(user)
checkErr(err)
printUsers([]*users.User{user})
}, pythonConfig{}),
}