use of eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto in project CzechIdMng by bcvsolutions.
the class IdentityIncompatibleRoleReportExecutorIntegrationTest method testReport.
@Test
public void testReport() throws IOException {
// prepare test identities and incompatible roles
IdmIdentityDto identityOne = getHelper().createIdentity((GuardedString) null);
IdmIdentityDto identityTwo = getHelper().createIdentity((GuardedString) null);
IdmRoleDto roleOne = getHelper().createRole();
IdmRoleDto roleTwo = getHelper().createRole();
IdmRoleDto roleThree = getHelper().createRole();
IdmRoleDto roleFour = getHelper().createRole();
getHelper().createIncompatibleRole(roleOne, roleTwo);
getHelper().createIncompatibleRole(roleFour, roleThree);
getHelper().createIdentityRole(identityOne, roleOne);
getHelper().createIdentityRole(identityOne, roleTwo);
getHelper().createIdentityRole(identityOne, roleThree);
getHelper().createIdentityRole(identityTwo, roleOne);
//
// prepare report filter
RptReportDto report = new RptReportDto(UUID.randomUUID());
report.setExecutorName(reportExecutor.getName());
//
// generate report
report = reportExecutor.generate(report);
Assert.assertNotNull(report.getData());
List<RptIdentityIncompatibleRoleDto> identityIncompatibleRoles = mapper.readValue(attachmentManager.getAttachmentData(report.getData()), new TypeReference<List<RptIdentityIncompatibleRoleDto>>() {
});
//
// test
Assert.assertTrue(identityIncompatibleRoles.stream().anyMatch(i -> i.getIdentity().getId().equals(identityOne.getId()) && i.getSuperior().getId().equals(roleOne.getId()) && i.getSub().getId().equals(roleTwo.getId())));
Assert.assertFalse(identityIncompatibleRoles.stream().anyMatch(i -> i.getIdentity().getId().equals(identityTwo.getId())));
//
attachmentManager.deleteAttachments(report);
}
use of eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto in project CzechIdMng by bcvsolutions.
the class IdentityIncompatibleRoleReportExecutor method generateData.
@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
// prepare temp file for json stream
File temp = getAttachmentManager().createTempFile();
//
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
// write into json stream
JsonGenerator jGenerator = getMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
try {
// json will be array of identities
jGenerator.writeStartArray();
// form instance has useful methods to transform form values
Pageable pageable = PageRequest.of(0, 100, new Sort(Direction.ASC, IdmIdentity_.username.getName()));
//
counter = 0L;
do {
Page<IdmIdentityDto> identities = identityService.find(null, pageable, IdmBasePermission.READ);
if (count == null) {
count = identities.getTotalElements();
}
boolean canContinue = true;
for (Iterator<IdmIdentityDto> i = identities.iterator(); i.hasNext() && canContinue; ) {
IdmIdentityDto identity = i.next();
// search assigned roles
IdmIdentityRoleFilter filter = new IdmIdentityRoleFilter();
filter.setIdentityId(identity.getId());
// direct roles only
filter.setDirectRole(Boolean.TRUE);
List<IdmIdentityRoleDto> identityRoles = identityRoleService.find(filter, null, IdmBasePermission.READ).getContent();
// search incompatible roles
Set<ResolvedIncompatibleRoleDto> incompatibleRoles = incompatibleRoleService.resolveIncompatibleRoles(identityRoles.stream().map(ir -> ir.getRole()).collect(Collectors.toList()));
for (ResolvedIncompatibleRoleDto resolvedIncompatibleRole : incompatibleRoles) {
// add item into report
RptIdentityIncompatibleRoleDto reportItem = new RptIdentityIncompatibleRoleDto();
reportItem.setIdentity(identity);
reportItem.setDirectRole(resolvedIncompatibleRole.getDirectRole());
reportItem.setIncompatibleRole(resolvedIncompatibleRole.getIncompatibleRole());
// dtos in embedded cannot be parsed from json automatically as objects => aaet them into report dto directly
IdmRoleDto superior = DtoUtils.getEmbedded(resolvedIncompatibleRole.getIncompatibleRole(), IdmIncompatibleRole_.superior);
IdmRoleDto sub = DtoUtils.getEmbedded(resolvedIncompatibleRole.getIncompatibleRole(), IdmIncompatibleRole_.sub);
reportItem.setSuperior(superior);
reportItem.setSub(sub);
//
getMapper().writeValue(jGenerator, reportItem);
}
// supports cancel report generating (report extends long running task)
++counter;
canContinue = updateState();
}
// iterate while next page of identities is available
pageable = identities.hasNext() && canContinue ? identities.nextPageable() : null;
} while (pageable != null);
//
// close array of identities
jGenerator.writeEndArray();
} finally {
// close json stream
jGenerator.close();
}
// save create temp file with array of identities in json as attachment
return createAttachment(report, new FileInputStream(temp));
} catch (IOException ex) {
throw new ReportGenerateException(report.getName(), ex);
} finally {
FileUtils.deleteQuietly(temp);
}
}
use of eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto in project CzechIdMng by bcvsolutions.
the class IdentityIncompatibleRoleReportXlsxRenderer method render.
@Override
public InputStream render(RptReportDto report) {
try {
// read json stream
JsonParser jParser = getMapper().getFactory().createParser(getReportData(report));
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Report");
sheet.setDefaultColumnWidth(15);
// header
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Identity - username");
cell = row.createCell(1);
cell.setCellValue("Identity - personal number");
cell = row.createCell(2);
cell.setCellValue("Identity - lastName");
cell = row.createCell(3);
cell.setCellValue("Identity - firstName");
cell = row.createCell(4);
cell.setCellValue("Assigned role");
cell = row.createCell(5);
cell.setCellValue("Incompatible role definition - role with definition");
cell = row.createCell(6);
cell.setCellValue("Incompatible role definition - target role");
int rowNum = 1;
// json is array of identities
if (jParser.nextToken() == JsonToken.START_ARRAY) {
// write single identity
while (jParser.nextToken() == JsonToken.START_OBJECT) {
RptIdentityIncompatibleRoleDto item = getMapper().readValue(jParser, RptIdentityIncompatibleRoleDto.class);
row = sheet.createRow(rowNum++);
cell = row.createCell(0);
cell.setCellValue(item.getIdentity().getUsername());
cell = row.createCell(1);
cell.setCellValue(item.getIdentity().getExternalCode());
cell = row.createCell(2);
cell.setCellValue(item.getIdentity().getLastName());
cell = row.createCell(3);
cell.setCellValue(item.getIdentity().getFirstName());
cell = row.createCell(4);
cell.setCellValue(item.getDirectRole().getCode());
cell = row.createCell(5);
cell.setCellValue(item.getSuperior().getCode());
cell = row.createCell(6);
cell.setCellValue(item.getSub().getCode());
}
}
// close json stream
jParser.close();
// close and return input stream
return getInputStream(workbook);
} catch (IOException ex) {
throw new ReportRenderException(report.getName(), ex);
}
}
Aggregations