Skip to content

Commit

Permalink
feat: add POST /rolePermissionsMapping for adding new rolePermissions…
Browse files Browse the repository at this point in the history
…Mapping entry #144
  • Loading branch information
duttarnab committed Jan 7, 2022
1 parent bd34c29 commit 6ac57dc
Showing 1 changed file with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,7 @@ public List<RolePermissionMapping> getAdminUIRolePermissionsMapping() throws App
public List<RolePermissionMapping> addPermissionsToRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
try {
AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
List<AdminRole> roles = adminConf.getDynamic().getRoles();
List<AdminPermission> permissions = adminConf.getDynamic().getPermissions();

if (roles.stream().noneMatch(ele -> ele.getRole().equals(rolePermissionMappingArg.getRole()))) {
log.error(ErrorResponse.ROLE_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_NOT_FOUND.getDescription());
}
if (permissions.stream().noneMatch(ele -> rolePermissionMappingArg.getPermissions().contains(ele.getPermission()))) {
log.error(ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
}

List<RolePermissionMapping> roleScopeMappingList = adminConf.getDynamic().getRolePermissionMapping()
.stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole()))
.collect(Collectors.toList());
List<RolePermissionMapping> roleScopeMappingList = getRolePermMapByRole(adminConf, rolePermissionMappingArg);

if (CollectionUtils.isNotEmpty(roleScopeMappingList)) {
log.warn(ErrorResponse.ROLE_PERMISSION_MAPPING_PRESENT.getDescription());
Expand All @@ -251,6 +237,7 @@ public List<RolePermissionMapping> addPermissionsToRole(RolePermissionMapping ro
entryManager.merge(adminConf);
return adminConf.getDynamic().getRolePermissionMapping();
} catch (ApplicationException e) {
log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription());
throw e;
} catch (Exception e) {
log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription(), e);
Expand All @@ -261,21 +248,7 @@ public List<RolePermissionMapping> addPermissionsToRole(RolePermissionMapping ro
public List<RolePermissionMapping> mapPermissionsToRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
try {
AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
List<AdminRole> roles = adminConf.getDynamic().getRoles();
List<AdminPermission> permissions = adminConf.getDynamic().getPermissions();

if (roles.stream().noneMatch(ele -> ele.getRole().equals(rolePermissionMappingArg.getRole()))) {
log.error(ErrorResponse.ROLE_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_NOT_FOUND.getDescription());
}
if (permissions.stream().noneMatch(ele -> rolePermissionMappingArg.getPermissions().contains(ele.getPermission()))) {
log.error(ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
}

List<RolePermissionMapping> roleScopeMappingList = adminConf.getDynamic().getRolePermissionMapping()
.stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole()))
.collect(Collectors.toList());
List<RolePermissionMapping> roleScopeMappingList = getRolePermMapByRole(adminConf, rolePermissionMappingArg);

if (roleScopeMappingList == null || roleScopeMappingList.isEmpty()) {
RolePermissionMapping rolePermissionMapping = new RolePermissionMapping();
Expand All @@ -285,8 +258,6 @@ public List<RolePermissionMapping> mapPermissionsToRole(RolePermissionMapping ro
roleScopeMappingList.add(rolePermissionMapping);
}

Optional<RolePermissionMapping> rolePermissionMappingOptional = roleScopeMappingList.stream().findFirst();

//remove duplicate permissions
Set<String> scopesSet = new LinkedHashSet<>(rolePermissionMappingArg.getPermissions());
List<String> combinedScopes = new ArrayList<>(scopesSet);
Expand All @@ -305,6 +276,7 @@ public List<RolePermissionMapping> mapPermissionsToRole(RolePermissionMapping ro
entryManager.merge(adminConf);
return adminConf.getDynamic().getRolePermissionMapping();
} catch (ApplicationException e) {
log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription());
throw e;
} catch (Exception e) {
log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription(), e);
Expand All @@ -327,4 +299,27 @@ public List<RolePermissionMapping> removePermissionsFromRole(RolePermissionMappi
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_DELETING_ROLE_PERMISSION.getDescription());
}
}

private List<RolePermissionMapping> getRolePermMapByRole(AdminConf adminConf, RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
validateRolePermissionMapping(adminConf, rolePermissionMappingArg);

List<RolePermissionMapping> roleScopeMappingList = adminConf.getDynamic().getRolePermissionMapping()
.stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole()))
.collect(Collectors.toList());
return roleScopeMappingList;
}

private void validateRolePermissionMapping(AdminConf adminConf, RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
List<AdminRole> roles = adminConf.getDynamic().getRoles();
List<AdminPermission> permissions = adminConf.getDynamic().getPermissions();

if (roles.stream().noneMatch(ele -> ele.getRole().equals(rolePermissionMappingArg.getRole()))) {
log.error(ErrorResponse.ROLE_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_NOT_FOUND.getDescription());
}
if (permissions.stream().noneMatch(ele -> rolePermissionMappingArg.getPermissions().contains(ele.getPermission()))) {
log.error(ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
}
}
}

0 comments on commit 6ac57dc

Please sign in to comment.