Skip to content

Commit

Permalink
feat: update rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Jan 5, 2024
1 parent a7936b3 commit 79fd061
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public PageResult<Permission> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public interface PermissionMapper {
@Mapping(target = "removeStreamItem", ignore = true)
PermissionPageResultDTO map(PageResult<Permission> page);

@Mapping(target = "_object", source = "object")
PermissionDTO map(Permission data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -59,6 +60,11 @@ public Response createOrUpdatePermission(String appId, PermissionRequestDTOV1 pe
return Response.ok().build();
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTOV1> exception(ConstraintException ex) {
return exceptionMapper.exception(ex);
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTOV1> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Item> getClasses() {
return List.of(
item(10, PermissionRequestDTOV1.class, x -> x.getClass().getSimpleName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,12 @@
@Mapper
public abstract class ExceptionMapper {

public RestResponse<ProblemDetailResponseDTOV1> 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<ProblemDetailResponseDTOV1> constraint(ConstraintViolationException ex) {
var dto = exception(ErrorKeys.CONSTRAINT_VIOLATIONS.name(), ex.getMessage());
dto.setInvalidParams(createErrorValidationResponse(ex.getConstraintViolations()));
Expand Down
4 changes: 2 additions & 2 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ components:
type: string
name:
type: string
object:
resource:
type: string
action:
type: string
Expand Down Expand Up @@ -98,7 +98,7 @@ components:
type: string
name:
type: string
object:
resource:
type: string
action:
type: string
Expand Down
4 changes: 2 additions & 2 deletions src/main/openapi/onecx-permission-operator-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ components:
Permission:
type: object
required:
- object
- resource
- action
properties:
name:
type: string
object:
resource:
type: string
action:
type: string
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/db/v1/2024-01-04-create-tables.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
<constraints nullable="false" primaryKey="true" primaryKeyName="permission_pkey"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="object" type="VARCHAR(255)"/>
<column name="resource" type="VARCHAR(255)"/>
<column name="creationuser" type="VARCHAR(255)"/>
<column name="modificationuser" type="VARCHAR(255)"/>
</createTable>

<addUniqueConstraint columnNames="app_id, object, action" constraintName="permission_key" tableName="permission"/>
<addUniqueConstraint columnNames="app_id, resource, action" constraintName="permission_key" tableName="permission"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void searchTest() {

criteria.setAppId(" ");
criteria.setName(" ");
criteria.setObject(" ");
criteria.setResource(" ");
criteria.setAction(" ");

data = given()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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.]");
}
}
14 changes: 7 additions & 7 deletions src/test/resources/data/test-internal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<dataset>

<!-- PERMISSION -->
<PERMISSION guid="p11" optlock="0" name="n1" app_id="app1" object="o1" action="a1"/>
<PERMISSION guid="p12" optlock="0" name="n1" app_id="app1" object="o1" action="a2"/>
<PERMISSION guid="p13" optlock="0" name="n1" app_id="app1" object="o1" action="a3"/>
<PERMISSION guid="p11" optlock="0" name="n1" app_id="app1" resource="o1" action="a1"/>
<PERMISSION guid="p12" optlock="0" name="n1" app_id="app1" resource="o1" action="a2"/>
<PERMISSION guid="p13" optlock="0" name="n1" app_id="app1" resource="o1" action="a3"/>

<PERMISSION guid="p14" optlock="0" name="n1" app_id="app1" object="o2" action="a2"/>
<PERMISSION guid="p15" optlock="0" name="n1" app_id="app1" object="o2" action="a3"/>
<PERMISSION guid="p14" optlock="0" name="n1" app_id="app1" resource="o2" action="a2"/>
<PERMISSION guid="p15" optlock="0" name="n1" app_id="app1" resource="o2" action="a3"/>

<PERMISSION guid="p21" optlock="0" name="n1" app_id="app2" object="o1" action="a1"/>
<PERMISSION guid="p22" optlock="0" name="n1" app_id="app2" object="o1" action="a2"/>
<PERMISSION guid="p21" optlock="0" name="n1" app_id="app2" resource="o1" action="a1"/>
<PERMISSION guid="p22" optlock="0" name="n1" app_id="app2" resource="o1" action="a2"/>
</dataset>
14 changes: 7 additions & 7 deletions src/test/resources/data/test-operator-v1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<dataset>

<!-- PERMISSION -->
<PERMISSION guid="p11" optlock="0" name="n1" app_id="app1" object="o1" action="a1"/>
<PERMISSION guid="p12" optlock="0" name="n1" app_id="app1" object="o1" action="a2"/>
<PERMISSION guid="p13" optlock="0" name="n1" app_id="app1" object="o1" action="a3"/>
<PERMISSION guid="p11" optlock="0" name="n1" app_id="app1" resource="o1" action="a1"/>
<PERMISSION guid="p12" optlock="0" name="n1" app_id="app1" resource="o1" action="a2"/>
<PERMISSION guid="p13" optlock="0" name="n1" app_id="app1" resource="o1" action="a3"/>

<PERMISSION guid="p14" optlock="0" name="n1" app_id="app1" object="o2" action="a2"/>
<PERMISSION guid="p15" optlock="0" name="n1" app_id="app1" object="o2" action="a3"/>
<PERMISSION guid="p14" optlock="0" name="n1" app_id="app1" resource="o2" action="a2"/>
<PERMISSION guid="p15" optlock="0" name="n1" app_id="app1" resource="o2" action="a3"/>

<PERMISSION guid="p21" optlock="0" name="n1" app_id="app2" object="o1" action="a1"/>
<PERMISSION guid="p22" optlock="0" name="n1" app_id="app2" object="o1" action="a2"/>
<PERMISSION guid="p21" optlock="0" name="n1" app_id="app2" resource="o1" action="a1"/>
<PERMISSION guid="p22" optlock="0" name="n1" app_id="app2" resource="o1" action="a2"/>
</dataset>

0 comments on commit 79fd061

Please sign in to comment.