|
23 | 23 | to make automatic initialization process more smoothly`,
|
24 | 24 | Subcommands: []cli.Command{
|
25 | 25 | subcmdCreateUser,
|
| 26 | + subcmdChangePassword, |
26 | 27 | },
|
27 | 28 | }
|
28 | 29 |
|
@@ -57,8 +58,59 @@ to make automatic initialization process more smoothly`,
|
57 | 58 | },
|
58 | 59 | },
|
59 | 60 | }
|
| 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 | + } |
60 | 79 | )
|
61 | 80 |
|
| 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 | + |
62 | 114 | func runCreateUser(c *cli.Context) error {
|
63 | 115 | if !c.IsSet("name") {
|
64 | 116 | return fmt.Errorf("Username is not specified")
|
|
0 commit comments