RoleAspect.java 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
package cn.timer.api.aspect;

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

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.timer.api.utils.redis.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Console;
import cn.timer.api.aspect.lang.annotation.BindingResultCtrol;
import cn.timer.api.aspect.lang.annotation.Role;
import cn.timer.api.bean.qyzx.QyzxEmpEntAsso;
import cn.timer.api.bean.qyzx.QyzxEmpLogin;
import cn.timer.api.config.enums.SysRoleType;
import cn.timer.api.dao.qyzx.QyzxEmpEntAssoMapper;
import cn.timer.api.utils.ResultUtil;

/**
 * 角色权限 处理
 * 
 * @author Tang
 */
@Aspect
@Component
public class RoleAspect {

//	private static final Logger log = LoggerFactory.getLogger(RoleAspect.class);

	@Autowired
	private QyzxEmpEntAssoMapper qyzxEmpEntAssoMapper;
	
	@Resource
	private HttpSession session;
	@Resource
	private RedisUtil redisUtil;

	// 配置织入点
	@Pointcut("@annotation(cn.timer.api.aspect.lang.annotation.Role)")
	public void RolePointCut() {
	}

	@Around("RolePointCut()")
	public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {

		Object retVal;
		// 获得注解
    	Role role = getAnnotationLog(joinPoint);
        
        String httpMethodName = role.httpMethod().name();
        List<Integer> sysRoleType = ListUtil.toList();
        for (SysRoleType t : role.sysRoleType()) {
        	sysRoleType.add(t.getType());
		}
        
        QyzxEmpLogin eld = BeanUtil.toBean(redisUtil.getAttribute(session.getId(),"ui"), QyzxEmpLogin.class);

        Integer orgCode = eld.getOrgId();
		Integer count = new LambdaQueryChainWrapper<QyzxEmpEntAsso>(qyzxEmpEntAssoMapper)
				.eq(QyzxEmpEntAsso::getEmpNum, eld.getId())
				.eq(QyzxEmpEntAsso::getOrgCode, orgCode)
				.in(QyzxEmpEntAsso::getUserType, sysRoleType)
				.count();
		
		if (count <= 0) {
			
			switch (httpMethodName) {
			case "POST":
				retVal = ResultUtil.error("无权限操作");
				break;
			case "PUT":
				retVal = ResultUtil.error("无权限编辑");
				break;
			case "GET":
				retVal = ResultUtil.error("无权限查看");
				break;
			case "DELETE":
				retVal = ResultUtil.error("无权限删除");
				break;
			default:
				retVal = ResultUtil.error("无权限操作");
				break;
			}
			
			return retVal;
		}else {
			return retVal = joinPoint.proceed(joinPoint.getArgs());
        }
        
	}

	/**
	 * 是否存在注解,如果存在就获取
	 */
	private Role getAnnotationLog(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();

		if (method != null) {
			return method.getAnnotation(Role.class);
		}
		return null;
	}

}