Chore: Add user service method ChangePassword (#53303)

* Chore: Add user service method ChangePassword

* Fix lint
This commit is contained in:
idafurjes 2022-08-04 15:05:05 +02:00 committed by GitHub
parent 6fdb6ea6f6
commit 20f4191e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 17 deletions

View File

@ -121,12 +121,12 @@ func (hs *HTTPServer) AdminUpdateUserPassword(c *models.ReqContext) response.Res
return response.Error(500, "Could not encode password", err)
}
cmd := models.ChangeUserPasswordCommand{
UserId: userID,
cmd := user.ChangeUserPasswordCommand{
UserID: userID,
NewPassword: passwordHashed,
}
if err := hs.SQLStore.ChangeUserPassword(c.Req.Context(), &cmd); err != nil {
if err := hs.userService.ChangePassword(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to update user password", err)
}

View File

@ -71,15 +71,15 @@ func (hs *HTTPServer) ResetPassword(c *models.ReqContext) response.Response {
return response.Error(400, "New password is too short", nil)
}
cmd := models.ChangeUserPasswordCommand{}
cmd.UserId = query.Result.ID
cmd := user.ChangeUserPasswordCommand{}
cmd.UserID = query.Result.ID
var err error
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, query.Result.Salt)
if err != nil {
return response.Error(500, "Failed to encode password", err)
}
if err := hs.SQLStore.ChangeUserPassword(c.Req.Context(), &cmd); err != nil {
if err := hs.userService.ChangePassword(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to change user password", err)
}

View File

@ -379,7 +379,7 @@ func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *models.ReqContext) {
// 403: forbiddenError
// 500: internalServerError
func (hs *HTTPServer) ChangeUserPassword(c *models.ReqContext) response.Response {
cmd := models.ChangeUserPasswordCommand{}
cmd := user.ChangeUserPasswordCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
@ -407,13 +407,13 @@ func (hs *HTTPServer) ChangeUserPassword(c *models.ReqContext) response.Response
return response.Error(400, "New password is too short", nil)
}
cmd.UserId = c.UserId
cmd.UserID = c.UserId
cmd.NewPassword, err = util.EncodePassword(cmd.NewPassword, user.Salt)
if err != nil {
return response.Error(500, "Failed to encode password", err)
}
if err := hs.SQLStore.ChangeUserPassword(c.Req.Context(), &cmd); err != nil {
if err := hs.userService.ChangePassword(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to change user password", err)
}

View File

@ -52,12 +52,12 @@ func resetPasswordCommand(c utils.CommandLine, runner runner.Runner) error {
return err
}
cmd := models.ChangeUserPasswordCommand{
UserId: AdminUserId,
cmd := user.ChangeUserPasswordCommand{
UserID: AdminUserId,
NewPassword: passwordHashed,
}
if err := runner.SQLStore.ChangeUserPassword(context.Background(), &cmd); err != nil {
if err := runner.UserService.ChangePassword(context.Background(), &cmd); err != nil {
return fmt.Errorf("failed to update user password: %w", err)
}

View File

@ -137,10 +137,6 @@ func (m *SQLStoreMock) CreateUser(ctx context.Context, cmd user.CreateUserComman
return nil, m.ExpectedError
}
func (m *SQLStoreMock) ChangeUserPassword(ctx context.Context, cmd *models.ChangeUserPasswordCommand) error {
return m.ExpectedError
}
func (m *SQLStoreMock) UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error {
return m.ExpectedError
}

View File

@ -31,7 +31,6 @@ type Store interface {
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error)
ChangeUserPassword(ctx context.Context, cmd *models.ChangeUserPasswordCommand) error
UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error

View File

@ -75,6 +75,13 @@ type UpdateUserCommand struct {
UserID int64 `json:"-"`
}
type ChangeUserPasswordCommand struct {
OldPassword string `json:"oldPassword"`
NewPassword string `json:"newPassword"`
UserID int64 `json:"-"`
}
func (u *User) NameOrFallback() string {
if u.Name != "" {
return u.Name

View File

@ -11,4 +11,5 @@ type Service interface {
GetByLogin(context.Context, *GetUserByLoginQuery) (*User, error)
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
Update(context.Context, *UpdateUserCommand) error
ChangePassword(context.Context, *ChangeUserPasswordCommand) error
}

View File

@ -279,3 +279,13 @@ func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error
}
return s.sqlStore.UpdateUser(ctx, q)
}
// TODO: remove wrapper around sqlstore
func (s *Service) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
q := &models.ChangeUserPasswordCommand{
UserId: cmd.UserID,
NewPassword: cmd.NewPassword,
OldPassword: cmd.OldPassword,
}
return s.sqlStore.ChangeUserPassword(ctx, q)
}

View File

@ -38,3 +38,7 @@ func (f *FakeUserService) GetByEmail(ctx context.Context, query *user.GetUserByE
func (f *FakeUserService) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
return f.ExpectedError
}
func (f *FakeUserService) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
return f.ExpectedError
}