From 6ac57dc4332ff1e975948fe118796977b11e6ce1 Mon Sep 17 00:00:00 2001 From: Arnab Dutta Date: Fri, 7 Jan 2022 11:54:01 +0530 Subject: [PATCH] feat: add POST /rolePermissionsMapping for adding new rolePermissionsMapping entry #144 --- .../service/user/UserManagementService.java | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/service/user/UserManagementService.java b/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/service/user/UserManagementService.java index 9d7902065da..d18ce7f3349 100644 --- a/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/service/user/UserManagementService.java +++ b/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/service/user/UserManagementService.java @@ -215,21 +215,7 @@ public List getAdminUIRolePermissionsMapping() throws App public List addPermissionsToRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException { try { AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN); - List roles = adminConf.getDynamic().getRoles(); - List 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 roleScopeMappingList = adminConf.getDynamic().getRolePermissionMapping() - .stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole())) - .collect(Collectors.toList()); + List roleScopeMappingList = getRolePermMapByRole(adminConf, rolePermissionMappingArg); if (CollectionUtils.isNotEmpty(roleScopeMappingList)) { log.warn(ErrorResponse.ROLE_PERMISSION_MAPPING_PRESENT.getDescription()); @@ -251,6 +237,7 @@ public List 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); @@ -261,21 +248,7 @@ public List addPermissionsToRole(RolePermissionMapping ro public List mapPermissionsToRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException { try { AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN); - List roles = adminConf.getDynamic().getRoles(); - List 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 roleScopeMappingList = adminConf.getDynamic().getRolePermissionMapping() - .stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole())) - .collect(Collectors.toList()); + List roleScopeMappingList = getRolePermMapByRole(adminConf, rolePermissionMappingArg); if (roleScopeMappingList == null || roleScopeMappingList.isEmpty()) { RolePermissionMapping rolePermissionMapping = new RolePermissionMapping(); @@ -285,8 +258,6 @@ public List mapPermissionsToRole(RolePermissionMapping ro roleScopeMappingList.add(rolePermissionMapping); } - Optional rolePermissionMappingOptional = roleScopeMappingList.stream().findFirst(); - //remove duplicate permissions Set scopesSet = new LinkedHashSet<>(rolePermissionMappingArg.getPermissions()); List combinedScopes = new ArrayList<>(scopesSet); @@ -305,6 +276,7 @@ public List 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); @@ -327,4 +299,27 @@ public List removePermissionsFromRole(RolePermissionMappi throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_DELETING_ROLE_PERMISSION.getDescription()); } } + + private List getRolePermMapByRole(AdminConf adminConf, RolePermissionMapping rolePermissionMappingArg) throws ApplicationException { + validateRolePermissionMapping(adminConf, rolePermissionMappingArg); + + List 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 roles = adminConf.getDynamic().getRoles(); + List 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()); + } + } } \ No newline at end of file