@@ -3,28 +3,19 @@ package services
33import (
44 "context"
55 "slices"
6+ "strings"
67
78 "github.com/shellhub-io/shellhub/cli/pkg/inputs"
8- "github.com/shellhub-io/shellhub/pkg/clock "
9+ "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
1215// UserCreate adds a new user based on the provided user's data. This method validates data and
1316// checks for conflicts.
1417func (s * service ) UserCreate (ctx context.Context , input * inputs.UserCreate ) (* models.User , error ) {
15- // TODO: convert username and email to lower case.
16- userData := models.UserData {
17- Name : input .Username ,
18- Email : input .Email ,
19- Username : input .Username ,
20- }
21-
22- // TODO: validate this at cmd layer
23- if ok , err := s .validator .Struct (userData ); ! ok || err != nil {
24- return nil , ErrUserDataInvalid
25- }
26-
27- if conflicts , has , _ := s .store .UserConflicts (ctx , & models.UserConflicts {Email : userData .Email , Username : userData .Username }); has {
18+ if conflicts , has , err := s .store .UserConflicts (ctx , & models.UserConflicts {Email : input .Email }); err != nil || has {
2819 containsEmail := slices .Contains (conflicts , "email" )
2920 containsUsername := slices .Contains (conflicts , "username" )
3021
@@ -40,34 +31,32 @@ func (s *service) UserCreate(ctx context.Context, input *inputs.UserCreate) (*mo
4031 }
4132 }
4233
43- password , err := models . HashUserPassword (input .Password )
34+ passwordDigest , err := hash . Do (input .Password )
4435 if err != nil {
4536 return nil , ErrUserPasswordInvalid
4637 }
4738
48- // TODO: validate this at cmd layer
49- if ok , err := s .validator .Struct (password ); ! ok || err != nil {
50- return nil , ErrUserPasswordInvalid
51- }
52-
5339 user := & models.User {
54- Origin : models .UserOriginLocal ,
55- UserData : userData ,
56- Password : password ,
57- Status : models .UserStatusConfirmed ,
58- CreatedAt : clock .Now (),
59- MaxNamespaces : MaxNumberNamespacesCommunity ,
40+ Origin : models .UserOriginLocal ,
41+ ExternalID : "" ,
42+ Status : models .UserStatusConfirmed ,
43+ Name : cases .Title (language .AmericanEnglish ).String (strings .ToLower (input .Username )),
44+ Email : strings .ToLower (input .Email ),
45+ Username : strings .ToLower (input .Username ),
46+ PasswordDigest : passwordDigest ,
6047 Preferences : models.UserPreferences {
61- AuthMethods : []models.UserAuthMethod {models .UserAuthMethodLocal },
48+ PreferredNamespace : "" ,
49+ AuthMethods : []models.UserAuthMethod {models .UserAuthMethodLocal },
50+ SecurityEmail : "" ,
51+ MaxNamespaces : - 1 ,
52+ EmailMarketing : false ,
6253 },
6354 }
6455
6556 if _ , err := s .store .UserCreate (ctx , user ); err != nil {
6657 return nil , ErrCreateNewUser
6758 }
6859
69- s .store .SystemSet (ctx , "setup" , true ) //nolint:errcheck
70-
7160 return user , nil
7261}
7362
@@ -117,17 +106,12 @@ func (s *service) UserUpdate(ctx context.Context, input *inputs.UserUpdate) erro
117106 return ErrUserNotFound
118107 }
119108
120- password , err := models . HashUserPassword (input .Password )
109+ passwordDigest , err := hash . Do (input .Password )
121110 if err != nil {
122111 return ErrUserPasswordInvalid
123112 }
124113
125- // TODO: validate this at cmd layer
126- if ok , err := s .validator .Struct (password ); ! ok || err != nil {
127- return ErrUserPasswordInvalid
128- }
129-
130- if err := s .store .UserUpdate (ctx , user .ID , & models.UserChanges {Password : password .Hash }); err != nil {
114+ if err := s .store .UserUpdate (ctx , user .ID , & models.UserChanges {Password : passwordDigest }); err != nil {
131115 return ErrFailedUpdateUser
132116 }
133117
0 commit comments