Skip to content

Commit

Permalink
feat: changed appIds to productNames
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Feb 28, 2024
1 parent cd8b196 commit b907cd5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PageResult<Assignment> findByCriteria(AssignmentSearchCriteria criteria)
}
}

public void deleteByRoleAndPermissionId(String roleId, List<String> permissionId) {
public void deleteByCriteria(String roleId, List<String> productNames, String permissionId) {
var cb = getEntityManager().getCriteriaBuilder();
var dq = this.deleteQuery();
var root = dq.from(Assignment.class);
Expand All @@ -70,8 +70,12 @@ public void deleteByRoleAndPermissionId(String roleId, List<String> permissionId

predicates.add(cb.equal(root.get(Assignment_.ROLE).get(TraceableEntity_.ID), roleId));

if (productNames != null) {
predicates.add(root.get(Assignment_.PERMISSION).get(Permission_.PRODUCT_NAME).in(productNames));
}

if (permissionId != null) {
predicates.add(root.get(Assignment_.PERMISSION).get(TraceableEntity_.ID).in(permissionId));
predicates.add(cb.equal(root.get(Assignment_.PERMISSION).get(TraceableEntity_.ID), permissionId));
}

dq.where(cb.and(predicates.toArray(new Predicate[0])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public List<Permission> loadByAppId(String appId) {
}
}

public List<Permission> loadByAppIds(List<String> appId) {
public List<Permission> loadByProductNames(List<String> productNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Permission.class);
var root = cq.from(Permission.class);
cq.where(root.get(Permission_.APP_ID).in(appId));
cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_BY_APP_ID, ex);
throw new DAOException(ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES, ex);
}
}

Expand Down Expand Up @@ -90,6 +90,7 @@ public enum ErrorKeys {

ERROR_FIND_PERMISSION_FOR_USER,
ERROR_LOAD_BY_APP_ID,
ERROR_FIND_PERMISSION_BY_CRITERIA;
ERROR_FIND_PERMISSION_BY_CRITERIA,
ERROR_LOAD_BY_PRODUCT_NAMES;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.tkit.onecx.permission.rs.internal.controllers;

import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
Expand All @@ -18,7 +16,6 @@
import org.tkit.onecx.permission.rs.internal.mappers.AssignmentMapper;
import org.tkit.onecx.permission.rs.internal.mappers.ExceptionMapper;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.jpa.models.TraceableEntity;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.permission.rs.internal.AssignmentInternalApi;
Expand Down Expand Up @@ -66,26 +63,19 @@ public Response revokeAssignments(CreateRevokeAssignmentRequestDTO createRevokeA
}

// case 1 ONLY ROLE ID
if (createRevokeAssignmentRequestDTO.getPermissionId() == null && createRevokeAssignmentRequestDTO.getAppId() == null) {
dao.deleteByRoleAndPermissionId(role.getId(), null);
if (createRevokeAssignmentRequestDTO.getPermissionId() == null
&& createRevokeAssignmentRequestDTO.getProductNames() == null) {
dao.deleteByCriteria(role.getId(), null, null);
}

// case 2 ROLE ID + APP ID
if (createRevokeAssignmentRequestDTO.getAppId() != null) {
var permissions = permissionDAO.loadByAppIds(createRevokeAssignmentRequestDTO.getAppId());
if (permissions.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
dao.deleteByRoleAndPermissionId(role.getId(), permissions.stream().map(TraceableEntity::getId).toList());
// case 2 ROLE ID + PRODUCT NAMES
if (createRevokeAssignmentRequestDTO.getProductNames() != null) {
dao.deleteByCriteria(role.getId(), createRevokeAssignmentRequestDTO.getProductNames(), null);
}

// case 3 ROLE ID + permissionID
if (createRevokeAssignmentRequestDTO.getPermissionId() != null) {
var permission = permissionDAO.findById(createRevokeAssignmentRequestDTO.getPermissionId());
if (permission == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
dao.deleteByRoleAndPermissionId(role.getId(), List.of(permission.getId()));
dao.deleteByCriteria(role.getId(), null, createRevokeAssignmentRequestDTO.getPermissionId());
}
return Response.status(Response.Status.NO_CONTENT).build();
}
Expand Down Expand Up @@ -120,15 +110,15 @@ public Response createAssignment(CreateRevokeAssignmentRequestDTO createAssignme
.build();
}

// batch operation for all permissions by appId
if (createAssignmentRequestDTO.getAppId() != null) {
var permissions = permissionDAO.loadByAppIds(createAssignmentRequestDTO.getAppId());
// batch operation for all permissions by productNames
if (createAssignmentRequestDTO.getProductNames() != null) {
var permissions = permissionDAO.loadByProductNames(createAssignmentRequestDTO.getProductNames());
if (permissions.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
var data = mapper.createList(role, permissions);

dao.deleteByRoleAndPermissionId(role.getId(), null);
dao.deleteByCriteria(role.getId(), createAssignmentRequestDTO.getProductNames(), null);
var result = dao.create(data).toList();
return Response.status(Response.Status.CREATED).entity(mapper.mapResponseList(result, null)).build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ components:
type: string
permissionId:
type: string
appId:
productNames:
type: array
items:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void methodExceptionTests() {
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_FOR_USER);
methodExceptionTests(() -> dao.loadByAppId(null),
PermissionDAO.ErrorKeys.ERROR_LOAD_BY_APP_ID);
methodExceptionTests(() -> dao.loadByAppIds(null),
PermissionDAO.ErrorKeys.ERROR_LOAD_BY_APP_ID);
methodExceptionTests(() -> dao.loadByProductNames(null),
PermissionDAO.ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES);
methodExceptionTests(() -> dao.findByCriteria(null),
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_BY_CRITERIA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void batchCreateAssignmentsTest() {
// create Assignment
var requestDTO = new CreateRevokeAssignmentRequestDTO();
requestDTO.setRoleId("r14");
requestDTO.setAppId(List.of("app1", "app2"));
requestDTO.setProductNames(List.of("test1"));

var output = given()
.when()
Expand All @@ -132,7 +132,7 @@ void batchCreateAssignmentsTest() {

var request = new CreateRevokeAssignmentRequestDTO();
request.setRoleId("r12");
request.setAppId(List.of("randomAppId"));
request.setProductNames(List.of("randomProductName"));

given()
.when()
Expand Down Expand Up @@ -193,23 +193,13 @@ void revokeAssignmentsByRoleIdAndPermissionIdTest() {
.get("a12")
.then()
.statusCode(NOT_FOUND.getStatusCode());

//not-existing permissionId
requestDTO.setPermissionId("not-existing");
given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTO)
.post("/revoke")
.then()
.statusCode(NOT_FOUND.getStatusCode());
}

@Test
void revokeAssignmentsByRoleIdAndAppIdsTest() {
var requestDTO = new CreateRevokeAssignmentRequestDTO();
requestDTO.roleId("r14");
requestDTO.appId(List.of("app1"));
requestDTO.setProductNames(List.of("test1"));
given()
.when()
.contentType(APPLICATION_JSON)
Expand All @@ -224,16 +214,6 @@ void revokeAssignmentsByRoleIdAndAppIdsTest() {
.get("a12")
.then()
.statusCode(NOT_FOUND.getStatusCode());

//not-existing appIds
requestDTO.setAppId(List.of("not-existing"));
given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTO)
.post("/revoke")
.then()
.statusCode(NOT_FOUND.getStatusCode());
}

@Test
Expand Down

0 comments on commit b907cd5

Please sign in to comment.