mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 16:57:14 -06:00
Chore: Add user service method ChangePassword (#53303)
* Chore: Add user service method ChangePassword * Fix lint
This commit is contained in:
parent
6fdb6ea6f6
commit
20f4191e56
@ -121,12 +121,12 @@ func (hs *HTTPServer) AdminUpdateUserPassword(c *models.ReqContext) response.Res
|
|||||||
return response.Error(500, "Could not encode password", err)
|
return response.Error(500, "Could not encode password", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := models.ChangeUserPasswordCommand{
|
cmd := user.ChangeUserPasswordCommand{
|
||||||
UserId: userID,
|
UserID: userID,
|
||||||
NewPassword: passwordHashed,
|
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)
|
return response.Error(500, "Failed to update user password", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,15 +71,15 @@ func (hs *HTTPServer) ResetPassword(c *models.ReqContext) response.Response {
|
|||||||
return response.Error(400, "New password is too short", nil)
|
return response.Error(400, "New password is too short", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := models.ChangeUserPasswordCommand{}
|
cmd := user.ChangeUserPasswordCommand{}
|
||||||
cmd.UserId = query.Result.ID
|
cmd.UserID = query.Result.ID
|
||||||
var err error
|
var err error
|
||||||
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, query.Result.Salt)
|
cmd.NewPassword, err = util.EncodePassword(form.NewPassword, query.Result.Salt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return response.Error(500, "Failed to encode password", err)
|
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)
|
return response.Error(500, "Failed to change user password", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,7 +379,7 @@ func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *models.ReqContext) {
|
|||||||
// 403: forbiddenError
|
// 403: forbiddenError
|
||||||
// 500: internalServerError
|
// 500: internalServerError
|
||||||
func (hs *HTTPServer) ChangeUserPassword(c *models.ReqContext) response.Response {
|
func (hs *HTTPServer) ChangeUserPassword(c *models.ReqContext) response.Response {
|
||||||
cmd := models.ChangeUserPasswordCommand{}
|
cmd := user.ChangeUserPasswordCommand{}
|
||||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
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)
|
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)
|
cmd.NewPassword, err = util.EncodePassword(cmd.NewPassword, user.Salt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return response.Error(500, "Failed to encode password", err)
|
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)
|
return response.Error(500, "Failed to change user password", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +52,12 @@ func resetPasswordCommand(c utils.CommandLine, runner runner.Runner) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := models.ChangeUserPasswordCommand{
|
cmd := user.ChangeUserPasswordCommand{
|
||||||
UserId: AdminUserId,
|
UserID: AdminUserId,
|
||||||
NewPassword: passwordHashed,
|
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)
|
return fmt.Errorf("failed to update user password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,10 +137,6 @@ func (m *SQLStoreMock) CreateUser(ctx context.Context, cmd user.CreateUserComman
|
|||||||
return nil, m.ExpectedError
|
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 {
|
func (m *SQLStoreMock) UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error {
|
||||||
return m.ExpectedError
|
return m.ExpectedError
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,6 @@ type Store interface {
|
|||||||
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
|
GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error
|
||||||
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
|
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
|
||||||
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, 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
|
UpdateUserLastSeenAt(ctx context.Context, cmd *models.UpdateUserLastSeenAtCommand) error
|
||||||
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
|
SetUsingOrg(ctx context.Context, cmd *models.SetUsingOrgCommand) error
|
||||||
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
|
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
|
||||||
|
@ -75,6 +75,13 @@ type UpdateUserCommand struct {
|
|||||||
UserID int64 `json:"-"`
|
UserID int64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChangeUserPasswordCommand struct {
|
||||||
|
OldPassword string `json:"oldPassword"`
|
||||||
|
NewPassword string `json:"newPassword"`
|
||||||
|
|
||||||
|
UserID int64 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) NameOrFallback() string {
|
func (u *User) NameOrFallback() string {
|
||||||
if u.Name != "" {
|
if u.Name != "" {
|
||||||
return u.Name
|
return u.Name
|
||||||
|
@ -11,4 +11,5 @@ type Service interface {
|
|||||||
GetByLogin(context.Context, *GetUserByLoginQuery) (*User, error)
|
GetByLogin(context.Context, *GetUserByLoginQuery) (*User, error)
|
||||||
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
|
GetByEmail(context.Context, *GetUserByEmailQuery) (*User, error)
|
||||||
Update(context.Context, *UpdateUserCommand) error
|
Update(context.Context, *UpdateUserCommand) error
|
||||||
|
ChangePassword(context.Context, *ChangeUserPasswordCommand) error
|
||||||
}
|
}
|
||||||
|
@ -279,3 +279,13 @@ func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error
|
|||||||
}
|
}
|
||||||
return s.sqlStore.UpdateUser(ctx, q)
|
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)
|
||||||
|
}
|
||||||
|
@ -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 {
|
func (f *FakeUserService) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
|
||||||
return f.ExpectedError
|
return f.ExpectedError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FakeUserService) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
|
||||||
|
return f.ExpectedError
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user