Search in sources :

Example 11 with AbstractAuthentication

use of eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication in project CzechIdMng by bcvsolutions.

the class AuditableEntityListener method touchForCreate.

/**
 * Sets creation date and creator on the target object in case it implements {@link Auditable} on
 * persist events.
 *
 * @param target
 */
@PrePersist
public void touchForCreate(Object target) {
    if (!(target instanceof Auditable)) {
        return;
    }
    // 
    AutowireHelper.autowire(this, this.securityService);
    // 
    ZonedDateTime date = ZonedDateTime.now();
    Auditable entity = (Auditable) target;
    // set created date - can be preset (e.g. preserve created date of original operation moved some where info archive)
    if (entity.getCreated() == null) {
        entity.setCreated(date);
    }
    // 
    AbstractAuthentication authentication = securityService.getAuthentication();
    IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
    IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
    if (entity.getCreator() == null) {
        String creator = currentIdentity == null ? securityService.getUsername() : currentIdentity.getUsername();
        entity.setCreator(creator);
        // 
        UUID creatorId = currentIdentity == null ? null : currentIdentity.getId();
        entity.setCreatorId(creatorId);
    }
    // could be filled in wf (applicant) ...
    if (entity.getOriginalCreator() == null) {
        String originalCreator = originalIdentity == null ? null : originalIdentity.getUsername();
        entity.setOriginalCreator(originalCreator);
        // 
        UUID originalCreatorId = originalIdentity == null ? null : originalIdentity.getId();
        entity.setOriginalCreatorId(originalCreatorId);
    }
    // set transaction id from context holder
    entity.setTransactionId(TransactionContextHolder.getContext().getTransactionId());
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) ZonedDateTime(java.time.ZonedDateTime) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) PrePersist(javax.persistence.PrePersist)

Example 12 with AbstractAuthentication

use of eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication in project CzechIdMng by bcvsolutions.

the class IdmAuditListener method changeRevisionEntity.

private void changeRevisionEntity(Class<AbstractEntity> entityClass, String entityName, UUID entityId, IdmAudit revisionEntity, RevisionType revisionType) {
    // name of entity class - full name.
    revisionEntity.setType(entityName);
    // revision type - MOD, DEL, ADD
    revisionEntity.setModification(revisionType.name());
    // action executer identity
    AbstractAuthentication authentication = securityService.getAuthentication();
    IdmIdentityDto currentIdentity = authentication == null ? null : authentication.getCurrentIdentity();
    IdmIdentityDto originalIdentity = authentication == null ? null : authentication.getOriginalIdentity();
    // 
    revisionEntity.setModifier(securityService.getUsername());
    revisionEntity.setModifierId(currentIdentity == null ? null : currentIdentity.getId());
    // original action executer identity (before switch)
    revisionEntity.setOriginalModifier(securityService.getOriginalUsername());
    revisionEntity.setOriginalModifierId(originalIdentity == null ? null : originalIdentity.getId());
    // entity id
    revisionEntity.setEntityId((UUID) entityId);
    // 
    AbstractEntity currentEntity = null;
    if (revisionType == RevisionType.DEL) {
        currentEntity = auditService.getActualRemovedEntity(entityClass, entityId);
    } else {
        currentEntity = (AbstractEntity) entityManger.find(entityClass, entityId);
    }
    if (currentEntity instanceof AuditSearchable) {
        AuditSearchable searchableEntity = ((AuditSearchable) currentEntity);
        revisionEntity.setOwnerCode(searchableEntity.getOwnerCode());
        revisionEntity.setOwnerId(searchableEntity.getOwnerId());
        revisionEntity.setOwnerType(searchableEntity.getOwnerType());
        revisionEntity.setSubOwnerCode(searchableEntity.getSubOwnerCode());
        revisionEntity.setSubOwnerId(searchableEntity.getSubOwnerId());
        revisionEntity.setSubOwnerType(searchableEntity.getSubOwnerType());
    } else if (currentEntity instanceof Codeable) {
        revisionEntity.setOwnerCode(((Codeable) currentEntity).getCode());
    }
    // transaction id
    revisionEntity.setTransactionId(TransactionContextHolder.getContext().getTransactionId());
}
Also used : AuditSearchable(eu.bcvsolutions.idm.core.api.domain.AuditSearchable) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) AbstractEntity(eu.bcvsolutions.idm.core.api.entity.AbstractEntity) AbstractAuthentication(eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Aggregations

AbstractAuthentication (eu.bcvsolutions.idm.core.security.api.domain.AbstractAuthentication)12 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)8 Auditable (eu.bcvsolutions.idm.core.api.domain.Auditable)4 UUID (java.util.UUID)4 AuditSearchable (eu.bcvsolutions.idm.core.api.domain.AuditSearchable)2 Codeable (eu.bcvsolutions.idm.core.api.domain.Codeable)2 AbstractEntity (eu.bcvsolutions.idm.core.api.entity.AbstractEntity)2 IdmRole (eu.bcvsolutions.idm.core.model.entity.IdmRole)2 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTime (org.joda.time.DateTime)2 Test (org.junit.Test)2 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)1 IdmRoleGuarantee (eu.bcvsolutions.idm.core.model.entity.IdmRoleGuarantee)1 IdmRoleGuaranteeRole (eu.bcvsolutions.idm.core.model.entity.IdmRoleGuaranteeRole)1 LocalDate (java.time.LocalDate)1 PrePersist (javax.persistence.PrePersist)1 PreUpdate (javax.persistence.PreUpdate)1 Predicate (javax.persistence.criteria.Predicate)1 Transactional (org.springframework.transaction.annotation.Transactional)1