Search in sources :

Example 1 with RptIdentityIncompatibleRoleDto

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);
}
Also used : AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) RptReportDto(eu.bcvsolutions.idm.rpt.api.dto.RptReportDto) List(java.util.List) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) After(org.junit.After) InitTestDataProcessor(eu.bcvsolutions.idm.core.model.event.processor.module.InitTestDataProcessor) Qualifier(org.springframework.beans.factory.annotation.Qualifier) RptIdentityIncompatibleRoleDto(eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Assert(org.junit.Assert) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LoginService(eu.bcvsolutions.idm.core.security.api.service.LoginService) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) Before(org.junit.Before) Transactional(org.springframework.transaction.annotation.Transactional) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) RptIdentityIncompatibleRoleDto(eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto) List(java.util.List) RptReportDto(eu.bcvsolutions.idm.rpt.api.dto.RptReportDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Example 2 with RptIdentityIncompatibleRoleDto

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);
    }
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IOException(java.io.IOException) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) IdmIdentityRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityRoleFilter) FileInputStream(java.io.FileInputStream) RptIdentityIncompatibleRoleDto(eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto) Pageable(org.springframework.data.domain.Pageable) ResolvedIncompatibleRoleDto(eu.bcvsolutions.idm.core.api.dto.ResolvedIncompatibleRoleDto) FileOutputStream(java.io.FileOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Sort(org.springframework.data.domain.Sort) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto) File(java.io.File)

Example 3 with RptIdentityIncompatibleRoleDto

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);
    }
}
Also used : RptIdentityIncompatibleRoleDto(eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ReportRenderException(eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException) Row(org.apache.poi.ss.usermodel.Row) IOException(java.io.IOException) Cell(org.apache.poi.ss.usermodel.Cell) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

RptIdentityIncompatibleRoleDto (eu.bcvsolutions.idm.rpt.dto.RptIdentityIncompatibleRoleDto)3 IOException (java.io.IOException)3 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)2 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)1 ResolvedIncompatibleRoleDto (eu.bcvsolutions.idm.core.api.dto.ResolvedIncompatibleRoleDto)1 IdmIdentityRoleFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityRoleFilter)1 AttachmentManager (eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager)1 InitTestDataProcessor (eu.bcvsolutions.idm.core.model.event.processor.module.InitTestDataProcessor)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 LoginDto (eu.bcvsolutions.idm.core.security.api.dto.LoginDto)1 LoginService (eu.bcvsolutions.idm.core.security.api.service.LoginService)1 RptReportDto (eu.bcvsolutions.idm.rpt.api.dto.RptReportDto)1 ReportGenerateException (eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException)1 ReportRenderException (eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1