@@ -104,27 +104,40 @@ func (impl UserServiceImpl) CreateUser(userInfo *bean.UserInfo) ([]*bean.UserInf
104104 var userResponse []* bean.UserInfo
105105 emailIds := strings .Split (userInfo .EmailId , "," )
106106 for _ , emailId := range emailIds {
107- dbConnection := impl .userRepository .GetConnection ()
108- tx , err := dbConnection .Begin ()
109- if err != nil {
110- return nil , err
111- }
112- // Rollback tx on error.
113- defer tx .Rollback ()
114107
115108 dbUser , err := impl .userRepository .FetchActiveOrDeletedUserByEmail (emailId )
116109 if err != nil && err != pg .ErrNoRows {
117110 impl .logger .Errorw ("error while fetching user from db" , "error" , err )
118111 return nil , err
119112 }
113+
114+ //if found update it with new roles
120115 if dbUser != nil && dbUser .Id > 0 && dbUser .Active {
121- // Do nothing, User already exist in our db. (unique check by email id)
122- impl .logger .Infow ("User already exist" , "user" , dbUser )
123- userInfo .Exist = true
124- err = & util.ApiError {Code : "409" , HttpStatusCode : http .StatusConflict , UserMessage : "User Already Exists" }
125- return userResponse , err
126- } else {
127- _ , err := impl .validateUserRequest (userInfo )
116+ updateUserInfo , err := impl .GetById (dbUser .Id )
117+ if err != nil && err != pg .ErrNoRows {
118+ impl .logger .Errorw ("error while fetching user from db" , "error" , err )
119+ return nil , err
120+ }
121+ updateUserInfo .RoleFilters = impl .mergeRoleFilter (updateUserInfo .RoleFilters , userInfo .RoleFilters )
122+ updateUserInfo .Groups = impl .mergeGroups (updateUserInfo .Groups , userInfo .Groups )
123+ updateUserInfo .UserId = userInfo .UserId
124+ updateUserInfo , err = impl .UpdateUser (updateUserInfo )
125+ if err != nil {
126+ return nil , err
127+ }
128+ }
129+
130+ // if not found, create new user
131+ if err == pg .ErrNoRows {
132+ dbConnection := impl .userRepository .GetConnection ()
133+ tx , err := dbConnection .Begin ()
134+ if err != nil {
135+ return nil , err
136+ }
137+ // Rollback tx on error.
138+ defer tx .Rollback ()
139+
140+ _ , err = impl .validateUserRequest (userInfo )
128141 if err != nil {
129142 err = & util.ApiError {HttpStatusCode : http .StatusBadRequest , UserMessage : "Invalid request, please provide role filters" }
130143 return nil , err
@@ -296,12 +309,13 @@ func (impl UserServiceImpl) CreateUser(userInfo *bean.UserInfo) ([]*bean.UserInf
296309 println (pRes )
297310 }
298311 //Ends
299- }
300312
301- err = tx .Commit ()
302- if err != nil {
303- return nil , err
313+ err = tx .Commit ()
314+ if err != nil {
315+ return nil , err
316+ }
304317 }
318+
305319 pass = append (pass , emailId )
306320 userInfo .EmailId = emailId
307321 userInfo .Exist = dbUser .Active
@@ -311,6 +325,52 @@ func (impl UserServiceImpl) CreateUser(userInfo *bean.UserInfo) ([]*bean.UserInf
311325 return userResponse , nil
312326}
313327
328+ func (impl UserServiceImpl ) mergeRoleFilter (oldR []bean.RoleFilter , newR []bean.RoleFilter ) []bean.RoleFilter {
329+ var roleFilters []bean.RoleFilter
330+ keysMap := make (map [string ]bool )
331+ for _ , role := range oldR {
332+ roleFilters = append (roleFilters , bean.RoleFilter {
333+ Entity : role .Entity ,
334+ Team : role .Team ,
335+ Environment : role .Environment ,
336+ EntityName : role .EntityName ,
337+ Action : role .Action ,
338+ })
339+ key := fmt .Sprintf ("%s-%s-%s-%s-%s" , role .Entity , role .Team , role .Environment , role .EntityName , role .Action )
340+ keysMap [key ] = true
341+ }
342+ for _ , role := range newR {
343+ key := fmt .Sprintf ("%s-%s-%s-%s-%s" , role .Entity , role .Team , role .Environment , role .EntityName , role .Action )
344+ if _ , ok := keysMap [key ]; ! ok {
345+ roleFilters = append (roleFilters , bean.RoleFilter {
346+ Entity : role .Entity ,
347+ Team : role .Team ,
348+ Environment : role .Environment ,
349+ EntityName : role .EntityName ,
350+ Action : role .Action ,
351+ })
352+ }
353+ }
354+ return roleFilters
355+ }
356+
357+ func (impl UserServiceImpl ) mergeGroups (oldGroups []string , newGroups []string ) []string {
358+ var groups []string
359+ keysMap := make (map [string ]bool )
360+ for _ , group := range oldGroups {
361+ groups = append (groups , group )
362+ key := fmt .Sprintf (group )
363+ keysMap [key ] = true
364+ }
365+ for _ , group := range newGroups {
366+ key := fmt .Sprintf (group )
367+ if _ , ok := keysMap [key ]; ! ok {
368+ groups = append (groups , group )
369+ }
370+ }
371+ return groups
372+ }
373+
314374func (impl UserServiceImpl ) UpdateUser (userInfo * bean.UserInfo ) (* bean.UserInfo , error ) {
315375 //validating if action user is not admin and trying to update user who has super admin polices, return 403
316376 isUserSuperAdmin , err := impl .IsSuperAdmin (int (userInfo .Id ))
0 commit comments