Skip to content

Commit

Permalink
feat: fixed grant and revoke and added me endpoints (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter authored Jun 3, 2024
1 parent f38399d commit 5aa4689
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void deleteByRoleId(String roleId) {
var dq = this.deleteQuery();
var root = dq.from(Assignment.class);
dq.where(cb.and(cb.equal(root.get(Assignment_.ROLE_ID), roleId),
cb.notEqual(root.get(Assignment_.MANDATORY), true)));
cb.or(cb.equal(root.get(Assignment_.MANDATORY), false), root.get(Assignment_.MANDATORY).isNull())));
this.getEntityManager().createQuery(dq).executeUpdate();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_DELETE_BY_ROLE_ID, ex);
Expand All @@ -100,7 +100,7 @@ public void deleteByPermissionId(String permissionId) {
var dq = this.deleteQuery();
var root = dq.from(Assignment.class);
dq.where(cb.and(cb.equal(root.get(Assignment_.PERMISSION).get(TraceableEntity_.ID), permissionId),
cb.notEqual(root.get(Assignment_.MANDATORY), true)));
cb.or(cb.equal(root.get(Assignment_.MANDATORY), false), root.get(Assignment_.MANDATORY).isNull())));
this.getEntityManager().createQuery(dq).executeUpdate();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_DELETE_BY_PERMISSION_ID, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ public List<Permission> findPermissionForUser(String productName, String appId,
}
}

public PageResult<Permission> findUsersPermissions(List<String> roles, int pageNumber, int pageSize) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Permission.class);
var root = cq.from(Permission.class);

Subquery<String> sq = cq.subquery(String.class);
var subRoot = sq.from(Assignment.class);
sq.select(subRoot.get(Assignment_.PERMISSION_ID));
sq.where(
subRoot.get(Assignment_.role).get(Role_.name).in(roles));
cq.where(root.get(TraceableEntity_.id).in(sq));

return createPageQuery(cq, Page.of(pageNumber, pageSize)).getPageResult();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_PERMISSION_FOR_USER, ex);
}
}

public enum ErrorKeys {

ERROR_FIND_BY_PRODUCT_NAMES_NOT_PERMISSIONS,
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public PageResult<Role> findByCriteria(RoleSearchCriteria criteria) {
}
}

public PageResult<Role> findUsersRoles(List<String> tokenRoles, int pageNumber, int pageSize) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Role.class);
var root = cq.from(Role.class);
cq.where(root.get(Role_.name).in(tokenRoles));

return createPageQuery(cq, Page.of(pageNumber, pageSize)).getPageResult();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_USER_ROLES, ex);
}
}

public List<Role> findByNames(Set<String> names) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
Expand All @@ -78,6 +91,7 @@ public List<Role> findByNames(Set<String> names) {
public enum ErrorKeys {

FIND_ENTITY_BY_ID_FAILED,
ERROR_FIND_ROLE_BY_CRITERIA;
ERROR_FIND_ROLE_BY_CRITERIA,
ERROR_FIND_USER_ROLES
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.tkit.onecx.permission.common.services.TokenService;
import org.tkit.onecx.permission.domain.daos.PermissionDAO;
import org.tkit.onecx.permission.domain.services.PermissionService;
import org.tkit.onecx.permission.rs.internal.mappers.ExceptionMapper;
import org.tkit.onecx.permission.rs.internal.mappers.PermissionMapper;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.log.cdi.LogExclude;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.permission.rs.internal.PermissionInternalApi;
import gen.org.tkit.onecx.permission.rs.internal.model.CreatePermissionRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.PermissionSearchCriteriaDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.UpdatePermissionRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.*;

@LogService
@ApplicationScoped
Expand All @@ -41,6 +40,12 @@ public class PermissionRestController implements PermissionInternalApi {
@Inject
PermissionService service;

@Inject
TokenService tokenService;

@Inject
PermissionDAO permissionDAO;

@Override
public Response createPermission(CreatePermissionRequestDTO createPermissionRequestDTO) {
var permission = mapper.create(createPermissionRequestDTO);
Expand Down Expand Up @@ -83,6 +88,15 @@ public Response updatePermission(String id, UpdatePermissionRequestDTO updatePer
return Response.ok(mapper.map(permission)).build();
}

@Override
public Response getUsersPermissions(
@LogExclude PermissionRequestDTO permissionRequestDTO) {
var roles = tokenService.getTokenRoles(permissionRequestDTO.getToken());
var permissions = permissionDAO.findUsersPermissions(roles, permissionRequestDTO.getPageNumber(),
permissionRequestDTO.getPageSize());
return Response.ok(mapper.map(permissions)).build();
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.tkit.onecx.permission.common.services.TokenService;
import org.tkit.onecx.permission.domain.daos.RoleDAO;
import org.tkit.onecx.permission.domain.services.RoleService;
import org.tkit.onecx.permission.rs.internal.mappers.ExceptionMapper;
import org.tkit.onecx.permission.rs.internal.mappers.RoleMapper;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.log.cdi.LogExclude;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.permission.rs.internal.RoleInternalApi;
import gen.org.tkit.onecx.permission.rs.internal.model.CreateRoleRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.RoleSearchCriteriaDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.UpdateRoleRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.*;

@LogService
@ApplicationScoped
Expand All @@ -42,6 +41,9 @@ public class RoleRestController implements RoleInternalApi {
@Inject
RoleService service;

@Inject
TokenService tokenService;

@Override
public Response createRole(CreateRoleRequestDTO createRoleRequestDTO) {
var role = mapper.create(createRoleRequestDTO);
Expand Down Expand Up @@ -85,6 +87,14 @@ public Response updateRole(String id, UpdateRoleRequestDTO updateRoleRequestDTO)
return Response.ok(mapper.map(role)).build();
}

@Override
public Response getUserRoles(
@LogExclude RoleRequestDTO roleRequestDTO) {
var roles = tokenService.getTokenRoles(roleRequestDTO.getToken());
var userRoles = dao.findUsersRoles(roles, roleRequestDTO.getPageNumber(), roleRequestDTO.getPageSize());
return Response.ok(mapper.mapPage(userRoles)).build();
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public interface PermissionMapper {
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "persisted", ignore = true)
void update(UpdatePermissionRequestDTO dto, @MappingTarget Permission permission);

}
82 changes: 82 additions & 0 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/roles/me:
post:
tags:
- roleInternal
operationId: getUserRoles
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/RoleRequest'
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/RolePageResult'
400:
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/permissions:
post:
tags:
Expand Down Expand Up @@ -502,6 +526,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/permissions/me:
post:
tags:
- permissionInternal
operationId: getUsersPermissions
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PermissionRequest'
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/PermissionPageResult'
400:
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/applications/search:
post:
tags:
Expand Down Expand Up @@ -529,6 +577,40 @@ paths:
$ref: '#/components/schemas/ProblemDetailResponse'
components:
schemas:
PermissionRequest:
type: object
required:
- token
properties:
token:
type: string
pageNumber:
format: int32
description: The number of page.
default: 0
type: integer
pageSize:
format: int32
description: The size of page
default: 100
type: integer
RoleRequest:
type: object
required:
- token
properties:
token:
type: string
pageNumber:
format: int32
description: The number of page.
default: 0
type: integer
pageSize:
format: int32
description: The size of page
default: 100
type: integer
AssignmentSearchCriteria:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void methodExceptionTests() {
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_BY_CRITERIA);
methodExceptionTests(() -> dao.findAllExcludingGivenIds(null),
PermissionDAO.ErrorKeys.ERROR_FIND_NOT_BY_IDS);
methodExceptionTests(() -> dao.findUsersPermissions(null, 0, 0),
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_FOR_USER);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void methodExceptionTests() {
RoleDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED);
methodExceptionTests(() -> dao.findByCriteria(null),
RoleDAO.ErrorKeys.ERROR_FIND_ROLE_BY_CRITERIA);
methodExceptionTests(() -> dao.findUsersRoles(null, 0, 0),
RoleDAO.ErrorKeys.ERROR_FIND_USER_ROLES);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ void grantAssignmentByRole() {
.then()
.statusCode(CREATED.getStatusCode());

given()
.when()
.contentType(APPLICATION_JSON)
.post("/grant/r14")
.then()
.statusCode(CREATED.getStatusCode());

var idToken = createToken("org1", List.of("n3-100"));
given()
.when()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ void getPermissionTest() {
.then()
.statusCode(NOT_FOUND.getStatusCode());
}

@Test
void getUsersPermissionsTest() {

// bearer prefix
var accessToken = createAccessTokenBearer(USER_ALICE);

var dto = given()
.contentType(APPLICATION_JSON)
.body(new PermissionRequestDTO().token(accessToken).pageNumber(0).pageSize(10))
.post("/me")
.then()
.statusCode(OK.getStatusCode())
.extract()
.body().as(PermissionPageResultDTO.class);

assertThat(dto).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,22 @@ void updateRoleWithoutBodyTest() {
Assertions.assertNotNull(exception.getInvalidParams());
Assertions.assertEquals(1, exception.getInvalidParams().size());
}

@Test
void getUserRolesTest() {

// bearer prefix
var accessToken = createAccessTokenBearer(USER_ALICE);

var dto = given()
.contentType(APPLICATION_JSON)
.body(new RoleRequestDTO().token(accessToken).pageNumber(0).pageSize(10))
.post("/me")
.then()
.statusCode(OK.getStatusCode())
.extract()
.body().as(RolePageResultDTO.class);

assertThat(dto).isNotNull();
}
}

0 comments on commit 5aa4689

Please sign in to comment.