@@ -49,6 +49,7 @@ type UserService interface {
49
49
CheckUserRoles (id int32 ) ([]string , error )
50
50
SyncOrchestratorToCasbin () (bool , error )
51
51
GetUserByToken (token string ) (int32 , error )
52
+ IsSuperAdmin (userId int ) (bool , error )
52
53
}
53
54
54
55
type UserServiceImpl struct {
@@ -311,41 +312,27 @@ func (impl UserServiceImpl) CreateUser(userInfo *bean.UserInfo) ([]*bean.UserInf
311
312
}
312
313
313
314
func (impl UserServiceImpl ) UpdateUser (userInfo * bean.UserInfo ) (* bean.UserInfo , error ) {
314
-
315
315
//validating if action user is not admin and trying to update user who has super admin polices, return 403
316
- isSuperAdmin := false
317
- userCasbinRoles , err := impl .CheckUserRoles (userInfo .Id )
316
+ isUserSuperAdmin , err := impl .IsSuperAdmin (int (userInfo .Id ))
318
317
if err != nil {
319
318
return nil , err
320
319
}
321
- actionUserRoles , err := impl .CheckUserRoles ( userInfo .UserId )
320
+ isActionPerformingUserSuperAdmin , err := impl .IsSuperAdmin ( int ( userInfo .UserId ) )
322
321
if err != nil {
323
322
return nil , err
324
323
}
325
324
//if request comes to make user as a super admin, action performing user also be super admin
326
325
if userInfo .SuperAdmin {
327
- for _ , item := range actionUserRoles {
328
- if item == bean .SUPERADMIN {
329
- isSuperAdmin = true
330
- }
331
- }
332
- if isSuperAdmin == false {
326
+ if ! isUserSuperAdmin {
333
327
err = & util.ApiError {HttpStatusCode : http .StatusForbidden , UserMessage : "Invalid request, not allow to update super admin type user" }
334
328
return nil , err
335
329
}
336
330
}
337
331
//if user which going to updated is super admin, action performing user also be super admin
338
- for _ , item := range userCasbinRoles {
339
- if item == bean .SUPERADMIN {
340
- for _ , item := range actionUserRoles {
341
- if item == bean .SUPERADMIN {
342
- isSuperAdmin = true
343
- }
344
- }
345
- if isSuperAdmin == false {
346
- err = & util.ApiError {HttpStatusCode : http .StatusForbidden , UserMessage : "Invalid request, not allow to update super admin type user" }
347
- return nil , err
348
- }
332
+ if isUserSuperAdmin {
333
+ if ! isActionPerformingUserSuperAdmin {
334
+ err = & util.ApiError {HttpStatusCode : http .StatusForbidden , UserMessage : "Invalid request, not allow to update super admin type user" }
335
+ return nil , err
349
336
}
350
337
}
351
338
@@ -525,6 +512,10 @@ func (impl UserServiceImpl) UpdateUser(userInfo *bean.UserInfo) (*bean.UserInfo,
525
512
//ROLE GROUP SETUP
526
513
newGroupMap := make (map [string ]string )
527
514
oldGroupMap := make (map [string ]string )
515
+ userCasbinRoles , err := impl .CheckUserRoles (userInfo .Id )
516
+ if err != nil {
517
+ return nil , err
518
+ }
528
519
for _ , oldItem := range userCasbinRoles {
529
520
oldGroupMap [oldItem ] = oldItem
530
521
}
@@ -944,3 +935,20 @@ func (impl UserServiceImpl) SyncOrchestratorToCasbin() (bool, error) {
944
935
}*/
945
936
return true , nil
946
937
}
938
+
939
+ func (impl UserServiceImpl ) IsSuperAdmin (userId int ) (bool , error ) {
940
+ //validating if action user is not admin and trying to update user who has super admin polices, return 403
941
+ isSuperAdmin := false
942
+ userCasbinRoles , err := impl .CheckUserRoles (int32 (userId ))
943
+ if err != nil {
944
+ return isSuperAdmin , err
945
+ }
946
+ //if user which going to updated is super admin, action performing user also be super admin
947
+ for _ , item := range userCasbinRoles {
948
+ if item == bean .SUPERADMIN {
949
+ isSuperAdmin = true
950
+ break
951
+ }
952
+ }
953
+ return isSuperAdmin , nil
954
+ }
0 commit comments