support modify user password by cli
This commit is contained in:
@@ -27,6 +27,23 @@ var UserData = &cli.Command{
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "user-modify-password",
|
||||
Usage: "Modify user password",
|
||||
Action: modifyUserPassword,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "username",
|
||||
Aliases: []string{"n"},
|
||||
Usage: "Specific user name",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "password",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "User new password",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "user-delete",
|
||||
Usage: "Delete specified user",
|
||||
@@ -91,6 +108,27 @@ func getUserInfo(c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func modifyUserPassword(c *cli.Context) error {
|
||||
_, err := initializeSystem(c)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userName := c.String("username")
|
||||
password := c.String("password")
|
||||
err = clis.UserData.ModifyUserPassword(c, userName, password)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.modifyUserPassword] error occurs when modifying user password")
|
||||
return err
|
||||
}
|
||||
|
||||
log.BootInfof("[user_data.modifyUserPassword] password of user \"%s\" has been changed", userName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteUser(c *cli.Context) error {
|
||||
_, err := initializeSystem(c)
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ type UserDataCli struct {
|
||||
categories *services.TransactionCategoryService
|
||||
tags *services.TransactionTagService
|
||||
users *services.UserService
|
||||
tokens *services.TokenService
|
||||
}
|
||||
|
||||
// Initialize an user data cli singleton instance
|
||||
@@ -34,6 +35,7 @@ var (
|
||||
users: services.Users,
|
||||
categories: services.TransactionCategories,
|
||||
tags: services.TransactionTags,
|
||||
tokens: services.Tokens,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -54,6 +56,54 @@ func (a *UserDataCli) GetUserByUsername(c *cli.Context, username string) (*model
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// ModifyUserPassword modifies user password
|
||||
func (a *UserDataCli) ModifyUserPassword(c *cli.Context, username string, password string) error {
|
||||
if username == "" {
|
||||
log.BootErrorf("[user_data.ModifyUserPassword] user name is empty")
|
||||
return errs.ErrUsernameIsEmpty
|
||||
}
|
||||
|
||||
if password == "" {
|
||||
log.BootErrorf("[user_data.ModifyUserPassword] user password is empty")
|
||||
return errs.ErrPasswordIsEmpty
|
||||
}
|
||||
|
||||
user, err := a.users.GetUserByUsername(username)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.ModifyUserPassword] failed to get user by user name \"%s\", because %s", username, err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if a.users.IsPasswordEqualsUserPassword(password, user) {
|
||||
return errs.ErrNothingWillBeUpdated
|
||||
}
|
||||
|
||||
userNew := &models.User{
|
||||
Uid: user.Uid,
|
||||
Salt: user.Salt,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
_, err = a.users.UpdateUser(userNew)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
err = a.tokens.DeleteTokensBeforeTime(user.Uid, now)
|
||||
|
||||
if err == nil {
|
||||
log.BootInfof("[user_data.ModifyUserPassword] revoke old tokens before unix time \"%d\" for user \"%s\"", now, user.Username)
|
||||
} else {
|
||||
log.BootWarnf("[user_data.ModifyUserPassword] failed to revoke old tokens for user \"uid:%d\", because %s", user.Uid, err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUser deletes user according to the specified user name
|
||||
func (a *UserDataCli) DeleteUser(c *cli.Context, username string) error {
|
||||
if username == "" {
|
||||
|
||||
Reference in New Issue
Block a user