Skip to content

Commit

Permalink
feat: refactoring permission model
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Feb 12, 2024
1 parent a1105f2 commit 580b5de
Show file tree
Hide file tree
Showing 31 changed files with 50 additions and 1,839 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ public class PermissionImportService {
@Inject
RoleDAO roleDAO;

@Inject
WorkspacePermissionDAO workspacePermissionDAO;

@Inject
WorkspaceAssignmentDAO workspaceAssignmentDAO;

@Inject
DataImportV1Mapper mapper;

Expand All @@ -52,8 +46,6 @@ public void deleteAllData(String tenantId) {
ApplicationContext.start(ctx);

assignmentDAO.deleteQueryAll();
workspaceAssignmentDAO.deleteQueryAll();
workspacePermissionDAO.deleteQueryAll();
roleDAO.deleteQueryAll();

} finally {
Expand Down Expand Up @@ -90,12 +82,6 @@ public void createTenantData(String tenantId, DataImportTenantWrapperDTOV1 dto,

ApplicationContext.start(ctx);

// create workspace permissions
var workspacePermissions = mapper.mapWorkspace(dto.getWorkspacesPermissions());
workspacePermissionDAO.create(workspacePermissions);
var workspacePermissionsMap = workspacePermissions.stream()
.collect(Collectors.toMap(r -> r.getWorkspaceId() + r.getResource() + r.getAction(), r -> r));

// create tenant roles
var roles = mapper.createRoles(dto.getRoles());
roleDAO.create(roles);
Expand All @@ -106,11 +92,6 @@ public void createTenantData(String tenantId, DataImportTenantWrapperDTOV1 dto,
var assignments = mapper.createAssignments(mapping, rolesMap, permissionMap);
assignmentDAO.create(assignments);

// create tenant workspace assignments
var workspaceMapping = mapper.createWorkspaceMapping(dto.getRoles());
var workspaceAssignments = mapper.createWorkspaceAssignments(workspaceMapping, rolesMap, workspacePermissionsMap);
workspaceAssignmentDAO.create(workspaceAssignments);

} finally {
ApplicationContext.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,6 @@ default Map<String, Set<String>> createMapping(Map<String, DataImportRoleDTOV1>
return mapping;
}

default Map<String, Set<String>> createWorkspaceMapping(Map<String, DataImportRoleDTOV1> dtoRoles) {
if (dtoRoles == null || dtoRoles.isEmpty()) {
return Map.of();
}
Map<String, Set<String>> mapping = new HashMap<>();
dtoRoles.forEach((role, item) -> {
Set<String> perms = new HashSet<>();

item.getWorkspacesAssignments().forEach((workspaceId, an) -> an
.forEach((resource, actions) -> actions.forEach(action -> perms.add(workspaceId + resource + action))));

mapping.put(role, perms);
});
return mapping;
}

default List<WorkspaceAssignment> createWorkspaceAssignments(Map<String, Set<String>> mapping, Map<String, Role> roles,
Map<String, WorkspacePermission> permissions) {
if (permissions == null || roles == null || mapping == null) {
return List.of();
}

List<WorkspaceAssignment> result = new ArrayList<>();
mapping.forEach((role, perms) -> perms.forEach(perm -> result.add(create(roles.get(role), permissions.get(perm)))));
return result;
}

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "creationUser", ignore = true)
Expand All @@ -78,19 +51,6 @@ default List<WorkspaceAssignment> createWorkspaceAssignments(Map<String, Set<Str
@Mapping(target = "tenantId", ignore = true)
Assignment createAssignment(Role role, Permission permission);

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "creationUser", ignore = true)
@Mapping(target = "modificationDate", ignore = true)
@Mapping(target = "modificationUser", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "roleId", ignore = true)
@Mapping(target = "permissionId", ignore = true)
@Mapping(target = "tenantId", ignore = true)
WorkspaceAssignment create(Role role, WorkspacePermission permission);

default List<Role> createRoles(Map<String, DataImportRoleDTOV1> dto) {
if (dto == null) {
return List.of();
Expand All @@ -109,34 +69,6 @@ default List<Role> createRoles(Map<String, DataImportRoleDTOV1> dto) {
@Mapping(target = "tenantId", ignore = true)
Role create(String name, String description);

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

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "creationUser", ignore = true)
@Mapping(target = "modificationDate", ignore = true)
@Mapping(target = "modificationUser", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "description", ignore = true)
WorkspacePermission mapWorkspace(String workspaceId, String resource, String action);

default List<Application> createApps(Map<String, DataImportApplicationWrapperValueDTOV1> dtos) {
if (dtos == null) {
return List.of();
Expand Down

This file was deleted.

Loading

0 comments on commit 580b5de

Please sign in to comment.