Skip to content

Commit

Permalink
fix: sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed May 28, 2024
1 parent 229974f commit 163ff17
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -124,18 +125,18 @@ public void deleteByRoleProductNameAppId(String roleId, String productName, Stri
}

@Transactional
public void deleteByProductNameAppId(String productName, String appId) {
public void deleteByProductNameAppIds(String productName, Collection<String> appId) {
try {
var cb = getEntityManager().getCriteriaBuilder();
var dq = this.deleteQuery();
var root = dq.from(Assignment.class);

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);
}
}

Expand Down Expand Up @@ -177,7 +178,7 @@ public List<PermissionAction> findPermissionActionForProducts(Set<String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ public void createRoleProductsAssignments(Role role, List<String> productNames,

@Transactional
public void importOperator(List<Assignment> assignments, Map<String, List<String>> productNames) {

productNames.forEach((productName, apps) -> {
apps.forEach(appId -> {
dao.deleteByProductNameAppId(productName, appId);
});
});

productNames.forEach((productName, apps) -> dao.deleteByProductNameAppIds(productName, apps));
dao.create(assignments);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,65 +45,22 @@ public class PermissionExportImportV1 implements PermissionExportImportApi {
@Override
public Response operatorImportAssignments(AssignmentSnapshotDTOV1 assignmentSnapshotDTO) {

Map<String, List<String>> productNames = new HashMap<>();
Set<String> 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<EximProblemDetailInvalidParamDTOV1> problems = new ArrayList<>();

// create assignments
List<Assignment> 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<EximProblemDetailInvalidParamDTOV1> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class EximExceptionMapperV1 {

public abstract EximProblemDetailInvalidParamDTOV1 createProblem(String name, String message);

public Response importError(List<EximProblemDetailInvalidParamDTOV1> 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -24,11 +29,80 @@ public interface EximMapperV1 {
@Mapping(target = "operator", ignore = true)
Assignment create(Role role, Permission permission);

default List<Assignment> createAssignments(List<EximProblemDetailInvalidParamDTOV1> problems, AssignmentSnapshotDTOV1 dto,
Map<String, Role> roleMap, Map<String, Permission> permissionMap) {
List<Assignment> 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<Assignment> createProductAppAssignments(String productName, String appId,
Map<String, Map<String, List<String>>> dto, List<EximProblemDetailInvalidParamDTOV1> problems,
Map<String, Role> roleMap, Map<String, Permission> permissionMap) {
List<Assignment> 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<String, List<String>> result = new HashMap<>();
Set<String> 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());
}

static String permId(String productName, String appId, String resource, String action) {
return productName + "#" + appId + "#" + resource + "#" + action;
}

record RequestData(Map<String, List<String>> product, Set<String> roles) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 163ff17

Please sign in to comment.