diff --git a/src/main/java/io/github/onecx/permission/domain/criteria/PermissionSearchCriteria.java b/src/main/java/io/github/onecx/permission/domain/criteria/PermissionSearchCriteria.java index 254cd09..55e17d3 100644 --- a/src/main/java/io/github/onecx/permission/domain/criteria/PermissionSearchCriteria.java +++ b/src/main/java/io/github/onecx/permission/domain/criteria/PermissionSearchCriteria.java @@ -9,7 +9,7 @@ public class PermissionSearchCriteria { private String appId; private String name; - private String object; + private String resource; private String action; private Integer pageNumber; private Integer pageSize; diff --git a/src/main/java/io/github/onecx/permission/domain/daos/PermissionDAO.java b/src/main/java/io/github/onecx/permission/domain/daos/PermissionDAO.java index 8bd3e06..24421cf 100644 --- a/src/main/java/io/github/onecx/permission/domain/daos/PermissionDAO.java +++ b/src/main/java/io/github/onecx/permission/domain/daos/PermissionDAO.java @@ -38,8 +38,8 @@ public PageResult findByCriteria(PermissionSearchCriteria criteria) if (criteria.getAppId() != null && !criteria.getAppId().isBlank()) { predicates.add(cb.like(root.get(Permission_.appId), QueryCriteriaUtil.wildcard(criteria.getAppId()))); } - if (criteria.getObject() != null && !criteria.getObject().isBlank()) { - predicates.add(cb.like(root.get(Permission_.object), QueryCriteriaUtil.wildcard(criteria.getObject()))); + if (criteria.getResource() != null && !criteria.getResource().isBlank()) { + predicates.add(cb.like(root.get(Permission_.resource), QueryCriteriaUtil.wildcard(criteria.getResource()))); } if (!predicates.isEmpty()) { diff --git a/src/main/java/io/github/onecx/permission/domain/models/Permission.java b/src/main/java/io/github/onecx/permission/domain/models/Permission.java index 2c0debf..0da39ee 100644 --- a/src/main/java/io/github/onecx/permission/domain/models/Permission.java +++ b/src/main/java/io/github/onecx/permission/domain/models/Permission.java @@ -14,7 +14,7 @@ @Setter @Entity @Table(name = "PERMISSION", uniqueConstraints = { - @UniqueConstraint(name = "PERMISSION_KEY", columnNames = { "APP_ID", "OBJECT", "ACTION" }) + @UniqueConstraint(name = "PERMISSION_KEY", columnNames = { "APP_ID", "RESOURCE", "ACTION" }) }) @SuppressWarnings("squid:S2160") public class Permission extends TraceableEntity { @@ -31,8 +31,8 @@ public class Permission extends TraceableEntity { /** * The permission object. */ - @Column(name = "OBJECT") - private String object; + @Column(name = "RESOURCE") + private String resource; /** * The permission name. diff --git a/src/main/java/io/github/onecx/permission/rs/internal/mappers/PermissionMapper.java b/src/main/java/io/github/onecx/permission/rs/internal/mappers/PermissionMapper.java index fc576c5..d377718 100644 --- a/src/main/java/io/github/onecx/permission/rs/internal/mappers/PermissionMapper.java +++ b/src/main/java/io/github/onecx/permission/rs/internal/mappers/PermissionMapper.java @@ -19,6 +19,5 @@ public interface PermissionMapper { @Mapping(target = "removeStreamItem", ignore = true) PermissionPageResultDTO map(PageResult page); - @Mapping(target = "_object", source = "object") PermissionDTO map(Permission data); } diff --git a/src/main/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java b/src/main/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java index fae87cd..7de4280 100644 --- a/src/main/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java +++ b/src/main/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java @@ -10,6 +10,7 @@ import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; +import org.tkit.quarkus.jpa.exceptions.ConstraintException; import org.tkit.quarkus.log.cdi.LogService; import gen.io.github.onecx.permission.rs.operator.v1.PermissionOperatorApi; @@ -42,10 +43,10 @@ public Response createOrUpdatePermission(String appId, PermissionRequestDTOV1 pe return Response.ok().build(); } var permissions = dao.loadByAppId(appId); - var map = permissions.stream().collect(Collectors.toMap(x -> x.getObject() + x.getAction(), x -> x)); + var map = permissions.stream().collect(Collectors.toMap(x -> x.getResource() + x.getAction(), x -> x)); for (Permission item : data) { - var permission = map.get(item.getObject() + item.getAction()); + var permission = map.get(item.getResource() + item.getAction()); if (permission == null) { // create new permission dao.create(item); @@ -59,6 +60,11 @@ public Response createOrUpdatePermission(String appId, PermissionRequestDTOV1 pe return Response.ok().build(); } + @ServerExceptionMapper + public RestResponse exception(ConstraintException ex) { + return exceptionMapper.exception(ex); + } + @ServerExceptionMapper public RestResponse constraint(ConstraintViolationException ex) { return exceptionMapper.constraint(ex); diff --git a/src/main/java/io/github/onecx/permission/rs/operator/v1/log/OperatorLogParam.java b/src/main/java/io/github/onecx/permission/rs/operator/v1/log/OperatorLogParam.java new file mode 100644 index 0000000..555c910 --- /dev/null +++ b/src/main/java/io/github/onecx/permission/rs/operator/v1/log/OperatorLogParam.java @@ -0,0 +1,19 @@ +package io.github.onecx.permission.rs.operator.v1.log; + +import java.util.List; + +import jakarta.enterprise.context.ApplicationScoped; + +import org.tkit.quarkus.log.cdi.LogParam; + +import gen.io.github.onecx.permission.rs.operator.v1.model.PermissionRequestDTOV1; + +@ApplicationScoped +public class OperatorLogParam implements LogParam { + + @Override + public List getClasses() { + return List.of( + item(10, PermissionRequestDTOV1.class, x -> x.getClass().getSimpleName())); + } +} diff --git a/src/main/java/io/github/onecx/permission/rs/operator/v1/mappers/ExceptionMapper.java b/src/main/java/io/github/onecx/permission/rs/operator/v1/mappers/ExceptionMapper.java index d75a333..4a7e5d7 100644 --- a/src/main/java/io/github/onecx/permission/rs/operator/v1/mappers/ExceptionMapper.java +++ b/src/main/java/io/github/onecx/permission/rs/operator/v1/mappers/ExceptionMapper.java @@ -12,6 +12,7 @@ import org.jboss.resteasy.reactive.RestResponse; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.tkit.quarkus.jpa.exceptions.ConstraintException; import gen.io.github.onecx.permission.rs.operator.v1.model.ProblemDetailInvalidParamDTOV1; import gen.io.github.onecx.permission.rs.operator.v1.model.ProblemDetailParamDTOV1; @@ -20,6 +21,12 @@ @Mapper public abstract class ExceptionMapper { + public RestResponse exception(ConstraintException ex) { + var dto = exception(ex.getMessageKey().name(), ex.getConstraints()); + dto.setParams(map(ex.namedParameters)); + return RestResponse.status(Response.Status.BAD_REQUEST, dto); + } + public RestResponse constraint(ConstraintViolationException ex) { var dto = exception(ErrorKeys.CONSTRAINT_VIOLATIONS.name(), ex.getMessage()); dto.setInvalidParams(createErrorValidationResponse(ex.getConstraintViolations())); diff --git a/src/main/openapi/onecx-permission-internal-openapi.yaml b/src/main/openapi/onecx-permission-internal-openapi.yaml index a3a374c..ca80c07 100644 --- a/src/main/openapi/onecx-permission-internal-openapi.yaml +++ b/src/main/openapi/onecx-permission-internal-openapi.yaml @@ -44,7 +44,7 @@ components: type: string name: type: string - object: + resource: type: string action: type: string @@ -98,7 +98,7 @@ components: type: string name: type: string - object: + resource: type: string action: type: string diff --git a/src/main/openapi/onecx-permission-operator-v1.yaml b/src/main/openapi/onecx-permission-operator-v1.yaml index 0d0e901..55b13f4 100644 --- a/src/main/openapi/onecx-permission-operator-v1.yaml +++ b/src/main/openapi/onecx-permission-operator-v1.yaml @@ -48,12 +48,12 @@ components: Permission: type: object required: - - object + - resource - action properties: name: type: string - object: + resource: type: string action: type: string diff --git a/src/main/resources/db/v1/2024-01-04-create-tables.xml b/src/main/resources/db/v1/2024-01-04-create-tables.xml index fee0490..0ce4e72 100644 --- a/src/main/resources/db/v1/2024-01-04-create-tables.xml +++ b/src/main/resources/db/v1/2024-01-04-create-tables.xml @@ -20,11 +20,11 @@ - + - + diff --git a/src/test/java/io/github/onecx/permission/rs/internal/controllers/PermissionRestControllerTest.java b/src/test/java/io/github/onecx/permission/rs/internal/controllers/PermissionRestControllerTest.java index 72db785..72c5709 100644 --- a/src/test/java/io/github/onecx/permission/rs/internal/controllers/PermissionRestControllerTest.java +++ b/src/test/java/io/github/onecx/permission/rs/internal/controllers/PermissionRestControllerTest.java @@ -42,7 +42,7 @@ void searchTest() { criteria.setAppId(" "); criteria.setName(" "); - criteria.setObject(" "); + criteria.setResource(" "); criteria.setAction(" "); data = given() @@ -65,7 +65,7 @@ void searchCriteriaTest() { var criteria = new PermissionSearchCriteriaDTO(); criteria.setAppId("app1"); criteria.setName("n1"); - criteria.setObject("o1"); + criteria.setResource("o1"); criteria.setAction("a1"); var data = given() diff --git a/src/test/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestControllerTest.java b/src/test/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestControllerTest.java index 223dd5e..0724d28 100644 --- a/src/test/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestControllerTest.java +++ b/src/test/java/io/github/onecx/permission/rs/operator/v1/controllers/OperatorRestControllerTest.java @@ -84,8 +84,8 @@ void requestWrongPermissionTest() { @Test void requestPermissionTest() { - var per1 = new PermissionDTOV1().action("a1")._object("o1").name("name").description("description"); - var per2 = new PermissionDTOV1().action("new1")._object("o1").name("name1").description("description1"); + var per1 = new PermissionDTOV1().action("a1").resource("o1").name("name").description("description"); + var per2 = new PermissionDTOV1().action("new1").resource("o1").name("name1").description("description1"); var request = new PermissionRequestDTOV1(); request.setPermissions(List.of(per1, per2)); @@ -99,4 +99,29 @@ void requestPermissionTest() { .statusCode(OK.getStatusCode()); } + + @Test + void requestDuplicatePermissionTest() { + var per1 = new PermissionDTOV1().action("a1").resource("o1").name("name").description("description"); + var per2 = new PermissionDTOV1().action("a1").resource("o1").name("name1").description("description1"); + + var request = new PermissionRequestDTOV1(); + request.setPermissions(List.of(per1, per2)); + + var exception = given() + .contentType(APPLICATION_JSON) + .body(request) + .pathParam("appId", "app3") + .put() + .then() + .statusCode(BAD_REQUEST.getStatusCode()) + .contentType(APPLICATION_JSON) + .extract() + .as(ProblemDetailResponseDTOV1.class); + + assertThat(exception).isNotNull(); + assertThat(exception.getErrorCode()).isEqualTo("PERSIST_ENTITY_FAILED"); + assertThat(exception.getDetail()).isEqualTo( + "could not execute statement [ERROR: duplicate key value violates unique constraint 'permission_key' Detail: Key (app_id, resource, action)=(app3, o1, a1) already exists.]"); + } } diff --git a/src/test/resources/data/test-internal.xml b/src/test/resources/data/test-internal.xml index 3fc27d3..45766b8 100644 --- a/src/test/resources/data/test-internal.xml +++ b/src/test/resources/data/test-internal.xml @@ -2,13 +2,13 @@ - - - + + + - - + + - - + + \ No newline at end of file diff --git a/src/test/resources/data/test-operator-v1.xml b/src/test/resources/data/test-operator-v1.xml index 3fc27d3..45766b8 100644 --- a/src/test/resources/data/test-operator-v1.xml +++ b/src/test/resources/data/test-operator-v1.xml @@ -2,13 +2,13 @@ - - - + + + - - + + - - + + \ No newline at end of file