Skip to content

Commit 04a7385

Browse files
refactoring iss
1 parent 2e45765 commit 04a7385

File tree

2 files changed

+59
-66
lines changed

2 files changed

+59
-66
lines changed

pkg/auth/user/RoleGroupService.go

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,12 @@ func (impl RoleGroupServiceImpl) CreateRoleGroup(request *bean.RoleGroup) (*bean
138138

139139
var policies = make([]casbin2.Policy, 0)
140140
if request.SuperAdmin {
141-
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, request.UserId)
142-
if err != nil || flag == false {
143-
impl.logger.Errorw("error in CreateRoleForSuperAdminIfNotExists ", "err", err, "roleGroupName", request.Name)
144-
return nil, err
145-
}
146-
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", bean2.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
141+
policiesToBeAdded, err := impl.CreateAndAddPolicesForSuperAdmin(tx, request.UserId, model.Id, model.CasbinName)
147142
if err != nil {
148-
impl.logger.Errorw("error in getting role by filter for all Types for superAdmin", "err", err)
143+
impl.logger.Errorw("error encountered in CreateRoleGroup", "err", err)
149144
return nil, err
150145
}
151-
if roleModel.Id > 0 {
152-
roleGroupMappingModel := &repository.RoleGroupRoleMapping{RoleGroupId: model.Id, RoleId: roleModel.Id}
153-
roleGroupMappingModel.AuditLog = sql.NewDefaultAuditLog(request.UserId)
154-
roleGroupMappingModel, err = impl.roleGroupRepository.CreateRoleGroupRoleMapping(roleGroupMappingModel, tx)
155-
if err != nil {
156-
impl.logger.Errorw("error in creating role group role mapping", "err", err, "RoleGroupId", model.Id)
157-
return nil, err
158-
}
159-
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(model.CasbinName), Obj: casbin2.Object(roleModel.Role)})
160-
}
146+
policies = append(policies, policiesToBeAdded...)
161147
} else {
162148
policiesToBeAdded, err := impl.createAndAddPolciesForNonSuperAdmin(tx, request.RoleFilters, request.UserId, model)
163149
if err != nil {
@@ -183,6 +169,31 @@ func (impl RoleGroupServiceImpl) CreateRoleGroup(request *bean.RoleGroup) (*bean
183169
return request, nil
184170
}
185171

172+
func (impl RoleGroupServiceImpl) CreateAndAddPolicesForSuperAdmin(tx *pg.Tx, userLoggedInId int32, roleGroupId int32, groupCasbinName string) ([]casbin2.Policy, error) {
173+
policies := make([]casbin2.Policy, 0)
174+
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, userLoggedInId)
175+
if err != nil || flag == false {
176+
impl.logger.Errorw("error in CreateRoleForSuperAdminIfNotExists ", "err", err, "groupCasbinName", groupCasbinName)
177+
return nil, err
178+
}
179+
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", bean2.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
180+
if err != nil {
181+
impl.logger.Errorw("error in getting role by filter for all Types for superAdmin", "err", err)
182+
return nil, err
183+
}
184+
if roleModel.Id > 0 {
185+
roleGroupMappingModel := &repository.RoleGroupRoleMapping{RoleGroupId: roleGroupId, RoleId: roleModel.Id}
186+
roleGroupMappingModel.AuditLog = sql.NewDefaultAuditLog(userLoggedInId)
187+
roleGroupMappingModel, err = impl.roleGroupRepository.CreateRoleGroupRoleMapping(roleGroupMappingModel, tx)
188+
if err != nil {
189+
impl.logger.Errorw("error in creating role group role mapping", "err", err, "RoleGroupId", roleGroupId)
190+
return nil, err
191+
}
192+
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(groupCasbinName), Obj: casbin2.Object(roleModel.Role)})
193+
}
194+
return policies, nil
195+
}
196+
186197
func (impl RoleGroupServiceImpl) createAndAddPolciesForNonSuperAdmin(tx *pg.Tx, roleFilters []bean.RoleFilter, userLoggedInId int32, model *repository.RoleGroup) ([]casbin2.Policy, error) {
187198
capacity, mapping := impl.userCommonService.GetCapacityForRoleFilter(roleFilters)
188199
var policies = make([]casbin2.Policy, 0, capacity)
@@ -441,26 +452,12 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
441452
var eliminatedRoleModels []*repository.RoleModel
442453

443454
if request.SuperAdmin {
444-
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, request.UserId)
445-
if err != nil || flag == false {
446-
impl.logger.Errorw("error in CreateRoleForSuperAdminIfNotExists ", "err", err, "roleGroupName", request.Name)
447-
return nil, err
448-
}
449-
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", bean2.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
455+
policiesToBeAdded, err := impl.CreateAndAddPolicesForSuperAdmin(tx, request.UserId, roleGroup.Id, roleGroup.CasbinName)
450456
if err != nil {
451-
impl.logger.Errorw("error in getting role by filter for all Types for superAdmin", "err", err)
457+
impl.logger.Errorw("error encountered in UpdateRoleGroup", "error", err, "roleGroupId", roleGroup.Id)
452458
return nil, err
453459
}
454-
if roleModel.Id > 0 {
455-
roleGroupMappingModel := &repository.RoleGroupRoleMapping{RoleGroupId: roleGroup.Id, RoleId: roleModel.Id}
456-
roleGroupMappingModel.CreateAuditLog(request.UserId)
457-
roleGroupMappingModel, err = impl.roleGroupRepository.CreateRoleGroupRoleMapping(roleGroupMappingModel, tx)
458-
if err != nil {
459-
impl.logger.Errorw("error in creating role group role mapping", "err", err, "RoleGroupId", roleGroup.Id)
460-
return nil, err
461-
}
462-
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(roleGroup.CasbinName), Obj: casbin2.Object(roleModel.Role)})
463-
}
460+
policies = append(policies, policiesToBeAdded...)
464461
} else {
465462
var policiesToBeAdded, policiesToBeEliminated []casbin2.Policy
466463
policiesToBeAdded, policiesToBeEliminated, eliminatedRoleModels, err = impl.UpdateAndAddPoliciesForNonSuperAdmin(tx, request, roleGroup, token, managerAuth)
@@ -470,7 +467,6 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
470467
}
471468
policies = append(policies, policiesToBeAdded...)
472469
eliminatedPolicies = append(eliminatedPolicies, policiesToBeEliminated...)
473-
474470
}
475471

476472
if checkRBACForGroupUpdate != nil {

pkg/auth/user/UserService.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -425,27 +425,12 @@ func (impl *UserServiceImpl) createUserIfNotExists(userInfo *bean.UserInfo, emai
425425
}
426426
// END GROUP POLICY
427427
} else if userInfo.SuperAdmin == true {
428-
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, userInfo.UserId)
429-
if err != nil || flag == false {
430-
return nil, err
431-
}
432-
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", userBean.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
428+
policiesToBeAdded, err := impl.CreateAndAddPoliciesForSuperAdmin(tx, userInfo.UserId, model.EmailId, model.Id)
433429
if err != nil {
430+
impl.logger.Errorw("error in createUserIfNotExists", "err", err)
434431
return nil, err
435432
}
436-
if roleModel.Id > 0 {
437-
userRoleModel := &repository.UserRoleModel{UserId: model.Id, RoleId: roleModel.Id, AuditLog: sql.AuditLog{
438-
CreatedBy: userInfo.UserId,
439-
CreatedOn: time.Now(),
440-
UpdatedBy: userInfo.UserId,
441-
UpdatedOn: time.Now(),
442-
}}
443-
userRoleModel, err = impl.userAuthRepository.CreateUserRoleMapping(userRoleModel, tx)
444-
if err != nil {
445-
return nil, err
446-
}
447-
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(model.EmailId), Obj: casbin2.Object(roleModel.Role)})
448-
}
433+
policies = append(policies, policiesToBeAdded...)
449434
}
450435
impl.logger.Infow("Checking the length of policies to be added and Adding in casbin ")
451436
if len(policies) > 0 {
@@ -463,6 +448,28 @@ func (impl *UserServiceImpl) createUserIfNotExists(userInfo *bean.UserInfo, emai
463448
return userInfo, nil
464449
}
465450

451+
// CreateAndAddPoliciesForSuperAdmin : checks if super Admin roles else creates and creates mapping in orchestrator , returns casbin polices
452+
func (impl *UserServiceImpl) CreateAndAddPoliciesForSuperAdmin(tx *pg.Tx, userLoggedInId int32, emailId string, userModelId int32) ([]casbin2.Policy, error) {
453+
policies := make([]casbin2.Policy, 0)
454+
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, userLoggedInId)
455+
if err != nil || flag == false {
456+
return nil, err
457+
}
458+
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", userBean.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
459+
if err != nil {
460+
return nil, err
461+
}
462+
if roleModel.Id > 0 {
463+
userRoleModel := &repository.UserRoleModel{UserId: userModelId, RoleId: roleModel.Id, AuditLog: sql.NewDefaultAuditLog(userLoggedInId)}
464+
userRoleModel, err = impl.userAuthRepository.CreateUserRoleMapping(userRoleModel, tx)
465+
if err != nil {
466+
return nil, err
467+
}
468+
policies = append(policies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(emailId), Obj: casbin2.Object(roleModel.Role)})
469+
}
470+
return policies, nil
471+
}
472+
466473
func (impl *UserServiceImpl) CreateOrUpdateUserRolesForAllTypes(roleFilter bean.RoleFilter, userId int32, model *repository.UserModel, existingRoles map[int]repository.UserRoleModel, tx *pg.Tx, entity string, capacity int) ([]casbin2.Policy, bool, error) {
467474
//var policiesToBeAdded []casbin2.Policy
468475
var policiesToBeAdded = make([]casbin2.Policy, 0, capacity)
@@ -785,22 +792,12 @@ func (impl *UserServiceImpl) UpdateUser(userInfo *bean.UserInfo, token string, c
785792
}
786793
}
787794
} else if userInfo.SuperAdmin == true {
788-
flag, err := impl.userAuthRepository.CreateRoleForSuperAdminIfNotExists(tx, userInfo.UserId)
789-
if err != nil || flag == false {
790-
return nil, err
791-
}
792-
roleModel, err := impl.userAuthRepository.GetRoleByFilterForAllTypes("", "", "", "", userBean.SUPER_ADMIN, "", "", "", "", "", "", "", false, "")
795+
policiesToBeAdded, err := impl.CreateAndAddPoliciesForSuperAdmin(tx, userInfo.UserId, model.EmailId, model.Id)
793796
if err != nil {
797+
impl.logger.Errorw("error in update user", "err", err)
794798
return nil, err
795799
}
796-
if roleModel.Id > 0 {
797-
userRoleModel := &repository.UserRoleModel{UserId: model.Id, RoleId: roleModel.Id}
798-
userRoleModel, err = impl.userAuthRepository.CreateUserRoleMapping(userRoleModel, tx)
799-
if err != nil {
800-
return nil, err
801-
}
802-
addedPolicies = append(addedPolicies, casbin2.Policy{Type: "g", Sub: casbin2.Subject(model.EmailId), Obj: casbin2.Object(roleModel.Role)})
803-
}
800+
addedPolicies = append(addedPolicies, policiesToBeAdded...)
804801
}
805802

806803
if checkRBACForUserUpdate != nil {

0 commit comments

Comments
 (0)