2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-01-06 13:21:31 +00:00
|
|
|
usersCmd.AddCommand(usersAddCmd)
|
2019-01-08 10:50:37 +00:00
|
|
|
addUserFlags(usersAddCmd.Flags())
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
var usersAddCmd = &cobra.Command{
|
|
|
|
Use: "add <username> <password>",
|
2019-01-05 22:44:33 +00:00
|
|
|
Short: "Create a new user",
|
|
|
|
Long: `Create a new user and add it to the database.`,
|
2019-01-06 13:21:31 +00:00
|
|
|
Args: cobra.ExactArgs(2),
|
2019-01-07 20:24:23 +00:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
|
|
|
s, err := d.store.Settings.Get()
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
getUserDefaults(cmd, &s.Defaults, false)
|
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
password, err := users.HashPwd(args[1])
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
user := &users.User{
|
2019-01-06 13:21:31 +00:00
|
|
|
Username: args[0],
|
2019-01-05 22:44:33 +00:00
|
|
|
Password: password,
|
|
|
|
LockPassword: mustGetBool(cmd, "lockPassword"),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Defaults.Apply(user)
|
2019-01-07 20:24:23 +00:00
|
|
|
err = d.store.Users.Save(user)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
printUsers([]*users.User{user})
|
2019-01-07 20:24:23 +00:00
|
|
|
}, pythonConfig{}),
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|