Search in sources :

Example 1 with IdmPassword

use of eu.bcvsolutions.idm.core.model.entity.IdmPassword 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

IdmPasswordSelfRelationWithOwnerExpression (eu.bcvsolutions.idm.core.api.audit.criteria.IdmPasswordSelfRelationWithOwnerExpression)1 IdmAuditDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)1 IdmAuditEntityDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto)1 IdmAudit (eu.bcvsolutions.idm.core.audit.entity.IdmAudit)1 IdmPassword (eu.bcvsolutions.idm.core.model.entity.IdmPassword)1 ArrayList (java.util.ArrayList)1 AuditQuery (org.hibernate.envers.query.AuditQuery)1 AuditConjunction (org.hibernate.envers.query.criteria.AuditConjunction)1 AuditDisjunction (org.hibernate.envers.query.criteria.AuditDisjunction)1 PageImpl (org.springframework.data.domain.PageImpl)1 Sort (org.springframework.data.domain.Sort)1