Search in sources :

Example 1 with DataColumn

use of com.ruoyi.common.mybatis.annotation.DataColumn in project RuoYi-Cloud-Plus by JavaLionLi.

the class PlusDataPermissionHandler method findAnnotation.

private DataColumn[] findAnnotation(String mappedStatementId) {
    StringBuilder sb = new StringBuilder(mappedStatementId);
    int index = sb.lastIndexOf(".");
    String clazzName = sb.substring(0, index);
    String methodName = sb.substring(index + 1, sb.length());
    Class<?> clazz = ClassUtil.loadClass(clazzName);
    List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz)).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
    DataPermission dataPermission;
    // 获取方法注解
    for (Method method : methods) {
        dataPermission = dataPermissionCacheMap.get(mappedStatementId);
        if (ObjectUtil.isNotNull(dataPermission)) {
            return dataPermission.value();
        }
        if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) {
            dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class);
            dataPermissionCacheMap.put(mappedStatementId, dataPermission);
            return dataPermission.value();
        }
    }
    dataPermission = dataPermissionCacheMap.get(clazz.getName());
    if (ObjectUtil.isNotNull(dataPermission)) {
        return dataPermission.value();
    }
    // 获取类注解
    if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) {
        dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class);
        dataPermissionCacheMap.put(clazz.getName(), dataPermission);
        return dataPermission.value();
    }
    return null;
}
Also used : Arrays(java.util.Arrays) ObjectUtil(cn.hutool.core.util.ObjectUtil) ParserContext(org.springframework.expression.ParserContext) DataPermissionHelper(com.ruoyi.common.mybatis.helper.DataPermissionHelper) ClassUtil(cn.hutool.core.util.ClassUtil) LoginHelper(com.ruoyi.common.satoken.utils.LoginHelper) ServiceException(com.ruoyi.common.core.exception.ServiceException) JSQLParserException(net.sf.jsqlparser.JSQLParserException) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) DataPermission(com.ruoyi.common.mybatis.annotation.DataPermission) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SpringUtils(com.ruoyi.common.core.utils.SpringUtils) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Map(java.util.Map) DataScopeType(com.ruoyi.common.mybatis.enums.DataScopeType) Method(java.lang.reflect.Method) LoginUser(com.ruoyi.system.api.model.LoginUser) CCJSqlParserUtil(net.sf.jsqlparser.parser.CCJSqlParserUtil) AnnotationUtil(cn.hutool.core.annotation.AnnotationUtil) ConcurrentHashSet(cn.hutool.core.collection.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) StringUtils(com.ruoyi.common.core.utils.StringUtils) Expression(net.sf.jsqlparser.expression.Expression) Collectors(java.util.stream.Collectors) BeanResolver(org.springframework.expression.BeanResolver) ExpressionParser(org.springframework.expression.ExpressionParser) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RoleDTO(com.ruoyi.system.api.model.RoleDTO) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) ArrayUtil(cn.hutool.core.util.ArrayUtil) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) DataPermission(com.ruoyi.common.mybatis.annotation.DataPermission) Method(java.lang.reflect.Method)

Example 2 with DataColumn

use of com.ruoyi.common.mybatis.annotation.DataColumn in project RuoYi-Cloud-Plus by JavaLionLi.

the class PlusDataPermissionHandler method buildDataFilter.

/**
 * 构造数据过滤sql
 */
private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) {
    StringBuilder sqlString = new StringBuilder();
    // 更新或删除需满足所有条件
    String joinStr = isSelect ? " OR " : " AND ";
    LoginUser loginUser = DataPermissionHelper.getVariable("user");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(beanResolver);
    DataPermissionHelper.getContext().forEach(context::setVariable);
    for (RoleDTO role : loginUser.getRoles()) {
        loginUser.setRoleId(role.getRoleId());
        // 获取角色权限泛型
        DataScopeType type = DataScopeType.findCode(role.getDataScope());
        if (ObjectUtil.isNull(type)) {
            throw new ServiceException("角色数据范围异常 => " + role.getDataScope());
        }
        // 全部数据权限直接返回
        if (type == DataScopeType.ALL) {
            return "";
        }
        boolean isSuccess = false;
        for (DataColumn dataColumn : dataColumns) {
            // 不包含 key 变量 则不处理
            if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) {
                continue;
            }
            // 设置注解变量 key 为表达式变量 value 为变量值
            context.setVariable(dataColumn.key(), dataColumn.value());
            // 解析sql模板并填充
            String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class);
            sqlString.append(joinStr).append(sql);
            isSuccess = true;
        }
        // 未处理成功则填充兜底方案
        if (!isSuccess) {
            sqlString.append(joinStr).append(type.getElseSql());
        }
    }
    if (StringUtils.isNotBlank(sqlString.toString())) {
        return sqlString.substring(joinStr.length());
    }
    return "";
}
Also used : RoleDTO(com.ruoyi.system.api.model.RoleDTO) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DataScopeType(com.ruoyi.common.mybatis.enums.DataScopeType) ServiceException(com.ruoyi.common.core.exception.ServiceException) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) LoginUser(com.ruoyi.system.api.model.LoginUser)

Example 3 with DataColumn

use of com.ruoyi.common.mybatis.annotation.DataColumn in project RuoYi-Cloud-Plus by JavaLionLi.

the class PlusDataPermissionHandler method getSqlSegment.

public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
    DataColumn[] dataColumns = findAnnotation(mappedStatementId);
    if (ArrayUtil.isEmpty(dataColumns)) {
        inavlidCacheSet.add(mappedStatementId);
        return where;
    }
    LoginUser currentUser = DataPermissionHelper.getVariable("user");
    if (ObjectUtil.isNull(currentUser)) {
        currentUser = LoginHelper.getLoginUser();
        DataPermissionHelper.setVariable("user", currentUser);
    }
    // 如果是超级管理员,则不过滤数据
    if (ObjectUtil.isNull(currentUser) || LoginHelper.isAdmin(currentUser.getUserId())) {
        return where;
    }
    String dataFilterSql = buildDataFilter(dataColumns, isSelect);
    if (StringUtils.isBlank(dataFilterSql)) {
        return where;
    }
    try {
        Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql);
        // 数据权限使用单独的括号 防止与其他条件冲突
        Parenthesis parenthesis = new Parenthesis(expression);
        if (ObjectUtil.isNotNull(where)) {
            return new AndExpression(where, parenthesis);
        } else {
            return parenthesis;
        }
    } catch (JSQLParserException e) {
        throw new ServiceException("数据权限解析异常 => " + e.getMessage());
    }
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) ServiceException(com.ruoyi.common.core.exception.ServiceException) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) JSQLParserException(net.sf.jsqlparser.JSQLParserException) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) LoginUser(com.ruoyi.system.api.model.LoginUser)

Aggregations

ServiceException (com.ruoyi.common.core.exception.ServiceException)3 DataColumn (com.ruoyi.common.mybatis.annotation.DataColumn)3 LoginUser (com.ruoyi.system.api.model.LoginUser)3 DataScopeType (com.ruoyi.common.mybatis.enums.DataScopeType)2 RoleDTO (com.ruoyi.system.api.model.RoleDTO)2 JSQLParserException (net.sf.jsqlparser.JSQLParserException)2 Expression (net.sf.jsqlparser.expression.Expression)2 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)2 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 AnnotationUtil (cn.hutool.core.annotation.AnnotationUtil)1 ConcurrentHashSet (cn.hutool.core.collection.ConcurrentHashSet)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 ClassUtil (cn.hutool.core.util.ClassUtil)1 ObjectUtil (cn.hutool.core.util.ObjectUtil)1 SpringUtils (com.ruoyi.common.core.utils.SpringUtils)1 StringUtils (com.ruoyi.common.core.utils.StringUtils)1 DataPermission (com.ruoyi.common.mybatis.annotation.DataPermission)1 DataPermissionHelper (com.ruoyi.common.mybatis.helper.DataPermissionHelper)1 LoginHelper (com.ruoyi.common.satoken.utils.LoginHelper)1