From 163ff17f81c1e14420bd0d88454ce85f26960b6f Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Tue, 28 May 2024 18:27:53 +0200 Subject: [PATCH] fix: sonar issues --- .../permission/domain/daos/AssignmentDAO.java | 9 ++- .../domain/services/AssignmentService.java | 8 +- .../controllers/PermissionExportImportV1.java | 56 ++------------ .../v1/mappers/EximExceptionMapperV1.java | 2 - .../rs/exim/v1/mappers/EximMapperV1.java | 74 +++++++++++++++++++ .../domain/daos/AssignmentDAOTest.java | 4 +- 6 files changed, 88 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java b/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java index 02017d0..aea43f6 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java +++ b/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java @@ -3,6 +3,7 @@ import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -124,7 +125,7 @@ public void deleteByRoleProductNameAppId(String roleId, String productName, Stri } @Transactional - public void deleteByProductNameAppId(String productName, String appId) { + public void deleteByProductNameAppIds(String productName, Collection appId) { try { var cb = getEntityManager().getCriteriaBuilder(); var dq = this.deleteQuery(); @@ -132,10 +133,10 @@ public void deleteByProductNameAppId(String productName, String appId) { dq.where(cb.and( cb.equal(root.get(Assignment_.PERMISSION).get(Permission_.PRODUCT_NAME), productName), - cb.equal(root.get(Assignment_.PERMISSION).get(Permission_.APP_ID), appId))); + root.get(Assignment_.PERMISSION).get(Permission_.APP_ID).in(appId))); this.getEntityManager().createQuery(dq).executeUpdate(); } catch (Exception ex) { - throw new DAOException(ErrorKeys.ERROR_DELETE_BY_PRODUCT_NAME_APP_ID, ex); + throw new DAOException(ErrorKeys.ERROR_DELETE_BY_PRODUCT_NAME_APP_IDS, ex); } } @@ -177,7 +178,7 @@ public List findPermissionActionForProducts(Set produc public enum ErrorKeys { - ERROR_DELETE_BY_PRODUCT_NAME_APP_ID, + ERROR_DELETE_BY_PRODUCT_NAME_APP_IDS, ERROR_DELETE_BY_PRODUCTS, ERROR_DELETE_BY_ROLE_PRODUCT_NAME_APP_ID, ERROR_DELETE_BY_PERMISSION_ID, diff --git a/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java b/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java index e977e77..90e2a2e 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java +++ b/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java @@ -37,13 +37,7 @@ public void createRoleProductsAssignments(Role role, List productNames, @Transactional public void importOperator(List assignments, Map> productNames) { - - productNames.forEach((productName, apps) -> { - apps.forEach(appId -> { - dao.deleteByProductNameAppId(productName, appId); - }); - }); - + productNames.forEach((productName, apps) -> dao.deleteByProductNameAppIds(productName, apps)); dao.create(assignments); } diff --git a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/controllers/PermissionExportImportV1.java b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/controllers/PermissionExportImportV1.java index 9d66745..ac94ac3 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/controllers/PermissionExportImportV1.java +++ b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/controllers/PermissionExportImportV1.java @@ -12,7 +12,6 @@ import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.onecx.permission.domain.daos.PermissionDAO; import org.tkit.onecx.permission.domain.daos.RoleDAO; -import org.tkit.onecx.permission.domain.models.Assignment; import org.tkit.onecx.permission.domain.models.Role; import org.tkit.onecx.permission.domain.services.AssignmentService; import org.tkit.onecx.permission.rs.exim.v1.mappers.EximExceptionMapperV1; @@ -46,65 +45,22 @@ public class PermissionExportImportV1 implements PermissionExportImportApi { @Override public Response operatorImportAssignments(AssignmentSnapshotDTOV1 assignmentSnapshotDTO) { - Map> productNames = new HashMap<>(); - Set roleNames = new HashSet<>(); - - assignmentSnapshotDTO.getAssignments().forEach((productName, product) -> { - if (product != null) { - productNames.computeIfAbsent(productName, k -> new ArrayList<>()).addAll(product.keySet()); - product.forEach((appId, app) -> { - if (app != null) { - roleNames.addAll(app.keySet()); - } - }); - } - }); + var request = mapper.createRequestData(assignmentSnapshotDTO); // map of roles for assignments - var roles = roleDAO.findByNames(roleNames); + var roles = roleDAO.findByNames(request.roles()); var roleMap = roles.stream().collect(Collectors.toMap(Role::getName, x -> x)); // map of permissions for products - var permissions = permissionDAO.findByProductNames(productNames.keySet()); + var permissions = permissionDAO.findByProductNames(request.product().keySet()); var permissionMap = permissions.stream().collect(Collectors.toMap(EximMapperV1::permId, x -> x)); - List problems = new ArrayList<>(); - // create assignments - List assignments = new ArrayList<>(); - assignmentSnapshotDTO.getAssignments().forEach((productName, product) -> { - if (product != null) { - product.forEach((appId, app) -> { - if (app != null) { - for (var e : app.entrySet()) { - var roleName = e.getKey(); - - var role = roleMap.get(roleName); - if (role == null) { - problems.add(exceptionMapper.createProblem("Role not found", "Role name: " + roleName)); - continue; - } - - e.getValue().forEach((resource, actions) -> actions.forEach(action -> { - var permId = EximMapperV1.permId(productName, appId, resource, action); - var permission = permissionMap.get(permId); - if (permission == null) { - problems.add(exceptionMapper - .createProblem("Permission not found", "Permission ID: " + permId)); - } else { - var assignment = mapper.create(role, permission); - assignment.setOperator(true); - assignments.add(assignment); - } - })); - } - } - }); - } - }); + List problems = new ArrayList<>(); + var assignments = mapper.createAssignments(problems, assignmentSnapshotDTO, roleMap, permissionMap); // delete old and create new assignments - service.importOperator(assignments, productNames); + service.importOperator(assignments, request.product()); // check problems if (!problems.isEmpty()) { diff --git a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximExceptionMapperV1.java b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximExceptionMapperV1.java index edf2f9e..8f2eef9 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximExceptionMapperV1.java +++ b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximExceptionMapperV1.java @@ -23,8 +23,6 @@ @Mapper(uses = { OffsetDateTimeMapper.class }) public abstract class EximExceptionMapperV1 { - public abstract EximProblemDetailInvalidParamDTOV1 createProblem(String name, String message); - public Response importError(List invalidParamDTOV1s) { var dto = exception(ErrorCode.INVALID_IMPORT_REQUEST.name(), "The request could not be fully completed due to a conflict with the current state of the roles and permissions"); diff --git a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximMapperV1.java b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximMapperV1.java index 9e0dbf3..8a82978 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximMapperV1.java +++ b/src/main/java/org/tkit/onecx/permission/rs/exim/v1/mappers/EximMapperV1.java @@ -1,11 +1,16 @@ package org.tkit.onecx.permission.rs.exim.v1.mappers; +import java.util.*; + import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.tkit.onecx.permission.domain.models.Assignment; import org.tkit.onecx.permission.domain.models.Permission; import org.tkit.onecx.permission.domain.models.Role; +import gen.org.tkit.onecx.permission.rs.exim.v1.model.AssignmentSnapshotDTOV1; +import gen.org.tkit.onecx.permission.rs.exim.v1.model.EximProblemDetailInvalidParamDTOV1; + @Mapper public interface EximMapperV1 { @@ -24,6 +29,72 @@ public interface EximMapperV1 { @Mapping(target = "operator", ignore = true) Assignment create(Role role, Permission permission); + default List createAssignments(List problems, AssignmentSnapshotDTOV1 dto, + Map roleMap, Map permissionMap) { + List assignments = new ArrayList<>(); + dto.getAssignments().forEach((productName, product) -> { + if (product != null) { + product.forEach((appId, app) -> { + if (app != null) { + assignments + .addAll(createProductAppAssignments(productName, appId, app, problems, roleMap, permissionMap)); + } + }); + } + }); + return assignments; + } + + default List createProductAppAssignments(String productName, String appId, + Map>> dto, List problems, + Map roleMap, Map permissionMap) { + List assignments = new ArrayList<>(); + + // application role - resource - actions + for (var e : dto.entrySet()) { + var roleName = e.getKey(); + + var role = roleMap.get(roleName); + if (role == null) { + problems.add(createProblem("Role not found", "Role name: " + roleName)); + continue; + } + + e.getValue().forEach((resource, actions) -> actions.forEach(action -> { + var permId = permId(productName, appId, resource, action); + var permission = permissionMap.get(permId); + if (permission == null) { + problems.add(createProblem("Permission not found", "Permission ID: " + permId)); + } else { + var assignment = create(role, permission); + assignment.setOperator(true); + assignments.add(assignment); + } + })); + } + + return assignments; + } + + EximProblemDetailInvalidParamDTOV1 createProblem(String name, String message); + + default RequestData createRequestData(AssignmentSnapshotDTOV1 dto) { + Map> result = new HashMap<>(); + Set roles = new HashSet<>(); + dto.getAssignments().forEach((productName, product) -> { + if (product != null && !product.keySet().isEmpty()) { + result.computeIfAbsent(productName, k -> new ArrayList<>()).addAll(product.keySet()); + product.forEach((appId, app) -> { + if (app != null) { + roles.addAll(app.keySet()); + } + }); + } + }); + + return new RequestData(result, roles); + } + static String permId(Permission p) { return permId(p.getProductName(), p.getAppId(), p.getResource(), p.getAction()); } @@ -31,4 +102,7 @@ static String permId(Permission p) { static String permId(String productName, String appId, String resource, String action) { return productName + "#" + appId + "#" + resource + "#" + action; } + + record RequestData(Map> product, Set roles) { + } } diff --git a/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java b/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java index b660344..4eb4b9a 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java @@ -23,8 +23,8 @@ void methodExceptionTests() { AssignmentDAO.ErrorKeys.ERROR_DELETE_BY_ROLE_ID); methodExceptionTests(() -> dao.deleteByRoleProductNameAppId(null, null, null), AssignmentDAO.ErrorKeys.ERROR_DELETE_BY_ROLE_PRODUCT_NAME_APP_ID); - methodExceptionTests(() -> dao.deleteByProductNameAppId(null, null), - AssignmentDAO.ErrorKeys.ERROR_DELETE_BY_PRODUCT_NAME_APP_ID); + methodExceptionTests(() -> dao.deleteByProductNameAppIds(null, null), + AssignmentDAO.ErrorKeys.ERROR_DELETE_BY_PRODUCT_NAME_APP_IDS); methodExceptionTests(() -> dao.deleteByProducts(null, null), AssignmentDAO.ErrorKeys.ERROR_DELETE_BY_PRODUCTS); methodExceptionTests(() -> dao.deleteByPermissionId(null),