Skip to content

Commit e158689

Browse files
strkbkcsoft
authored andcommitted
Add change-password admin command (#1304)
* Add change-password admin command
1 parent 97ee889 commit e158689

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

cmd/admin.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
to make automatic initialization process more smoothly`,
2424
Subcommands: []cli.Command{
2525
subcmdCreateUser,
26+
subcmdChangePassword,
2627
},
2728
}
2829

@@ -57,8 +58,59 @@ to make automatic initialization process more smoothly`,
5758
},
5859
},
5960
}
61+
62+
subcmdChangePassword = cli.Command{
63+
Name: "change-password",
64+
Usage: "Change a user's password",
65+
Action: runChangePassword,
66+
Flags: []cli.Flag{
67+
cli.StringFlag{
68+
Name: "username,u",
69+
Value: "",
70+
Usage: "The user to change password for",
71+
},
72+
cli.StringFlag{
73+
Name: "password,p",
74+
Value: "",
75+
Usage: "New password to set for user",
76+
},
77+
},
78+
}
6079
)
6180

81+
func runChangePassword(c *cli.Context) error {
82+
if !c.IsSet("password") {
83+
return fmt.Errorf("Password is not specified")
84+
} else if !c.IsSet("username") {
85+
return fmt.Errorf("Username is not specified")
86+
}
87+
88+
setting.NewContext()
89+
models.LoadConfigs()
90+
91+
setting.NewXORMLogService(false)
92+
if err := models.SetEngine(); err != nil {
93+
return fmt.Errorf("models.SetEngine: %v", err)
94+
}
95+
96+
uname := c.String("username")
97+
user, err := models.GetUserByName(uname)
98+
if err != nil {
99+
return fmt.Errorf("%v", err)
100+
}
101+
user.Passwd = c.String("password")
102+
if user.Salt, err = models.GetUserSalt(); err != nil {
103+
return fmt.Errorf("%v", err)
104+
}
105+
user.EncodePasswd()
106+
if err := models.UpdateUser(user); err != nil {
107+
return fmt.Errorf("%v", err)
108+
}
109+
110+
fmt.Printf("User '%s' password has been successfully updated!\n", uname)
111+
return nil
112+
}
113+
62114
func runCreateUser(c *cli.Context) error {
63115
if !c.IsSet("name") {
64116
return fmt.Errorf("Username is not specified")

0 commit comments

Comments
 (0)