2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
2020-06-01 07:12:36 +08:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
2019-01-06 06:44:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-01-06 21:21:31 +08:00
|
|
|
usersCmd.AddCommand(usersAddCmd)
|
2019-01-08 18:50:37 +08:00
|
|
|
addUserFlags(usersAddCmd.Flags())
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-06 21:21:31 +08:00
|
|
|
var usersAddCmd = &cobra.Command{
|
|
|
|
Use: "add <username> <password>",
|
2019-01-06 06:44:33 +08:00
|
|
|
Short: "Create a new user",
|
|
|
|
Long: `Create a new user and add it to the database.`,
|
2021-03-04 07:10:08 +08:00
|
|
|
Args: cobra.ExactArgs(2), //nolint:gomnd
|
2019-01-08 04:24:23 +08:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
|
|
|
s, err := d.store.Settings.Get()
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
2019-01-08 22:07:55 +08:00
|
|
|
getUserDefaults(cmd.Flags(), &s.Defaults, false)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-06 21:21:31 +08:00
|
|
|
password, err := users.HashPwd(args[1])
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
user := &users.User{
|
2019-01-06 21:21:31 +08:00
|
|
|
Username: args[0],
|
2019-01-06 06:44:33 +08:00
|
|
|
Password: password,
|
2019-01-08 22:07:55 +08:00
|
|
|
LockPassword: mustGetBool(cmd.Flags(), "lockPassword"),
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Defaults.Apply(user)
|
2019-02-15 23:12:02 +08:00
|
|
|
|
|
|
|
servSettings, err := d.store.Settings.GetServer()
|
|
|
|
checkErr(err)
|
2020-06-01 07:12:36 +08:00
|
|
|
// 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
|
2019-02-15 23:12:02 +08:00
|
|
|
s2, err := d.store.Settings.Get()
|
|
|
|
checkErr(err)
|
|
|
|
|
2019-04-20 21:15:28 +08:00
|
|
|
userHome, err := s2.MakeUserDir(user.Username, user.Scope, servSettings.Root)
|
2019-02-15 23:12:02 +08:00
|
|
|
checkErr(err)
|
|
|
|
user.Scope = userHome
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
err = d.store.Users.Save(user)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
printUsers([]*users.User{user})
|
2019-01-08 04:24:23 +08:00
|
|
|
}, pythonConfig{}),
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|