@@ -4,9 +4,12 @@ import (
44 "context"
55 "strings"
66
7+ "github.com/shellhub-io/shellhub/api/store"
78 "github.com/shellhub-io/shellhub/pkg/api/requests"
89 "github.com/shellhub-io/shellhub/pkg/hash"
910 "github.com/shellhub-io/shellhub/pkg/models"
11+ "golang.org/x/text/cases"
12+ "golang.org/x/text/language"
1013)
1114
1215type UserService interface {
@@ -23,7 +26,7 @@ type UserService interface {
2326}
2427
2528func (s * service ) UpdateUser (ctx context.Context , req * requests.UpdateUser ) ([]string , error ) {
26- user , _ , err := s .store .UserGetByID (ctx , req . UserID , false )
29+ user , err := s .store .UserGet (ctx , store . UserIdentID , req . UserID )
2730 if err != nil {
2831 return []string {}, NewErrUserNotFound (req .UserID , nil )
2932 }
@@ -38,11 +41,20 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
3841 return conflicts , NewErrUserDuplicated (conflicts , nil )
3942 }
4043
41- changes := & models.UserChanges {
42- Name : req .Name ,
43- Username : strings .ToLower (req .Username ),
44- Email : strings .ToLower (req .Email ),
45- RecoveryEmail : strings .ToLower (req .RecoveryEmail ),
44+ if req .Name != "" {
45+ user .Name = cases .Title (language .AmericanEnglish ).String (strings .ToLower (req .Name ))
46+ }
47+
48+ if req .Username != "" {
49+ user .Username = strings .ToLower (req .Username )
50+ }
51+
52+ if req .Email != "" {
53+ user .Email = strings .ToLower (req .Email )
54+ }
55+
56+ if req .RecoveryEmail != "" {
57+ user .Preferences .SecurityEmail = strings .ToLower (req .RecoveryEmail )
4658 }
4759
4860 if req .Password != "" {
@@ -52,10 +64,10 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
5264 }
5365
5466 passwordDigest , _ := hash .Do (req .Password )
55- changes . Password = passwordDigest
67+ user . PasswordDigest = passwordDigest
5668 }
5769
58- if err := s .store .UserUpdate (ctx , req . UserID , changes ); err != nil {
70+ if err := s .store .Save (ctx , user ); err != nil {
5971 return []string {}, NewErrUserUpdate (user , err )
6072 }
6173
@@ -66,7 +78,7 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
6678//
6779// Deprecated, use [Service.UpdateUser] instead.
6880func (s * service ) UpdatePasswordUser (ctx context.Context , id , currentPassword , newPassword string ) error {
69- user , _ , err := s .store .UserGetByID (ctx , id , false )
81+ user , err := s .store .UserGet (ctx , store . UserIdentID , id )
7082 if user == nil {
7183 return NewErrUserNotFound (id , err )
7284 }
@@ -75,12 +87,11 @@ func (s *service) UpdatePasswordUser(ctx context.Context, id, currentPassword, n
7587 return NewErrUserPasswordNotMatch (nil )
7688 }
7789
78- passwordDigest , err := hash .Do (newPassword )
79- if err != nil {
90+ if user .PasswordDigest , err = hash .Do (newPassword ); err != nil {
8091 return NewErrUserPasswordInvalid (err )
8192 }
8293
83- if err := s .store .UserUpdate (ctx , id , & models. UserChanges { Password : passwordDigest } ); err != nil {
94+ if err := s .store .Save (ctx , user ); err != nil {
8495 return NewErrUserUpdate (user , err )
8596 }
8697
0 commit comments