Search in sources :

Example 1 with IdmAuditEntityDto

use of eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto in project CzechIdMng by bcvsolutions.

the class DefaultAuditServiceIntegrationTest method testToDtoWithVersion.

@Test
public void testToDtoWithVersion() {
    IdmIdentityDto identity = getHelper().createIdentity();
    String newDescription = "description-" + System.currentTimeMillis();
    identity.setDescription(newDescription);
    identity = identityService.save(identity);
    IdmAuditFilter filter = new IdmAuditFilter();
    filter.setEntityId(identity.getId());
    filter.setWithVersion(Boolean.TRUE);
    List<IdmAuditDto> audits = auditService.find(filter, null).getContent();
    assertEquals(2, audits.size());
    for (IdmAuditDto audit : audits) {
        assertTrue(audit instanceof IdmAuditEntityDto);
        IdmAuditEntityDto auditEntity = (IdmAuditEntityDto) audit;
        assertNotNull(auditEntity.getEntity());
        // Check attribute for MOD
        if (auditEntity.getModification().equals("MOD")) {
            assertTrue(auditEntity.getEntity().containsKey(IdmIdentity_.description.getName()));
            Object description = auditEntity.getEntity().get(IdmIdentity_.description.getName());
            assertNotNull(description);
            assertEquals(newDescription, description);
        }
    }
}
Also used : IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto) IdmAuditDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto) IdmAuditFilter(eu.bcvsolutions.idm.core.api.audit.dto.filter.IdmAuditFilter) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with IdmAuditEntityDto

use of eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto in project CzechIdMng by bcvsolutions.

the class IdmAuditController method loadEmbeddedEntity.

/**
 * Fills referenced entity to dto - prevent to load entity for each row.
 *
 * @param dto
 */
private void loadEmbeddedEntity(Map<UUID, BaseDto> loadedDtos, IdmAuditDto dto) {
    UUID entityId = dto.getEntityId();
    if (entityId == null || StringUtils.isEmpty(dto.getType())) {
        // just for sure - IdmAudit entity doesn't specify it as required (but it should be)
        return;
    }
    // set context - add additional common props
    DataFilter context = new DataFilter(null);
    context.set(IdmFormValueFilter.PARAMETER_ADD_OWNER_DTO, Boolean.TRUE);
    BaseDto revision = null;
    if (loadedDtos.containsKey(entityId)) {
        revision = loadedDtos.get(entityId);
    } else {
        try {
            BaseEntity revisionEntity = getLookupService().lookupEntity(dto.getType(), entityId);
            if (revisionEntity != null) {
                revision = getLookupService().toDto(revisionEntity, null, context);
            }
            loadedDtos.put(entityId, revision);
        } catch (IllegalArgumentException ex) {
            LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getType(), ex);
        } catch (Exception ex) {
            LOG.debug("Type [{}] cannot be mapped to dto.", dto.getType(), ex);
        }
    }
    // nullable
    dto.getEmbedded().put(IdmAudit_.entityId.getName(), revision);
    // try to load last revision for deleted entity - main table only ~ subowner will not be solved
    if (revision == null) {
        dto.setDeleted(true);
        try {
            Object lastPersistedVersion = auditService.findLastPersistedVersion(Class.forName(dto.getType()), entityId);
            if (lastPersistedVersion != null) {
                dto.getEmbedded().put(IdmAudit_.entityId.getName(), getLookupService().toDto((BaseEntity) lastPersistedVersion, null, context));
            }
        } catch (IllegalArgumentException | ClassNotFoundException ex) {
            LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getType(), ex);
        } catch (Exception ex) {
            LOG.debug("Type [{}] cannot be mapped to dto.", dto.getType(), ex);
        }
    }
    // For subowner, some entities doesn't support owner and subowner.
    if (dto.getSubOwnerId() != null) {
        try {
            UUID subOwnerId = UUID.fromString(dto.getSubOwnerId());
            if (!loadedDtos.containsKey(subOwnerId)) {
                loadedDtos.put(subOwnerId, getLookupService().lookupDto(dto.getSubOwnerType(), subOwnerId));
            }
            dto.getEmbedded().put(IdmAudit_.subOwnerId.getName(), loadedDtos.get(subOwnerId));
        } catch (IllegalArgumentException ex) {
            LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getSubOwnerType(), ex);
        } catch (Exception ex) {
            LOG.debug("Type [{}] cannot be mapped to dto.", dto.getSubOwnerId(), ex);
        }
    }
    // For owner, some entities doesn't support owner and subowner.
    if (dto.getOwnerId() != null) {
        try {
            UUID ownerId = UUID.fromString(dto.getOwnerId());
            if (!loadedDtos.containsKey(ownerId)) {
                loadedDtos.put(ownerId, getLookupService().lookupDto(dto.getOwnerType(), ownerId));
            }
            dto.getEmbedded().put(IdmAudit_.ownerId.getName(), loadedDtos.get(ownerId));
        } catch (IllegalArgumentException ex) {
            LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getSubOwnerType(), ex);
        } catch (Exception ex) {
            LOG.debug("Entity [{}] cannot be mapped to dto.", dto.getOwnerId(), ex);
        }
    }
    // Fill embedded contract for FE agenda (prevent to load contract for each row).
    if ((dto instanceof IdmAuditEntityDto) && dto.getType().equals(IdmIdentityRole.class.getCanonicalName())) {
        IdmAuditEntityDto auditEntity = (IdmAuditEntityDto) dto;
        if (auditEntity.getEntity().containsKey(IdmIdentityRoleDto.PROPERTY_IDENTITY_CONTRACT) && !auditEntity.getEmbedded().containsKey(IdmIdentityRoleDto.PROPERTY_IDENTITY_CONTRACT)) {
            UUID contractId = DtoUtils.toUuid(auditEntity.getEntity().get(IdmIdentityRoleDto.PROPERTY_IDENTITY_CONTRACT));
            if (contractId != null) {
                if (!loadedDtos.containsKey(contractId)) {
                    loadedDtos.put(contractId, getLookupService().lookupDto(IdmIdentityContractDto.class, contractId));
                }
                auditEntity.getEmbedded().put(IdmIdentityRoleDto.PROPERTY_IDENTITY_CONTRACT, loadedDtos.get(contractId));
            }
        }
    }
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto) BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) UUID(java.util.UUID) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 3 with IdmAuditEntityDto

use of eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto in project CzechIdMng by bcvsolutions.

the class DefaultAuditServiceIntegrationTest method testToDtoWithoutVersion.

@Test
public void testToDtoWithoutVersion() {
    IdmIdentityDto identity = getHelper().createIdentity();
    identity.setDescription("description-" + System.currentTimeMillis());
    identity = identityService.save(identity);
    IdmAuditFilter filter = new IdmAuditFilter();
    filter.setEntityId(identity.getId());
    List<IdmAuditDto> audits = auditService.find(filter, null).getContent();
    assertEquals(2, audits.size());
    for (IdmAuditDto audit : audits) {
        assertFalse(audit instanceof IdmAuditEntityDto);
    }
    filter = new IdmAuditFilter();
    filter.setEntityId(identity.getId());
    filter.setWithVersion(Boolean.FALSE);
    audits = auditService.find(filter, null).getContent();
    assertEquals(2, audits.size());
    for (IdmAuditDto audit : audits) {
        assertFalse(audit instanceof IdmAuditEntityDto);
    }
}
Also used : IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto) IdmAuditDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto) IdmAuditFilter(eu.bcvsolutions.idm.core.api.audit.dto.filter.IdmAuditFilter) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with IdmAuditEntityDto

use of eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method toDto.

@Override
protected IdmAuditDto toDto(IdmAudit entity, IdmAuditDto dto, IdmAuditFilter filter) {
    if (filter != null && BooleanUtils.isTrue(filter.getWithVersion())) {
        Class<?> forName;
        try {
            forName = Class.forName(entity.getType());
        } catch (ClassNotFoundException e) {
            LOG.warn("Class for type [{}], doesn't exists.", entity.getType(), e);
            return super.toDto(entity, dto, filter);
        }
        Object findVersion = findVersion(forName, entity.getEntityId(), Long.valueOf(entity.getId().toString()));
        // For delete operation is current version null, we must find the last one
        if (findVersion == null) {
            findVersion = this.findPreviousVersion(Long.valueOf(entity.getId().toString()));
        }
        if (findVersion != null) {
            IdmAuditEntityDto newDto = (IdmAuditEntityDto) super.toDto(entity, new IdmAuditEntityDto(), filter);
            newDto.setEntity(getValuesFromVersion(findVersion));
            return newDto;
        }
    }
    return super.toDto(entity, dto, filter);
}
Also used : IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto)

Example 5 with IdmAuditEntityDto

use of eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method findLogin.

@Override
public Page<IdmAuditDto> findLogin(IdmAuditFilter filter, Pageable pageable) {
    // 
    if (pageable == null) {
        // pageable is required noe
        pageable = PageRequest.of(0, Integer.MAX_VALUE);
    }
    // Create audit query for specific login audit
    // Conjunction solve connection between successful and failed query
    AuditConjunction conjunction = AuditEntity.conjunction();
    // Disjunctions solves connection between each query condition, there are two conditions for successful and failed logins
    AuditConjunction conjunctionForSuccessful = AuditEntity.conjunction();
    AuditConjunction conjunctionForFailed = AuditEntity.conjunction();
    AuditDisjunction disjunction = AuditEntity.disjunction();
    conjunctionForFailed.add(AuditEntity.revisionProperty(IdmAudit_.changedAttributes.getName()).ilike(IdmPassword_.unsuccessfulAttempts.getName(), MatchMode.ANYWHERE));
    conjunctionForFailed.add(AuditEntity.property(IdmPassword_.modifier.getName()).eq(SecurityService.GUEST_NAME));
    conjunctionForSuccessful.add(AuditEntity.revisionProperty(IdmAudit_.changedAttributes.getName()).ilike(IdmPassword_.lastSuccessfulLogin.getName(), MatchMode.ANYWHERE));
    // Self created relation, created by expression
    conjunctionForSuccessful.add(new IdmPasswordSelfRelationWithOwnerExpression());
    disjunction.add(conjunctionForSuccessful);
    disjunction.add(conjunctionForFailed);
    conjunction.add(disjunction);
    if (StringUtils.isNotEmpty(filter.getOwnerId())) {
        // 'coleration' attribute for connection with IdmAudit entity - ownerId
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.ownerId.getName()).eq(filter.getOwnerId()));
    }
    if (StringUtils.isNotEmpty(filter.getOwnerCode())) {
        // 'coleration' attribute for connection with IdmAudit entity - ownerCode
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.ownerCode.getName()).eq(filter.getOwnerCode()));
    }
    if (filter.getFrom() != null) {
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.timestamp.getName()).ge(filter.getFrom().toInstant().toEpochMilli()));
    }
    if (filter.getTill() != null) {
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.timestamp.getName()).le(filter.getTill().toInstant().toEpochMilli()));
    }
    // Count is for pageable and check if is required made query
    Object count = this.getAuditReader().createQuery().forRevisionsOfEntity(IdmPassword.class, false, true).add(conjunction).addProjection(AuditEntity.id().count()).getSingleResult();
    Long countAsLong = null;
    if (count instanceof Long) {
        countAsLong = (Long) count;
    }
    // Count is zero. Count is for queries better than real query
    if (countAsLong == null || countAsLong == 0) {
        return new PageImpl<IdmAuditDto>(Collections.emptyList(), pageable, 0);
    }
    // Create final query and solve pagination and order
    AuditQuery query = this.getAuditReader().createQuery().forRevisionsOfEntity(IdmPassword.class, false, true).add(conjunction);
    int maxResults = pageable.getPageSize();
    int firstResult = pageable.getPageSize() * pageable.getPageNumber();
    query.setMaxResults(maxResults).setFirstResult(firstResult);
    Sort sort = pageable.getSort();
    if (sort != null) {
        sort.forEach(order -> {
            AuditProperty<Object> property = AuditEntity.revisionProperty(order.getProperty());
            if (order.isAscending()) {
                query.addOrder(property.asc());
            } else {
                query.addOrder(property.desc());
            }
        });
    }
    // Returned list contains three object IdmAudit, Version (IdmPassword) and type of modification
    @SuppressWarnings("unchecked") List<Object[]> resultList = query.getResultList();
    // We doesn't need made again get for version, because version is in result form audit query
    filter.setWithVersion(Boolean.FALSE);
    // Iterate over all result and transform it into dtos
    List<IdmAuditDto> result = new ArrayList<>(resultList.size());
    for (Object[] object : resultList) {
        Object version = object[PROPERTY_AUDIT_VERSION];
        IdmAudit entity = (IdmAudit) object[PROPERTY_AUDIT_ENTITY];
        IdmAuditEntityDto newDto = (IdmAuditEntityDto) super.toDto(entity, new IdmAuditEntityDto(), filter);
        newDto.setEntity(getValuesFromVersion(version));
        result.add(newDto);
    }
    return new PageImpl<IdmAuditDto>(result, pageable, countAsLong);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) IdmPassword(eu.bcvsolutions.idm.core.model.entity.IdmPassword) AuditQuery(org.hibernate.envers.query.AuditQuery) IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto) AuditConjunction(org.hibernate.envers.query.criteria.AuditConjunction) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) IdmAudit(eu.bcvsolutions.idm.core.audit.entity.IdmAudit) IdmAuditDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto) ArrayList(java.util.ArrayList) IdmPasswordSelfRelationWithOwnerExpression(eu.bcvsolutions.idm.core.api.audit.criteria.IdmPasswordSelfRelationWithOwnerExpression) Sort(org.springframework.data.domain.Sort)

Aggregations

IdmAuditEntityDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto)5 IdmAuditDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)3 IdmAuditFilter (eu.bcvsolutions.idm.core.api.audit.dto.filter.IdmAuditFilter)2 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 Test (org.junit.Test)2 IdmPasswordSelfRelationWithOwnerExpression (eu.bcvsolutions.idm.core.api.audit.criteria.IdmPasswordSelfRelationWithOwnerExpression)1 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)1 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)1 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)1 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 IdmAudit (eu.bcvsolutions.idm.core.audit.entity.IdmAudit)1 IdmPassword (eu.bcvsolutions.idm.core.model.entity.IdmPassword)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 AuditQuery (org.hibernate.envers.query.AuditQuery)1 AuditConjunction (org.hibernate.envers.query.criteria.AuditConjunction)1 AuditDisjunction (org.hibernate.envers.query.criteria.AuditDisjunction)1