@@ -4,9 +4,12 @@ import (
4
4
"context"
5
5
"strings"
6
6
7
+ "github.com/shellhub-io/shellhub/api/store"
7
8
"github.com/shellhub-io/shellhub/pkg/api/requests"
8
9
"github.com/shellhub-io/shellhub/pkg/hash"
9
10
"github.com/shellhub-io/shellhub/pkg/models"
11
+ "golang.org/x/text/cases"
12
+ "golang.org/x/text/language"
10
13
)
11
14
12
15
type UserService interface {
@@ -23,7 +26,7 @@ type UserService interface {
23
26
}
24
27
25
28
func (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 )
27
30
if err != nil {
28
31
return []string {}, NewErrUserNotFound (req .UserID , nil )
29
32
}
@@ -38,11 +41,20 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
38
41
return conflicts , NewErrUserDuplicated (conflicts , nil )
39
42
}
40
43
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 )
46
58
}
47
59
48
60
if req .Password != "" {
@@ -52,10 +64,10 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
52
64
}
53
65
54
66
passwordDigest , _ := hash .Do (req .Password )
55
- changes . Password = passwordDigest
67
+ user . PasswordDigest = passwordDigest
56
68
}
57
69
58
- if err := s .store .UserUpdate (ctx , req . UserID , changes ); err != nil {
70
+ if err := s .store .Save (ctx , user ); err != nil {
59
71
return []string {}, NewErrUserUpdate (user , err )
60
72
}
61
73
@@ -66,7 +78,7 @@ func (s *service) UpdateUser(ctx context.Context, req *requests.UpdateUser) ([]s
66
78
//
67
79
// Deprecated, use [Service.UpdateUser] instead.
68
80
func (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 )
70
82
if user == nil {
71
83
return NewErrUserNotFound (id , err )
72
84
}
@@ -75,12 +87,11 @@ func (s *service) UpdatePasswordUser(ctx context.Context, id, currentPassword, n
75
87
return NewErrUserPasswordNotMatch (nil )
76
88
}
77
89
78
- passwordDigest , err := hash .Do (newPassword )
79
- if err != nil {
90
+ if user .PasswordDigest , err = hash .Do (newPassword ); err != nil {
80
91
return NewErrUserPasswordInvalid (err )
81
92
}
82
93
83
- if err := s .store .UserUpdate (ctx , id , & models. UserChanges { Password : passwordDigest } ); err != nil {
94
+ if err := s .store .Save (ctx , user ); err != nil {
84
95
return NewErrUserUpdate (user , err )
85
96
}
86
97
0 commit comments