diff options
author | Sandro Santilli <strk@kbt.io> | 2017-03-20 09:23:38 +0100 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-03-20 09:23:38 +0100 |
commit | e1586898b2f0905b6f59383c01a3aed6b39de93a (patch) | |
tree | 7fac20413a9711172c61c64836e518da4da008d4 /cmd/admin.go | |
parent | 97ee88975a43b8d69bd2abfcce0af10fadc172fa (diff) | |
download | gitea-e1586898b2f0905b6f59383c01a3aed6b39de93a.tar.gz gitea-e1586898b2f0905b6f59383c01a3aed6b39de93a.zip |
Add change-password admin command (#1304)
* Add change-password admin command
Diffstat (limited to 'cmd/admin.go')
-rw-r--r-- | cmd/admin.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/cmd/admin.go b/cmd/admin.go index 4b4ab956a6..d4240e17c0 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -23,6 +23,7 @@ var ( to make automatic initialization process more smoothly`, Subcommands: []cli.Command{ subcmdCreateUser, + subcmdChangePassword, }, } @@ -57,8 +58,59 @@ to make automatic initialization process more smoothly`, }, }, } + + subcmdChangePassword = cli.Command{ + Name: "change-password", + Usage: "Change a user's password", + Action: runChangePassword, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "username,u", + Value: "", + Usage: "The user to change password for", + }, + cli.StringFlag{ + Name: "password,p", + Value: "", + Usage: "New password to set for user", + }, + }, + } ) +func runChangePassword(c *cli.Context) error { + if !c.IsSet("password") { + return fmt.Errorf("Password is not specified") + } else if !c.IsSet("username") { + return fmt.Errorf("Username is not specified") + } + + setting.NewContext() + models.LoadConfigs() + + setting.NewXORMLogService(false) + if err := models.SetEngine(); err != nil { + return fmt.Errorf("models.SetEngine: %v", err) + } + + uname := c.String("username") + user, err := models.GetUserByName(uname) + if err != nil { + return fmt.Errorf("%v", err) + } + user.Passwd = c.String("password") + if user.Salt, err = models.GetUserSalt(); err != nil { + return fmt.Errorf("%v", err) + } + user.EncodePasswd() + if err := models.UpdateUser(user); err != nil { + return fmt.Errorf("%v", err) + } + + fmt.Printf("User '%s' password has been successfully updated!\n", uname) + return nil +} + func runCreateUser(c *cli.Context) error { if !c.IsSet("name") { return fmt.Errorf("Username is not specified") |