Skip to content

Commit

Permalink
feat: add product name (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Feb 19, 2024
1 parent 2cb2419 commit 7efb907
Show file tree
Hide file tree
Showing 26 changed files with 207 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public PageResult<Application> findByCriteria(ApplicationSearchCriteria criteria
}
}

public Application loadByAppId(String appId) {
public Application loadByAppId(String productName, String appId) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Application.class);
var root = cq.from(Application.class);
cq.where(cb.equal(root.get(Application_.APP_ID), appId));
cq.where(
cb.equal(root.get(Application_.PRODUCT_NAME), productName),
cb.equal(root.get(Application_.APP_ID), appId));
return this.getEntityManager().createQuery(cq).getSingleResult();
} catch (NoResultException ne) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,8 @@ public List<Permission> findPermissionForUser(String appId, List<String> roles)
}
}

public List<Permission> findAllPermissionForUser(List<String> roles) {
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 this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_ALL_PERMISSION_FOR_USER, ex);
}
}

public enum ErrorKeys {

ERROR_FIND_ALL_PERMISSION_FOR_USER,
ERROR_FIND_PERMISSION_FOR_USER,
ERROR_LOAD_BY_APP_ID,
ERROR_FIND_PERMISSION_BY_CRITERIA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public void createAllApplications(Map<String, DataImportApplicationWrapperValueD
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
public Map<String, Permission> createAllPermissions(Map<String, Map<String, Map<String, String>>> permissions) {
public Map<String, Permission> createAllPermissions(
Map<String, Map<String, Map<String, Map<String, String>>>> permissions) {
var items = mapper.map(permissions);
permissionDAO.create(items);
return items.stream().collect(Collectors.toMap(r -> r.getAppId() + r.getResource() + r.getAction(), r -> r));
return items.stream()
.collect(Collectors.toMap(r -> r.getProductName() + r.getAppId() + r.getResource() + r.getAction(), r -> r));
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ default Map<String, Set<String>> createMapping(Map<String, DataImportRoleDTOV1>
dtoRoles.forEach((role, item) -> {
Set<String> perms = new HashSet<>();

item.getAssignments().forEach((appId, an) -> an
.forEach((resource, actions) -> actions.forEach(action -> perms.add(appId + resource + action))));
item.getAssignments()
.forEach((productName, apps) -> apps.forEach((appId, an) -> an.forEach((resource, actions) -> actions
.forEach(action -> perms.add(productName + appId + resource + action)))));

mapping.put(role, perms);
});
Expand Down Expand Up @@ -75,7 +76,7 @@ default List<Application> createApps(Map<String, DataImportApplicationWrapperVal
}
List<Application> result = new ArrayList<>();
dtos.forEach((appId, dto) -> {
var tmp = createApp(appId, dto.getName(), dto.getDescription());
var tmp = createApp(appId, dto.getName(), dto.getDescription(), dto.getProductName());
result.add(tmp);
});
return result;
Expand All @@ -90,21 +91,21 @@ default List<Application> createApps(Map<String, DataImportApplicationWrapperVal
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "description", ignore = true)
Application createApp(String appId, String name, String description);
Application createApp(String appId, String name, String description, String productName);

default List<Permission> map(Map<String, Map<String, Map<String, String>>> permissions) {
default List<Permission> map(Map<String, Map<String, Map<String, Map<String, String>>>> permissions) {
if (permissions == null) {
return List.of();
}
List<Permission> result = new ArrayList<>();
permissions.forEach((appId, perm) -> perm.forEach((resource, actions) -> actions
permissions.forEach((productName, apps) -> apps.forEach((appId, perm) -> perm.forEach((resource, actions) -> actions
.forEach((action, description) -> {
var tmp = map(appId, resource, action);
var tmp = map(appId, resource, action, productName);
if (tmp != null) {
tmp.setDescription(description);
result.add(tmp);
}
})));
}))));
return result;
}

Expand All @@ -117,5 +118,5 @@ default List<Permission> map(Map<String, Map<String, Map<String, String>>> permi
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "description", ignore = true)
Permission map(String appId, String resource, String action);
Permission map(String appId, String resource, String action, String productName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
@Setter
@Entity
@Table(name = "APPLICATION", uniqueConstraints = {
@UniqueConstraint(name = "APPLICATION_KEY", columnNames = { "APP_ID" }),
@UniqueConstraint(name = "APPLICATION_KEY", columnNames = { "PRODUCT_NAME", "APP_ID" }),
}, indexes = {
@Index(name = "APPLICATION_APP_ID", columnList = "APP_ID")
@Index(name = "APPLICATION_APP_ID", columnList = "PRODUCT_NAME,APP_ID")
})
@SuppressWarnings("squid:S2160")
public class Application extends TraceableEntity {
Expand All @@ -26,4 +26,7 @@ public class Application extends TraceableEntity {

@Column(name = "DESCRIPTION")
private String description;

@Column(name = "PRODUCT_NAME")
private String productName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
@Setter
@Entity
@Table(name = "PERMISSION", uniqueConstraints = {
@UniqueConstraint(name = "PERMISSION_KEY", columnNames = { "APP_ID", "RESOURCE", "ACTION" }),
@UniqueConstraint(name = "PERMISSION_KEY", columnNames = { "PRODUCT_NAME", "APP_ID", "RESOURCE", "ACTION" }),
}, indexes = {
@Index(name = "PERMISSION_APP_ID", columnList = "APP_ID")
@Index(name = "PERMISSION_PRODUCT_APP_ID", columnList = "PRODUCT_NAME,APP_ID")
})
@SuppressWarnings("squid:S2160")
public class Permission extends TraceableEntity {

@Column(name = "APP_ID")
private String appId;

@Column(name = "PRODUCT_NAME")
private String productName;

/**
* The permission action.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ public class PermissionRestController implements PermissionApiV1 {
ExceptionMapper exceptionMapper;

@Override
public Response getAllApplicationsPermissions(PermissionRequestDTOV1 permissionRequestDTOV1) {
var roles = tokenService.getTokenRoles(permissionRequestDTOV1.getToken());
var permissions = permissionDAO.findAllPermissionForUser(roles);
return Response.ok(mapper.create(permissions)).build();
}

@Override
public Response getApplicationPermissions(String appId, @LogExclude PermissionRequestDTOV1 permissionRequestDTOV1) {
public Response getApplicationPermissions(String productName, String appId,
@LogExclude PermissionRequestDTOV1 permissionRequestDTOV1) {
var roles = tokenService.getTokenRoles(permissionRequestDTOV1.getToken());
var permissions = permissionDAO.findPermissionForUser(appId, roles);
return Response.ok(mapper.create(appId, permissions)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ public class OperatorRestController implements PermissionOperatorApi {
@Override
@TenantExclude
@Transactional(Transactional.TxType.REQUIRED)
public Response createOrUpdatePermission(String appId, PermissionRequestDTOV1 permissionRequestDTOV1) {
public Response createOrUpdatePermission(String productName, String appId, PermissionRequestDTOV1 permissionRequestDTOV1) {

var app = applicationDAO.loadByAppId(appId);
var app = applicationDAO.loadByAppId(productName, appId);
if (app == null) {
app = mapper.createApp(permissionRequestDTOV1, appId);
app = mapper.createApp(permissionRequestDTOV1, appId, productName);
applicationDAO.create(app);
} else {
mapper.updateApp(permissionRequestDTOV1, app);
applicationDAO.update(app);
}
var data = mapper.map(permissionRequestDTOV1, appId);
var data = mapper.map(permissionRequestDTOV1, appId, productName);
if (data.isEmpty()) {
return Response.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface OperatorPermissionMapper {
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
Application createApp(PermissionRequestDTOV1 dto, String appId);
Application createApp(PermissionRequestDTOV1 dto, String appId, String productName);

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
Expand All @@ -34,19 +34,20 @@ public interface OperatorPermissionMapper {
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "appId", ignore = true)
@Mapping(target = "productName", ignore = true)
void updateApp(PermissionRequestDTOV1 dto, @MappingTarget Application app);

default List<Permission> map(PermissionRequestDTOV1 dto, String appId) {
return map(dto.getPermissions(), appId);
default List<Permission> map(PermissionRequestDTOV1 dto, String appId, String productName) {
return map(dto.getPermissions(), appId, productName);
}

default List<Permission> map(List<PermissionDTOV1> list, String appId) {
default List<Permission> map(List<PermissionDTOV1> list, String appId, String productName) {
if (list == null) {
return List.of();
}
List<Permission> data = new ArrayList<>();
for (PermissionDTOV1 dto : list) {
data.add(map(dto, appId));
data.add(map(dto, appId, productName));
}
return data;
}
Expand All @@ -59,5 +60,5 @@ default List<Permission> map(List<PermissionDTOV1> list, String appId) {
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
Permission map(PermissionDTOV1 dto, String appId);
Permission map(PermissionDTOV1 dto, String appId, String productName);
}
18 changes: 16 additions & 2 deletions src/main/openapi/onecx-permission-di-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ components:
applications:
$ref: '#/components/schemas/DataImportApplicationWrapper'
permissions:
$ref: '#/components/schemas/DataImportPermissionWrapper'
$ref: '#/components/schemas/DataImportProductWrapper'
DataImportApplicationWrapper:
type: object
nullable: false
Expand All @@ -37,8 +37,16 @@ components:
properties:
name:
type: string
productName:
type: string
description:
type: string
DataImportProductWrapper:
type: object
nullable: false
description: product name
additionalProperties:
$ref: '#/components/schemas/DataImportPermissionWrapper'
DataImportPermissionWrapper:
type: object
nullable: false
Expand Down Expand Up @@ -80,7 +88,13 @@ components:
description:
type: string
assignments:
$ref: '#/components/schemas/DataImportAssignmentWrapper'
$ref: '#/components/schemas/DataImportProductAssignmentWrapper'
DataImportProductAssignmentWrapper:
type: object
nullable: false
description: product name
additionalProperties:
$ref: '#/components/schemas/DataImportAssignmentWrapper'
DataImportAssignmentWrapper:
type: object
nullable: false
Expand Down
8 changes: 8 additions & 0 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ components:
properties:
appId:
type: string
productName:
type: string
pageNumber:
format: int32
description: The number of page.
Expand All @@ -448,6 +450,8 @@ components:
properties:
appId:
type: string
productName:
type: string
name:
type: string
pageNumber:
Expand Down Expand Up @@ -498,6 +502,8 @@ components:
type: string
appId:
type: string
productName:
type: string
name:
type: string
description:
Expand Down Expand Up @@ -540,6 +546,8 @@ components:
type: string
appId:
type: string
productName:
type: string
resource:
type: string
action:
Expand Down
8 changes: 7 additions & 1 deletion src/main/openapi/onecx-permission-operator-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ servers:
tags:
- name: permissionOperator
paths:
/operator/v1/update/{appId}:
/operator/v1/{productName}/{appId}:
put:
tags:
- permissionOperator
description: Creates or updates permission for application.
operationId: createOrUpdatePermission
parameters:
- name: productName
in: path
description: Product name
required: true
schema:
type: string
- name: appId
in: path
description: Application ID
Expand Down
37 changes: 10 additions & 27 deletions src/main/openapi/onecx-permission-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,19 @@ servers:
tags:
- name: permission
paths:
/v1/permissions/user/applications:
/v1/permissions/user/{productName}/{appId}:
post:
tags:
- permission
description: Get permission of all applications for the user
operationId: getAllApplicationsPermissions
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PermissionRequest'
responses:
200:
description: Workspace permission
content:
application/json:
schema:
$ref: '#/components/schemas/ApplicationsPermissions'
400:
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/v1/permissions/user/applications/{appId}:
post:
tags:
- permission
description: Get permissions of the application for the user
description: Get permissions of the product application for the user
operationId: getApplicationPermissions
parameters:
- name: productName
in: path
description: Product name
required: true
schema:
type: string
- name: appId
in: path
description: Application ID
Expand Down Expand Up @@ -86,6 +67,8 @@ components:
properties:
appId:
type: string
productName:
type: string
permissions:
type: object
nullable: false
Expand Down
Loading

0 comments on commit 7efb907

Please sign in to comment.