mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	add 增加 DataPermissionHelper 类 便于操作数据权限变量
This commit is contained in:
		@@ -10,7 +10,7 @@ import lombok.Getter;
 | 
			
		||||
 * 语法支持 spel 模板表达式
 | 
			
		||||
 *
 | 
			
		||||
 * 内置数据 user 当前用户 内容参考 SysUser
 | 
			
		||||
 * 如需扩展数据 需往 SysUser 内注入
 | 
			
		||||
 * 如需扩展数据 可使用 {@link com.ruoyi.common.helper.DataPermissionHelper} 操作
 | 
			
		||||
 * 内置服务 sdss 系统数据权限服务 内容参考 SysDataScopeService
 | 
			
		||||
 * 如需扩展更多自定义服务 可以参考 sdss 自行编写
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,45 @@
 | 
			
		||||
package com.ruoyi.common.helper;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import com.ruoyi.common.utils.ServletUtils;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 数据权限助手
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lion Li
 | 
			
		||||
 * @version 3.5.0
 | 
			
		||||
 */
 | 
			
		||||
@SuppressWarnings("unchecked cast")
 | 
			
		||||
public class DataPermissionHelper {
 | 
			
		||||
 | 
			
		||||
    private static final String DATA_PERMISSION_KEY = "data:permission";
 | 
			
		||||
 | 
			
		||||
    public static <T> T getVariable(String key) {
 | 
			
		||||
        Map<String, Object> context = getContext();
 | 
			
		||||
        return (T) context.get(key);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public static void setVariable(String key, Object value) {
 | 
			
		||||
        Map<String, Object> context = getContext();
 | 
			
		||||
        context.put(key, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Map<String, Object> getContext() {
 | 
			
		||||
        HttpServletRequest request = ServletUtils.getRequest();
 | 
			
		||||
        Object attribute = request.getAttribute(DATA_PERMISSION_KEY);
 | 
			
		||||
        if (ObjectUtil.isNull(attribute)) {
 | 
			
		||||
            request.setAttribute(DATA_PERMISSION_KEY, new HashMap<>());
 | 
			
		||||
            attribute = request.getAttribute(DATA_PERMISSION_KEY);
 | 
			
		||||
        }
 | 
			
		||||
        if (attribute instanceof Map) {
 | 
			
		||||
            return (Map<String, Object>) attribute;
 | 
			
		||||
        }
 | 
			
		||||
        throw new NullPointerException("data permission context type exception");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -12,6 +12,7 @@ import com.ruoyi.common.core.domain.entity.SysUser;
 | 
			
		||||
import com.ruoyi.common.core.service.UserService;
 | 
			
		||||
import com.ruoyi.common.enums.DataScopeType;
 | 
			
		||||
import com.ruoyi.common.exception.ServiceException;
 | 
			
		||||
import com.ruoyi.common.helper.DataPermissionHelper;
 | 
			
		||||
import com.ruoyi.common.utils.SecurityUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.common.utils.spring.SpringUtils;
 | 
			
		||||
@@ -73,12 +74,16 @@ public class PlusDataPermissionHandler {
 | 
			
		||||
            inavlidCacheSet.add(mappedStatementId);
 | 
			
		||||
            return where;
 | 
			
		||||
        }
 | 
			
		||||
        SysUser currentUser = SpringUtils.getBean(UserService.class).selectUserById(SecurityUtils.getUserId());
 | 
			
		||||
        SysUser currentUser = DataPermissionHelper.getVariable("user");
 | 
			
		||||
        if (ObjectUtil.isNull(currentUser)) {
 | 
			
		||||
            currentUser = SpringUtils.getBean(UserService.class).selectUserById(SecurityUtils.getUserId());
 | 
			
		||||
            DataPermissionHelper.setVariable("user", currentUser);
 | 
			
		||||
        }
 | 
			
		||||
        // 如果是超级管理员,则不过滤数据
 | 
			
		||||
        if (StringUtils.isNull(currentUser) || currentUser.isAdmin()) {
 | 
			
		||||
        if (ObjectUtil.isNull(currentUser) || currentUser.isAdmin()) {
 | 
			
		||||
            return where;
 | 
			
		||||
        }
 | 
			
		||||
        String dataFilterSql = buildDataFilter(currentUser, dataColumns, isSelect);
 | 
			
		||||
        String dataFilterSql = buildDataFilter(dataColumns, isSelect);
 | 
			
		||||
        if (StringUtils.isBlank(dataFilterSql)) {
 | 
			
		||||
            return where;
 | 
			
		||||
        }
 | 
			
		||||
@@ -99,13 +104,14 @@ public class PlusDataPermissionHandler {
 | 
			
		||||
    /**
 | 
			
		||||
     * 构造数据过滤sql
 | 
			
		||||
     */
 | 
			
		||||
    private String buildDataFilter(SysUser user, DataColumn[] dataColumns, boolean isSelect) {
 | 
			
		||||
    private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) {
 | 
			
		||||
        StringBuilder sqlString = new StringBuilder();
 | 
			
		||||
        // 更新或删除需满足所有条件
 | 
			
		||||
        String joinStr = isSelect ? " OR " : " AND ";
 | 
			
		||||
        SysUser user = DataPermissionHelper.getVariable("user");
 | 
			
		||||
        StandardEvaluationContext context = new StandardEvaluationContext();
 | 
			
		||||
        context.setBeanResolver(beanResolver);
 | 
			
		||||
        context.setVariable("user", user);
 | 
			
		||||
        DataPermissionHelper.getContext().forEach(context::setVariable);
 | 
			
		||||
        for (SysRole role : user.getRoles()) {
 | 
			
		||||
            user.setRoleId(role.getRoleId());
 | 
			
		||||
            // 获取角色权限泛型
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user