2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-01-06 21:21:31 +08:00
|
|
|
usersCmd.AddCommand(usersAddCmd)
|
|
|
|
addUserFlags(usersAddCmd)
|
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.`,
|
2019-01-06 21:21:31 +08:00
|
|
|
Args: cobra.ExactArgs(2),
|
2019-01-06 06:44:33 +08:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
db := getDB()
|
|
|
|
defer db.Close()
|
|
|
|
st := getStorage(db)
|
|
|
|
|
|
|
|
s, err := st.Settings.Get()
|
|
|
|
checkErr(err)
|
|
|
|
getUserDefaults(cmd, &s.Defaults, false)
|
|
|
|
|
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,
|
|
|
|
LockPassword: mustGetBool(cmd, "lockPassword"),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Defaults.Apply(user)
|
|
|
|
err = st.Users.Save(user)
|
|
|
|
checkErr(err)
|
|
|
|
printUsers([]*users.User{user})
|
|
|
|
},
|
|
|
|
}
|