Search in sources :

Example 1 with AuditTrail

use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.

the class AuditAdvice method addCreateAuditEvent.

/**
 * Adds 'create' AuditEvent to audit trail of the passed AbstractAuditable.
 *
 * @param note Additional text to add to the automatically generated note.
 */
private void addCreateAuditEvent(final AbstractAuditable auditable, User user, final String note) {
    if (this.isNullOrTransient(auditable))
        return;
    AuditTrail auditTrail = auditable.getAuditTrail();
    this.ensureInSession(auditTrail);
    if (auditTrail != null && !auditTrail.getEvents().isEmpty()) {
        // while persisting parent objects. That's okay.
        if (AuditAdvice.log.isDebugEnabled())
            AuditAdvice.log.debug("Call to addCreateAuditEvent but the auditTrail already has events. AuditTrail id: " + auditTrail.getId());
        return;
    }
    String details = "Create " + auditable.getClass().getSimpleName() + " " + auditable.getId() + note;
    try {
        auditHelper.addCreateAuditEvent(auditable, details, user);
        if (AuditAdvice.log.isDebugEnabled()) {
            AuditAdvice.log.debug("Audited event: " + (note.length() > 0 ? note : "[no note]") + " on " + auditable.getClass().getSimpleName() + ":" + auditable.getId() + " by " + user.getUserName());
        }
    } catch (UsernameNotFoundException e) {
        AuditAdvice.log.warn("No user, cannot add 'create' event");
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 2 with AuditTrail

use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.

the class AuditAdvice method addUpdateAuditEvent.

private void addUpdateAuditEvent(final AbstractAuditable auditable, User user) {
    assert auditable != null;
    AuditTrail auditTrail = auditable.getAuditTrail();
    this.ensureInSession(auditTrail);
    if (auditTrail == null || auditTrail.getEvents().isEmpty()) {
        /*
             * Note: This can happen for ExperimentalFactors when loading from GEO etc. because of the bidirectional
             * association and the way we persist them. See ExpressionPersister. (actually this seems to be fixed...)
             */
        AuditAdvice.log.error("No create event for update method call on " + auditable + ", performing 'create' instead");
        this.addCreateAuditEvent(auditable, user, " - Event added on update of existing object.");
    } else {
        String note = "Updated " + auditable.getClass().getSimpleName() + " " + auditable.getId();
        auditHelper.addUpdateAuditEvent(auditable, note, user);
        if (AuditAdvice.log.isDebugEnabled()) {
            AuditAdvice.log.debug("Audited event: " + note + " on " + auditable.getClass().getSimpleName() + ":" + auditable.getId() + " by " + user.getUserName());
        }
    }
}
Also used : AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 3 with AuditTrail

use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.

the class AuditAdvice method processAssociations.

/**
 * Fills in audit trails on newly created child objects after a 'create' or 'update'. It does not add 'update'
 * events on the child objects.
 * Thus if the update is on an expression experiment that has a new Characteristic, the Characteristic will have a
 * 'create' event, and the EEE will get an added update event (via the addUpdateAuditEvent call elsewhere, not here)
 *
 * @see AclAdvice for similar code for ACLs
 */
private void processAssociations(String methodName, Object object, User user) {
    if (object instanceof AuditTrail)
        // don't audit audit trails.
        return;
    EntityPersister persister = crudUtils.getEntityPersister(object);
    if (persister == null) {
        throw new IllegalArgumentException("No persister found for " + object.getClass().getName());
    }
    CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
    String[] propertyNames = persister.getPropertyNames();
    try {
        for (int j = 0; j < propertyNames.length; j++) {
            CascadeStyle cs = cascadeStyles[j];
            String propertyName = propertyNames[j];
            if (!this.specialCaseForAssociationFollow(object, propertyName) && (this.canSkipAssociationCheck(object, propertyName) || !crudUtils.needCascade(methodName, cs))) {
                continue;
            }
            PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(object.getClass(), propertyName);
            Object associatedObject = ReflectionUtil.getProperty(object, descriptor);
            if (associatedObject == null)
                continue;
            Class<?> propertyType = descriptor.getPropertyType();
            if (AbstractAuditable.class.isAssignableFrom(propertyType)) {
                AbstractAuditable auditable = (AbstractAuditable) associatedObject;
                try {
                    this.maybeAddCascadeCreateEvent(object, auditable, user);
                    this.processAssociations(methodName, auditable, user);
                } catch (LazyInitializationException e) {
                    // be necessary.
                    if (AuditAdvice.log.isDebugEnabled())
                        AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
                }
            } else if (Collection.class.isAssignableFrom(propertyType)) {
                Collection<?> associatedObjects = (Collection<?>) associatedObject;
                try {
                    Hibernate.initialize(associatedObjects);
                    for (Object collectionMember : associatedObjects) {
                        if (AbstractAuditable.class.isAssignableFrom(collectionMember.getClass())) {
                            AbstractAuditable auditable = (AbstractAuditable) collectionMember;
                            try {
                                Hibernate.initialize(auditable);
                                this.maybeAddCascadeCreateEvent(object, auditable, user);
                                this.processAssociations(methodName, collectionMember, user);
                            } catch (LazyInitializationException e) {
                                if (AuditAdvice.log.isDebugEnabled())
                                    AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
                            // If this happens, it means the object can't be 'new' so adding audit trail can't
                            // be necessary. But keep checking.
                            }
                        }
                    }
                } catch (LazyInitializationException e) {
                    // be necessary.
                    if (AuditAdvice.log.isDebugEnabled())
                        AuditAdvice.log.debug("Caught lazy init error while processing " + object + ": " + e.getMessage() + " - skipping creation of cascade event.");
                }
            }
        }
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CascadeStyle(org.hibernate.engine.CascadeStyle) PropertyDescriptor(java.beans.PropertyDescriptor) AbstractAuditable(ubic.gemma.model.common.AbstractAuditable) JoinPoint(org.aspectj.lang.JoinPoint) InvocationTargetException(java.lang.reflect.InvocationTargetException) LazyInitializationException(org.hibernate.LazyInitializationException) Collection(java.util.Collection) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 4 with AuditTrail

use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.

the class AuditEventDaoImpl method putAllQrs.

private void putAllQrs(Map<Auditable, AuditEvent> result, List<?> qr, Map<AuditTrail, Auditable> atMap) {
    for (Object o : qr) {
        Object[] ar = (Object[]) o;
        AuditTrail t = (AuditTrail) ar[0];
        AuditEvent e = (AuditEvent) ar[1];
        // only one event per object, please - the most recent.
        if (result.containsKey(atMap.get(t)))
            continue;
        result.put(atMap.get(t), e);
    }
}
Also used : AuditEventValueObject(ubic.gemma.model.common.auditAndSecurity.AuditEventValueObject) AuditEvent(ubic.gemma.model.common.auditAndSecurity.AuditEvent) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 5 with AuditTrail

use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.

the class AuditHelperImpl method addAuditTrailIfNeeded.

private void addAuditTrailIfNeeded(Auditable auditable) {
    if (auditable.getAuditTrail() != null) {
        auditable.getAuditTrail();
        return;
    }
    // no need to persist it here
    AuditTrail auditTrail = AuditTrail.Factory.newInstance();
    auditable.setAuditTrail(auditTrail);
}
Also used : AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Aggregations

AuditTrail (ubic.gemma.model.common.auditAndSecurity.AuditTrail)12 AuditEvent (ubic.gemma.model.common.auditAndSecurity.AuditEvent)5 StopWatch (org.apache.commons.lang3.time.StopWatch)3 Auditable (ubic.gemma.model.common.Auditable)3 AuditEventValueObject (ubic.gemma.model.common.auditAndSecurity.AuditEventValueObject)3 Query (org.hibernate.Query)2 AuditEventType (ubic.gemma.model.common.auditAndSecurity.eventType.AuditEventType)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 JoinPoint (org.aspectj.lang.JoinPoint)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 CascadeStyle (org.hibernate.engine.CascadeStyle)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1 Before (org.junit.Before)1 Test (org.junit.Test)1 HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1