filebrowser/backend/cmd/users_add.go

47 lines
1.1 KiB
Go
Raw Normal View History

2023-09-02 17:24:55 +00:00
package cmd
import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/users"
)
func init() {
usersCmd.AddCommand(usersAddCmd)
}
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), //nolint:gomnd
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
2023-10-10 14:57:06 +00:00
<<<<<<< HEAD
2023-09-02 17:24:55 +00:00
password, err := users.HashPwd(args[1])
checkErr(err)
2023-10-10 14:57:06 +00:00
=======
>>>>>>> v0.2.1
2023-09-02 17:24:55 +00:00
user := &users.User{
Username: args[0],
Password: args[1],
2023-09-02 17:24:55 +00:00
LockPassword: mustGetBool(cmd.Flags(), "lockPassword"),
}
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 := s2.MakeUserDir(user.Username, user.Scope, servSettings.Root)
checkErr(err)
user.Scope = userHome
err = d.store.Users.Save(user)
checkErr(err)
printUsers([]*users.User{user})
}, pythonConfig{}),
}