DataAuthAspect.java 2.83 KB
Newer Older
licc's avatar
licc committed
1 2 3 4 5 6 7
package cn.wisenergy.web.aspect;

import cn.wisenergy.common.annotation.DataAuth;
import cn.wisenergy.common.constant.CommonConstants;
import cn.wisenergy.common.enums.RespCodeEnum;
import cn.wisenergy.common.utils.exception.Result;
import cn.wisenergy.model.app.AccountInfo;
licc's avatar
licc committed
8
import cn.wisenergy.service.app.AccountService;
licc's avatar
licc committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 数据权限,切面处理类
 */
@Aspect
@Component
public class DataAuthAspect {
    @Autowired
licc's avatar
licc committed
29
    AccountService sysUserService;
licc's avatar
licc committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    @Pointcut("@annotation(cn.wisenergy.common.annotation.DataAuth)")
    public void dataPointCut() {
    }

    @Around("dataPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        // 获取方法上的注解
        DataAuth dataAuth = method.getAnnotation(DataAuth.class);
        if (dataAuth != null) {
            // 获取注解值
            String mapKey = dataAuth.value();
            // 获取请求参数
            Object[] args = pjp.getArgs();
            for (Object obj : args) {
                if (obj instanceof Map) {
                    Map<String, Object> map = (Map<String, Object>) obj;
                    // 获取当前用户
                    AccountInfo sysUser = (AccountInfo) SecurityUtils.getSubject().getPrincipal();
                    if (sysUser != null) {
                        // 超级管理员返回全量数据
                        if (!StringUtils.equals(sysUser.getId().toString(), CommonConstants.SUPER_ADMIN)) {
                            // 查询用户的数据权限信息,如果存在数据权限集合,将集合存入请求参数map中
//                            Set<String> dataAuthList = sysUserService.getUserDataAuth(sysUser);
//                            if (dataAuthList != null && !dataAuthList.isEmpty()) {
//                                map.put(mapKey, dataAuthList);
//                            }
                        }
                        return pjp.proceed();
                    }
                }
            }
        }
        Result result = new Result();
        result.setResult(Result.RESULT_FLG.FAIL.getValue());
        result.setErrorCode(RespCodeEnum.DATA_AUTH_UNAUTHORIZED.getCode());
        result.setErrorMsg(RespCodeEnum.DATA_AUTH_UNAUTHORIZED.getMsg());
        return result;
    }
}