AuditAspect.java.ftl 7.6 KB
Newer Older
zhouweidong's avatar
zhouweidong committed
1 2 3
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
4 5 6 7 8 9 10
<#assign hasESEntity=false>
<#list sys.getAllPSDataEntities() as dataEntity>
    <#if dataEntity.getUserTag()?? && dataEntity.getUserTag()=='elasticsearch'>
        <#assign hasESEntity=true>
        <#break>
    </#if>
</#list>
zhouweidong's avatar
zhouweidong committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package ${pub.getPKGCodeName()}.util.aspect;

import lombok.SneakyThrows;
import ${pub.getPKGCodeName()}.util.annotation.Audit;
import ${pub.getPKGCodeName()}.util.domain.EntityBase;
import ${pub.getPKGCodeName()}.util.helper.DEFieldCacheMap;
import ${pub.getPKGCodeName()}.util.service.IBZDataAuditService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 实体数据审计切面类
 */
@Aspect
@Component
public class AuditAspect
{
    private final ExpressionParser parser = new SpelExpressionParser();

    @Autowired
    IBZDataAuditService dataAuditService;

    /**
     * 实体数据建立切面,在成功创建数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
     * @param point
     */
54
    @AfterReturning(value = "execution(* ${pub.getPKGCodeName()}.core.*.service.*.create(..))<#if hasESEntity>&& !execution(* ${pub.getPKGCodeName()}.core.es.service.*.create*(..))</#if>")
zhouweidong's avatar
zhouweidong committed
55
    @SneakyThrows
zhouweidong's avatar
zhouweidong committed
56 57 58 59 60
    public void create(JoinPoint point) {
        HttpServletRequest request = null;
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(requestAttributes!=null) {
            request = ((ServletRequestAttributes)requestAttributes).getRequest();
zhouweidong's avatar
zhouweidong committed
61 62
        }
        Object [] args = point.getArgs();
zhouweidong's avatar
zhouweidong committed
63
        if(ObjectUtils.isEmpty(args) || args.length==0) {
zhouweidong's avatar
zhouweidong committed
64
            return;
zhouweidong's avatar
zhouweidong committed
65
        }
zhouweidong's avatar
zhouweidong committed
66 67 68 69
        Object serviceParam = args[0];
        if(serviceParam instanceof EntityBase) {
            EntityBase entity = (EntityBase)serviceParam;
            Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
zhouweidong's avatar
zhouweidong committed
70 71
            //是否有审计属性
            if(auditFields.size()==0) {
72
                return;
zhouweidong's avatar
zhouweidong committed
73
            }
zhouweidong's avatar
zhouweidong committed
74 75 76
            String idField = DEFieldCacheMap.getDEKeyField(entity.getClass());
            Object idValue = "";
            if(!StringUtils.isEmpty(idField)) {
77 78 79
                idValue=entity.get(idField);
            }
            //记录审计日志
zhouweidong's avatar
zhouweidong committed
80
            dataAuditService.createAudit(request, entity, idValue, auditFields);
zhouweidong's avatar
zhouweidong committed
81 82 83 84 85 86 87 88
        }
    }

    /**
     * 实体数据更新切面,在成功更新数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
     * 使用环切【@Around】获取到更新前后的实体数据并进行差异比较,并将差异内容记入审计日志内
     * @param point
     */
89
    @Around("execution(* ${pub.getPKGCodeName()}.core.*.service.*.update(..))<#if hasESEntity>&& !execution(* ${pub.getPKGCodeName()}.core.es.service.*.update*(..))</#if>")
zhouweidong's avatar
zhouweidong committed
90
    public Object update(ProceedingJoinPoint point) throws Throwable {
zhouweidong's avatar
zhouweidong committed
91 92 93
        HttpServletRequest request = null;
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(requestAttributes!=null) {
zhouweidong's avatar
zhouweidong committed
94 95
            request=((ServletRequestAttributes)requestAttributes).getRequest();
        }
zhouweidong's avatar
zhouweidong committed
96 97
        Object serviceObj = point.getTarget();
        Object args[] = point.getArgs();
zhouweidong's avatar
zhouweidong committed
98
        if(ObjectUtils.isEmpty(args) || args.length==0) {
zhouweidong's avatar
zhouweidong committed
99
            return point.proceed();
zhouweidong's avatar
zhouweidong committed
100
        }
zhouweidong's avatar
zhouweidong committed
101 102 103 104
        Object arg = args[0];
        if(arg instanceof EntityBase) {
            EntityBase entity = (EntityBase) arg;
            Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
105
            //是否有审计属性
zhouweidong's avatar
zhouweidong committed
106
            if(auditFields.size()==0) {
107
                return point.proceed();
zhouweidong's avatar
zhouweidong committed
108
            }
zhouweidong's avatar
zhouweidong committed
109 110
            String idField = DEFieldCacheMap.getDEKeyField(entity.getClass());
            Object idValue = "";
111
            if(!StringUtils.isEmpty(idField)){
zhouweidong's avatar
zhouweidong committed
112
                idValue = entity.get(idField);
113
            }
zhouweidong's avatar
zhouweidong committed
114
            if(ObjectUtils.isEmpty(idValue)) {
115
                return point.proceed();
zhouweidong's avatar
zhouweidong committed
116
            }
117
            //获取更新前实体
zhouweidong's avatar
zhouweidong committed
118
            EntityBase beforeEntity = getEntity(serviceObj, idValue);
119 120 121
            //执行更新操作
            point.proceed();
            //记录审计日志
zhouweidong's avatar
zhouweidong committed
122
            dataAuditService.updateAudit(request, beforeEntity, serviceObj, idValue, auditFields);
123 124 125
            return true;
        }
        return point.proceed();
zhouweidong's avatar
zhouweidong committed
126 127 128 129 130 131 132 133 134
    }

    /**
     * 实体数据更新切面,在成功更新数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
     * 使用环切【@Around】获取要删除的完整数据,并将审计属性相关信息记录到审计日志中
     * @param point
     * @return
     * @throws Throwable
     */
135
    @Around("execution(* ${pub.getPKGCodeName()}.core.*.service.*.remove(..))<#if hasESEntity>&& !execution(* ${pub.getPKGCodeName()}.core.es.service.*.remove*(..))</#if>")
zhouweidong's avatar
zhouweidong committed
136
    public Object remove(ProceedingJoinPoint point) throws Throwable {
zhouweidong's avatar
zhouweidong committed
137 138 139 140
        HttpServletRequest request = null;
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(requestAttributes!= null) {
            request = ((ServletRequestAttributes)requestAttributes).getRequest();
zhouweidong's avatar
zhouweidong committed
141
        }
zhouweidong's avatar
zhouweidong committed
142 143
        Object serviceObj = point.getTarget();
        Object args[] = point.getArgs();
zhouweidong's avatar
zhouweidong committed
144
        if(ObjectUtils.isEmpty(args) || args.length==0) {
zhouweidong's avatar
zhouweidong committed
145
            return point.proceed();
zhouweidong's avatar
zhouweidong committed
146
        }
zhouweidong's avatar
zhouweidong committed
147 148 149 150
        Object idValue = args[0];
        EntityBase entity = getEntity(serviceObj, idValue);
        Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
        if(auditFields.size()==0) {
zhouweidong's avatar
zhouweidong committed
151 152 153 154 155 156
            return point.proceed();
        }
        else{
            //执行删除操作
            point.proceed();
            //记录审计日志
zhouweidong's avatar
zhouweidong committed
157
            dataAuditService.removeAudit(request, entity, idValue, auditFields);
zhouweidong's avatar
zhouweidong committed
158 159 160 161 162 163 164 165 166 167 168
            return true;
        }
    }

    /**
     * 获取实体
     * @param service
     * @param id
     * @return
     */
    @SneakyThrows
zhouweidong's avatar
zhouweidong committed
169 170 171
    private EntityBase getEntity(Object service, Object id) {
        EntityBase entity = null;
        if(!ObjectUtils.isEmpty(service)) {
zhouweidong's avatar
zhouweidong committed
172
            EvaluationContext oldContext = new StandardEvaluationContext();
zhouweidong's avatar
zhouweidong committed
173 174
            oldContext.setVariable("service", service);
            oldContext.setVariable("id", id);
zhouweidong's avatar
zhouweidong committed
175 176 177 178 179 180
            Expression oldExp = parser.parseExpression("#service.get(#id)");
            return oldExp.getValue(oldContext, EntityBase.class);
        }
        return entity;
    }
}