feat(cli): adds command to reset admin password

closes #5479
This commit is contained in:
bergquist 2016-12-09 15:25:02 +01:00
parent cd85e1f651
commit 96e8ecfa7b
5 changed files with 23 additions and 10 deletions

View File

@ -73,6 +73,10 @@ func main() {
case "setup":
setup()
case "build-cli":
clean()
build("grafana-cli", "./pkg/cmd/grafana-cli", []string{})
case "build":
clean()
for _, binary := range binaries {

View File

@ -157,8 +157,9 @@ func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand)
return ApiError(401, "Invalid old password", nil)
}
if len(cmd.NewPassword) < 4 {
return ApiError(400, "New password too short", nil)
password := m.Password(cmd.NewPassword)
if password.IsWeak() {
return ApiError(400, "New password is too short", nil)
}
cmd.UserId = c.UserId

View File

@ -90,10 +90,10 @@ var pluginCommands = []cli.Command{
},
}
var userCommands = []cli.Command{
var adminCommands = []cli.Command{
{
Name: "reset-admin",
Usage: "reset-admin <new password>",
Name: "reset-admin-password",
Usage: "reset-admin-password <new password>",
Action: runDbCommand(resetPasswordCommand),
},
}
@ -105,8 +105,8 @@ var Commands = []cli.Command{
Subcommands: pluginCommands,
},
{
Name: "user",
Usage: "",
Subcommands: userCommands,
Name: "admin",
Usage: "Grafana admin commands",
Subcommands: adminCommands,
},
}

View File

@ -15,8 +15,9 @@ const AdminUserId = 1
func resetPasswordCommand(c CommandLine) error {
newPassword := c.Args().First()
if len(newPassword) < 4 {
return fmt.Errorf("New password too short")
password := models.Password(newPassword)
if password.IsWeak() {
return fmt.Errorf("New password is too short")
}
userQuery := models.GetUserByIdQuery{Id: AdminUserId}
@ -36,6 +37,7 @@ func resetPasswordCommand(c CommandLine) error {
return fmt.Errorf("Failed to update user password")
}
logger.Infof("\n")
logger.Infof("Admin password changed successfully %s", color.GreenString("✔"))
return nil

View File

@ -10,6 +10,12 @@ var (
ErrUserNotFound = errors.New("User not found")
)
type Password string
func (p Password) IsWeak() bool {
return len(p) <= 4
}
type User struct {
Id int64
Version int