User: use update function for password updates (#86419)
* Update password through Update function instead * Remove duplicated to lower * Refactor password code
This commit is contained in:
@@ -134,9 +134,9 @@ func (s *Service) Create(ctx context.Context, cmd *user.CreateUserCommand) (*use
|
||||
// create user
|
||||
usr := &user.User{
|
||||
UID: cmd.UID,
|
||||
Email: cmd.Email,
|
||||
Email: strings.ToLower(cmd.Email),
|
||||
Name: cmd.Name,
|
||||
Login: cmd.Login,
|
||||
Login: strings.ToLower(cmd.Login),
|
||||
Company: cmd.Company,
|
||||
IsAdmin: cmd.IsAdmin,
|
||||
IsDisabled: cmd.IsDisabled,
|
||||
@@ -160,11 +160,14 @@ func (s *Service) Create(ctx context.Context, cmd *user.CreateUserCommand) (*use
|
||||
usr.Rands = rands
|
||||
|
||||
if len(cmd.Password) > 0 {
|
||||
encodedPassword, err := util.EncodePassword(string(cmd.Password), usr.Salt)
|
||||
if err := cmd.Password.Validate(s.cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
usr.Password, err = cmd.Password.Hash(usr.Salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
usr.Password = user.Password(encodedPassword)
|
||||
}
|
||||
|
||||
_, err = s.store.Insert(ctx, usr)
|
||||
@@ -220,16 +223,37 @@ func (s *Service) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuer
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
|
||||
cmd.Login = strings.ToLower(cmd.Login)
|
||||
cmd.Email = strings.ToLower(cmd.Email)
|
||||
usr, err := s.store.GetByID(ctx, cmd.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cmd.OldPassword != nil {
|
||||
old, err := cmd.OldPassword.Hash(usr.Salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if old != usr.Password {
|
||||
return user.ErrPasswordMissmatch.Errorf("old password does not match stored password")
|
||||
}
|
||||
}
|
||||
|
||||
if cmd.Password != nil {
|
||||
if err := cmd.Password.Validate(s.cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hashed, err := cmd.Password.Hash(usr.Salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Password = &hashed
|
||||
}
|
||||
|
||||
return s.store.Update(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s *Service) ChangePassword(ctx context.Context, cmd *user.ChangeUserPasswordCommand) error {
|
||||
return s.store.ChangePassword(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateLastSeenAt(ctx context.Context, cmd *user.UpdateUserLastSeenAtCommand) error {
|
||||
u, err := s.GetSignedInUserWithCacheCtx(ctx, &user.GetSignedInUserQuery{
|
||||
UserID: cmd.UserID,
|
||||
|
||||
Reference in New Issue
Block a user