Skip to content

Commit

Permalink
feat: added exim for ui (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter authored Sep 18, 2024
1 parent 11af5a4 commit 38ac1e9
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 5 deletions.
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.onecx.permission.domain.daos.AssignmentDAO;
import org.tkit.onecx.permission.domain.daos.PermissionDAO;
import org.tkit.onecx.permission.domain.daos.RoleDAO;
import org.tkit.onecx.permission.domain.models.Role;
Expand All @@ -22,6 +23,7 @@
import gen.org.tkit.onecx.permission.rs.exim.v1.model.AssignmentSnapshotDTOV1;
import gen.org.tkit.onecx.permission.rs.exim.v1.model.EximProblemDetailInvalidParamDTOV1;
import gen.org.tkit.onecx.permission.rs.exim.v1.model.EximProblemDetailResponseDTOV1;
import gen.org.tkit.onecx.permission.rs.exim.v1.model.ExportAssignmentsRequestDTOV1;

@LogService
@ApplicationScoped
Expand All @@ -42,6 +44,20 @@ public class PermissionExportImportV1 implements PermissionExportImportApi {
@Inject
AssignmentService service;

@Inject
AssignmentDAO assignmentDAO;

@Override
public Response exportAssignments(ExportAssignmentsRequestDTOV1 exportAssignmentsRequestDTOV1) {
var permissionActions = assignmentDAO.findPermissionActionForProducts(exportAssignmentsRequestDTOV1.getProductNames());
return Response.ok(mapper.createSnapshot(permissionActions)).build();
}

@Override
public Response importAssignments(AssignmentSnapshotDTOV1 assignmentSnapshotDTOV1) {
return operatorImportAssignments(assignmentSnapshotDTOV1);
}

@Override
public Response operatorImportAssignments(AssignmentSnapshotDTOV1 assignmentSnapshotDTO) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.tkit.onecx.permission.rs.exim.v1.mappers;

import java.time.OffsetDateTime;
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.PermissionAction;
import org.tkit.onecx.permission.domain.models.Role;

import gen.org.tkit.onecx.permission.rs.exim.v1.model.AssignmentSnapshotDTOV1;
Expand Down Expand Up @@ -125,4 +127,32 @@ static String permId(String productName, String appId, String resource, String a

record RequestData(Map<String, List<String>> product, Set<String> roles) {
}

default AssignmentSnapshotDTOV1 createSnapshot(List<PermissionAction> items) {
AssignmentSnapshotDTOV1 assignmentSnapshotDTOV1 = new AssignmentSnapshotDTOV1();
assignmentSnapshotDTOV1.setId(UUID.randomUUID().toString());
assignmentSnapshotDTOV1.setCreated(OffsetDateTime.now());
assignmentSnapshotDTOV1.setAssignments(createSnapshotAssignments(items));
return assignmentSnapshotDTOV1;
}

default Map<String, Map<String, Map<String, Map<String, List<String>>>>> createSnapshotAssignments(
List<PermissionAction> items) {

if (items == null) {
return Map.of();
}
Map<String, Map<String, Map<String, Map<String, List<String>>>>> result = new HashMap<>();

for (PermissionAction permissionAction : items) {
result
.computeIfAbsent(permissionAction.productName(), k -> new HashMap<>())
.computeIfAbsent(permissionAction.applicationId(), k -> new HashMap<>())
.computeIfAbsent(permissionAction.roleName(), k -> new HashMap<>())
.computeIfAbsent(permissionAction.resource(), k -> new ArrayList<>())
.add(permissionAction.action());
}

return result;
}
}
60 changes: 60 additions & 0 deletions src/main/openapi/onecx-permission-exim-v1-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,58 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/EximProblemDetailResponse'
/exim/v1/assignments/import:
post:
security:
- oauth2: [ ocx-pm:write ]
tags:
- permissionExportImport
description: Operator import assignments
operationId: importAssignments
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AssignmentSnapshot'
responses:
"200":
description: Import successful
"409":
description: The request could not be fully completed due to a conflict with the current state of the roles and permissions
content:
application/json:
schema:
$ref: '#/components/schemas/EximProblemDetailResponse'
"400":
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/EximProblemDetailResponse'
/exim/v1/assignments/export:
post:
security:
- oauth2: [ ocx-pm:read ]
tags:
- permissionExportImport
description: Operator import assignments
operationId: exportAssignments
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ExportAssignmentsRequest'
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/AssignmentSnapshot'
"404":
description: No helps found
components:
securitySchemes:
oauth2:
Expand Down Expand Up @@ -83,6 +135,14 @@ components:
type: array
items:
type: string
ExportAssignmentsRequest:
type: object
properties:
productNames:
type: array
uniqueItems: true
items:
type: string
EximProblemDetailResponse:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.tkit.onecx.permission.rs.exim.v1.mappers.EximExceptionMapperV1;
Expand All @@ -18,6 +19,7 @@

import gen.org.tkit.onecx.permission.rs.exim.v1.model.AssignmentSnapshotDTOV1;
import gen.org.tkit.onecx.permission.rs.exim.v1.model.EximProblemDetailResponseDTOV1;
import gen.org.tkit.onecx.permission.rs.exim.v1.model.ExportAssignmentsRequestDTOV1;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;

Expand All @@ -36,7 +38,7 @@ void operatorImportNullProductTest() {
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(request)
.post()
.post("/operator")
.then().log().all()
.statusCode(OK.getStatusCode());
}
Expand All @@ -52,7 +54,7 @@ void operatorImportNullAppTest() {
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(request)
.post()
.post("/operator")
.then().log().all()
.statusCode(OK.getStatusCode());
}
Expand All @@ -70,7 +72,7 @@ void operatorImportTest() {
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(request)
.post()
.post("/operator")
.then().log().all()
.statusCode(OK.getStatusCode());
}
Expand All @@ -86,7 +88,7 @@ void operatorImportMissingDataTest() {
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(request)
.post()
.post("/operator")
.then().log().all()
.statusCode(CONFLICT.getStatusCode())
.extract()
Expand All @@ -105,7 +107,7 @@ void operatorImportEmptyBodyTest() {
var dto = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.post()
.post("/operator")
.then().log().all()
.statusCode(BAD_REQUEST.getStatusCode())
.extract()
Expand All @@ -117,4 +119,27 @@ void operatorImportEmptyBodyTest() {
"operatorImportAssignments.assignmentSnapshotDTOV1: must not be null");
}

@Test
void exportImportTest() {
var exportRequest = new ExportAssignmentsRequestDTOV1().productNames(Set.of("test1"));
var dto = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(exportRequest)
.post("/export")
.then().log().all()
.statusCode(OK.getStatusCode())
.extract()
.body().as(AssignmentSnapshotDTOV1.class);
assertThat(dto).isNotNull();

//snapshot should be importable
given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(dto)
.post("/import")
.then().log().all()
.statusCode(OK.getStatusCode());
}
}
6 changes: 6 additions & 0 deletions src/test/resources/data/test-exim-v1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@
<ASSIGNMENT guid="a11" optlock="0" permission_id="p13" role_id="r13" tenant_id="default"/>
<ASSIGNMENT guid="a12" optlock="0" permission_id="p13" role_id="r14" tenant_id="default"/>

<ASSIGNMENT guid="a13" optlock="0" permission_id="p14" role_id="r14" tenant_id="default"/>
<ASSIGNMENT guid="a14" optlock="0" permission_id="p14" role_id="r13" tenant_id="default"/>
<ASSIGNMENT guid="a15" optlock="0" permission_id="p22" role_id="r14" tenant_id="default"/>
<ASSIGNMENT guid="a16" optlock="0" permission_id="p15" role_id="r13" tenant_id="default"/>


</dataset>

0 comments on commit 38ac1e9

Please sign in to comment.