Search in sources :

Example 1 with Auditable

use of com.salesmanager.core.model.common.audit.Auditable in project shopizer by shopizer-ecommerce.

the class PersistableAuditAspect method afterReturning.

@AfterReturning(value = "execution(* populate(..))", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
    try {
        if (result instanceof Auditable) {
            Auditable entity = (Auditable) result;
            AuditSection audit = entity.getAuditSection();
            if (entity.getAuditSection() == null) {
                audit = new AuditSection();
            }
            audit.setDateModified(new Date());
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null) {
                if (auth instanceof UsernamePasswordAuthenticationToken) {
                    // api only is captured
                    com.salesmanager.shop.store.security.user.JWTUser user = (com.salesmanager.shop.store.security.user.JWTUser) auth.getPrincipal();
                    audit.setModifiedBy(user.getUsername());
                }
            }
            // TODO put in log audit log trail
            entity.setAuditSection(audit);
        }
    } catch (Throwable e) {
        LOGGER.error("Error while setting audit values" + e.getMessage());
    }
}
Also used : Auditable(com.salesmanager.core.model.common.audit.Auditable) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Date(java.util.Date) Authentication(org.springframework.security.core.Authentication) AuditSection(com.salesmanager.core.model.common.audit.AuditSection) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Aggregations

AuditSection (com.salesmanager.core.model.common.audit.AuditSection)1 Auditable (com.salesmanager.core.model.common.audit.Auditable)1 Date (java.util.Date)1 AfterReturning (org.aspectj.lang.annotation.AfterReturning)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1