فهرست منبع

数据权限全面升级

lzm 2 سال پیش
والد
کامیت
74354c7f2d

+ 5 - 5
ruoyi-common/ruoyi-common-datascope/src/main/java/com/ruoyi/common/datascope/handler/DataScopeType.java

@@ -29,27 +29,27 @@ public enum DataScopeType {
      */
      */
     //采用远程调用服务的方式调用getRoleCustom 调用roledept服务获取对应的dept数据   user为loginUser 目前只有userid,roleId和deptId都没有
     //采用远程调用服务的方式调用getRoleCustom 调用roledept服务获取对应的dept数据   user为loginUser 目前只有userid,roleId和deptId都没有
 //    CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) ", ""),
 //    CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) ", ""),
-    CUSTOM("30", " #{#deptName} IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = #{#user.roleId } ) ", ""),
+    CUSTOM("2", " #{#deptName} IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = #{#user.roleId } ) ", ""),
 
 
     /**
     /**
      * 部门数据权限
      * 部门数据权限
      */
      */
-    DEPT("20", " #{#deptName} = #{#user.sysUser.deptId} ", ""),
+    DEPT("3", " #{#deptName} = #{#user.sysUser.deptId} ", ""),
 
 
     /**
     /**
      * 部门及以下数据权限
      * 部门及以下数据权限
      */
      */
 //    DEPT_AND_CHILD("4", " #{#deptName} IN ( #{@sdss.getDeptAndChild( #user.deptId )} )", ""),
 //    DEPT_AND_CHILD("4", " #{#deptName} IN ( #{@sdss.getDeptAndChild( #user.deptId )} )", ""),
-    DEPT_AND_CHILD("10", " #{#deptName} IN ( SELECT dept_id FROM sys_dept WHERE dept_id = #{#user.sysUser.deptId} or find_in_set( #{#user.sysUser.deptId} , ancestors ) )", ""),
+    DEPT_AND_CHILD("4", " #{#deptName} IN ( SELECT dept_id FROM sys_dept WHERE dept_id = #{#user.sysUser.deptId} or find_in_set( #{#user.sysUser.deptId} , ancestors ) )", ""),
     /**
     /**
      * 仅本人数据权限
      * 仅本人数据权限
      */
      */
-    SELF("60", " #{#userName} = #{#user.userid} ", " 1 = 0 "),
+    SELF("5", " #{#userName} = #{#user.userid} ", " 1 = 0 "),
 
 
     /**
     /**
      * 仅本机构数据权限
      * 仅本机构数据权限
      */
      */
-    JGID("50", " #{#jgName}  = '#{#user.sysUser.jgId}' ", " 1 = 0 ");
+    JGID("6", " #{#jgName}  = '#{#user.sysUser.jgId}' ", " 1 = 0 ");
 
 
     private final String code;
     private final String code;
 
 

+ 49 - 31
ruoyi-common/ruoyi-common-datascope/src/main/java/com/ruoyi/common/datascope/handler/PlusDataPermissionHandler.java

@@ -6,7 +6,9 @@ import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.ClassUtil;
 import cn.hutool.core.util.ClassUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.extra.spring.SpringUtil;
+import com.ruoyi.common.core.context.SecurityContextHolder;
 import com.ruoyi.common.core.exception.ServiceException;
 import com.ruoyi.common.core.exception.ServiceException;
+import com.ruoyi.common.core.text.Convert;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.security.utils.SecurityUtils;
 import com.ruoyi.common.security.utils.SecurityUtils;
 import com.ruoyi.ext.api.enums.SysRoleStatus;
 import com.ruoyi.ext.api.enums.SysRoleStatus;
@@ -158,44 +160,60 @@ public class PlusDataPermissionHandler {
         StandardEvaluationContext context = new StandardEvaluationContext();
         StandardEvaluationContext context = new StandardEvaluationContext();
         context.setBeanResolver(beanResolver);
         context.setBeanResolver(beanResolver);
         DataPermissionHelper.getContext().forEach(context::setVariable);
         DataPermissionHelper.getContext().forEach(context::setVariable);
-//        for (SysRole role : loginUser.getSysUser().getRoles()) {
-//            loginUser.setRoleId(role.getRoleId());
-//
-//        }
-        // 获取角色权限泛型
-        DataScopeType type = DataScopeType.findCode(DataPermissionHelper.getVariable("datascope"));
-        if (ObjectUtil.isNull(type)) {
-            type = DataScopeType.ALL;
-            System.out.println("角色数据范围异常 (或者该方法没有权限注解)=> " + DataPermissionHelper.getVariable("datascope"));
-//            throw new ServiceException("角色数据范围异常 => " + DataPermissionHelper.getVariable("datascope"));
-        }
-        // 全部数据权限直接返回
-        if (type == DataScopeType.ALL) {
+
+        String permission = StringUtils.defaultString(SecurityContextHolder.getPermission());
+        List<String> conditions = new ArrayList<String>();
+
+        if (StringUtils.isEmpty(permission)){
+            System.out.println("方法不存在数据权限");
             return "";
             return "";
         }
         }
-        boolean isSuccess = false;
-        for (DataColumn dataColumn : dataColumns) {
-            // 不包含 key 变量 则不处理
-            if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) {
+
+        for (SysRole role : loginUser.getSysUser().getRoles()) {
+            String dataScope = role.getDataScope();
+            // 获取角色权限泛型
+            DataScopeType type = DataScopeType.findCode(dataScope);
+            if (ObjectUtil.isNull(type) || StringUtils.isEmpty(role.getPermissions())) {
+                continue;
+            }
+            if (type!=DataScopeType.CUSTOM && conditions.contains(dataScope))
+            {
                 continue;
                 continue;
             }
             }
-            // 设置注解变量 key 为表达式变量 value 为变量值
-            if (!StringUtils.isBlank(dataColumn.alias()) && isJoin){
-                context.setVariable(dataColumn.key(), dataColumn.alias() + "." + dataColumn.value());
-            }else {
-                context.setVariable(dataColumn.key(), dataColumn.value());
+            if (StringUtils.isNotEmpty(permission) && StringUtils.isNotEmpty(role.getPermissions())
+                    && !StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
+            {
+                continue;
             }
             }
 
 
-            // 解析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 (type == DataScopeType.ALL) {
+                return "";
+            }
+            boolean isSuccess = false;
+            for (DataColumn dataColumn : dataColumns) {
+                // 不包含 key 变量 则不处理
+                if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) {
+                    continue;
+                }
+                // 设置注解变量 key 为表达式变量 value 为变量值
+                if (!StringUtils.isBlank(dataColumn.alias()) && isJoin){
+                    context.setVariable(dataColumn.key(), dataColumn.alias() + "." + dataColumn.value());
+                }else {
+                    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());
+            }
+            conditions.add(dataScope);
+        }
         if (StringUtils.isNotBlank(sqlString.toString())) {
         if (StringUtils.isNotBlank(sqlString.toString())) {
             return sqlString.substring(joinStr.length());
             return sqlString.substring(joinStr.length());
         }
         }

+ 0 - 6
ruoyi-modules/mz-system-ext/src/main/java/com/ruoyi/system/service/ISysDeptJlService.java

@@ -77,12 +77,6 @@ public interface ISysDeptJlService
      */
      */
     public int deleteSysDeptJlById(String id);
     public int deleteSysDeptJlById(String id);
 
 
-    /**
-     * 同步行政区到部门表
-     * @return 结果
-     */
-    public boolean syncToDept();
-
     /** 根据登陆的人的地区级别,获取对应的信息*/
     /** 根据登陆的人的地区级别,获取对应的信息*/
     public List<SysDeptJl> getLevelList(String code, String level);
     public List<SysDeptJl> getLevelList(String code, String level);
     public List<SysDeptJl> getLevelListZS(String code, String level);
     public List<SysDeptJl> getLevelListZS(String code, String level);

+ 0 - 65
ruoyi-modules/mz-system-ext/src/main/java/com/ruoyi/system/service/impl/SysDeptJlServiceImpl.java

@@ -33,12 +33,6 @@ public class SysDeptJlServiceImpl implements ISysDeptJlService {
     @Autowired
     @Autowired
     private SysDeptJlMapper sysDeptJlMapper;
     private SysDeptJlMapper sysDeptJlMapper;
 
 
-//    @Autowired
-//    private ISysDeptService sysDeptService;
-
-    @Autowired
-    private RedisService redisService;
-
     /**
     /**
      * 查询吉林地区管理
      * 查询吉林地区管理
      *
      *
@@ -157,65 +151,6 @@ public class SysDeptJlServiceImpl implements ISysDeptJlService {
         return sysDeptJlMapper.deleteSysDeptJlById(id);
         return sysDeptJlMapper.deleteSysDeptJlById(id);
     }
     }
 
 
-    /**
-     * 同步行政区到部门表
-     *
-     * @return 结果
-     */
-    @Override
-    public boolean syncToDept() {
-        Object sync_time = redisService.getCacheObject("sync_time");
-        if (sync_time != null) {
-            throw new ServiceException("正在同步中,请稍后重试");
-        }
-        redisService.setCacheObject("sync_time", 1, 5L, TimeUnit.MINUTES);
-        HashMap<String, String> map = OpenDictUtil.getDeptLabel();
-
-        // 同步一级地区
-        SysDeptJl item = sysDeptJlMapper.selectSysDeptJlById(OtherConstants.DEPT_ID_JL);
-//        SysDept sysDept1 = syncD(item, 0L, map);
-        // 同步二级地区
-        List<SysDeptJl> sysDeptJls2 = sysDeptJlMapper.selectSysDeptJlByParentId(item.getId());
-        for (SysDeptJl item2 : sysDeptJls2) {
-            SysDept sysDept2 = syncD(item2, 1L, map);
-
-            List<SysDeptJl> sysDeptJls3 = sysDeptJlMapper.selectSysDeptJlByParentId(item2.getId());
-            for (SysDeptJl item3 : sysDeptJls3) {
-                SysDept sysDept3 = syncD(item3, sysDept2.getDeptId(), map);
-
-                List<SysDeptJl> sysDeptJls4 = sysDeptJlMapper.selectSysDeptJlByParentId(item3.getId());
-                for (SysDeptJl item4 : sysDeptJls4) {
-                    SysDept sysDept4 = syncD(item4, sysDept3.getDeptId(), map);
-
-                    List<SysDeptJl> sysDeptJls5 = sysDeptJlMapper.selectSysDeptJlByParentId(item4.getId());
-                    for (SysDeptJl item5 : sysDeptJls5) {
-                        syncD(item5, sysDept4.getDeptId(), map);
-
-                    }
-                }
-            }
-        }
-        return true;
-    }
-
-    private SysDept syncD(SysDeptJl item, Long parentId, HashMap<String, String> map) {
-        try {
-            SysDept sysDept = new SysDept();
-//            sysDept.setDeptId(Long.parseLong(item.getCode()));
-            sysDept.setDeptName(item.getName() + map.get(item.getLevel()));
-            sysDept.setOrderNum(0);
-            sysDept.setStatus("0");
-            sysDept.setParentId(parentId);
-            sysDept.setLocationCode(item.getCode());
-//            sysDeptService.insertDept(sysDept);
-            return sysDept;
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return null;
-    }
-
     public String retCode(String pid, String code, Integer level) {
     public String retCode(String pid, String code, Integer level) {
         SysDeptJl sysDeptJl1 = new SysDeptJl();
         SysDeptJl sysDeptJl1 = new SysDeptJl();
         sysDeptJl1.setParentId(pid);
         sysDeptJl1.setParentId(pid);

+ 5 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDeptJlController.java

@@ -14,6 +14,7 @@ import com.ruoyi.ext.api.utils.SubCodeUtil;
 import com.ruoyi.ext.api.validate.group.AddGroup;
 import com.ruoyi.ext.api.validate.group.AddGroup;
 import com.ruoyi.system.domain.SysDeptJl;
 import com.ruoyi.system.domain.SysDeptJl;
 import com.ruoyi.system.service.ISysDeptJlService;
 import com.ruoyi.system.service.ISysDeptJlService;
+import com.ruoyi.system.service.ISysDeptService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
@@ -35,6 +36,9 @@ public class SysDeptJlController extends BaseController
     @Autowired
     @Autowired
     private ISysDeptJlService sysDeptJlService;
     private ISysDeptJlService sysDeptJlService;
 
 
+    @Autowired
+    private ISysDeptService sysDeptService;
+
     /**
     /**
      * 查询吉林地区管理列表
      * 查询吉林地区管理列表
      */
      */
@@ -95,7 +99,7 @@ public class SysDeptJlController extends BaseController
     @GetMapping("/syncToDept")
     @GetMapping("/syncToDept")
     public AjaxResult syncToDept()
     public AjaxResult syncToDept()
     {
     {
-        return toAjax(sysDeptJlService.syncToDept());
+        return toAjax(sysDeptService.syncToDept());
     }
     }
 
 
     /**
     /**

+ 6 - 7
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java

@@ -19,13 +19,6 @@ public interface ISysDeptService
      * @return 部门信息集合
      * @return 部门信息集合
      */
      */
     public List<SysDept> selectDeptList(SysDept dept);
     public List<SysDept> selectDeptList(SysDept dept);
-    /**
-     * 查询部门管理数据
-     *
-     * @param dept 部门树形信息
-     * @return 部门信息集合
-     */
-    public List<TreeSelect> selectDeptTreeList(SysDept dept);
 
 
     /**
     /**
      * 查询部门树结构信息
      * 查询部门树结构信息
@@ -129,4 +122,10 @@ public interface ISysDeptService
      * @return 结果
      * @return 结果
      */
      */
     public int deleteDeptById(Long deptId);
     public int deleteDeptById(Long deptId);
+
+    /**
+     * 同步行政区到部门表
+     * @return 结果
+     */
+    public boolean syncToDept();
 }
 }

+ 67 - 13
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -6,7 +6,10 @@ import com.ruoyi.common.core.text.Convert;
 import com.ruoyi.common.core.utils.SpringUtils;
 import com.ruoyi.common.core.utils.SpringUtils;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.datascope.annotation.DataScope;
 import com.ruoyi.common.datascope.annotation.DataScope;
+import com.ruoyi.common.redis.service.RedisService;
 import com.ruoyi.common.security.utils.SecurityUtils;
 import com.ruoyi.common.security.utils.SecurityUtils;
+import com.ruoyi.ext.api.utils.OpenDictUtil;
+import com.ruoyi.ext.api.utils.OtherConstants;
 import com.ruoyi.system.api.domain.SysDept;
 import com.ruoyi.system.api.domain.SysDept;
 import com.ruoyi.system.api.domain.SysRole;
 import com.ruoyi.system.api.domain.SysRole;
 import com.ruoyi.system.domain.SysDeptJl;
 import com.ruoyi.system.domain.SysDeptJl;
@@ -19,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.util.*;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -38,6 +42,9 @@ public class SysDeptServiceImpl implements ISysDeptService
     @Autowired
     @Autowired
     private SysDeptJlMapper sysDeptJlMapper;
     private SysDeptJlMapper sysDeptJlMapper;
 
 
+    @Autowired
+    private RedisService redisService;
+
     /**
     /**
      * 查询部门管理数据
      * 查询部门管理数据
      *
      *
@@ -58,19 +65,6 @@ public class SysDeptServiceImpl implements ISysDeptService
         return buildDeptTreeSelect(deptMapper.selectDeptTreeList(dept));
         return buildDeptTreeSelect(deptMapper.selectDeptTreeList(dept));
     }
     }
 
 
-    /**
-     * 查询部门树结构信息
-     * 
-     * @param dept 部门信息
-     * @return 部门树信息集合
-     */
-    @Override
-    public List<TreeSelect> selectDeptTreeList(SysDept dept)
-    {
-        List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
-        return buildDeptTreeSelect(depts);
-    }
-
     /**
     /**
      * 构建前端所需要树结构
      * 构建前端所需要树结构
      *
      *
@@ -423,4 +417,64 @@ public class SysDeptServiceImpl implements ISysDeptService
     {
     {
         return getChildList(list, t).size() > 0 ? true : false;
         return getChildList(list, t).size() > 0 ? true : false;
     }
     }
+
+
+    /**
+     * 同步行政区到部门表
+     *
+     * @return 结果
+     */
+    @Override
+    public boolean syncToDept() {
+        Object sync_time = redisService.getCacheObject("sync_time");
+        if (sync_time != null) {
+            throw new ServiceException("正在同步中,请稍后重试");
+        }
+        redisService.setCacheObject("sync_time", 1, 5L, TimeUnit.MINUTES);
+        HashMap<String, String> map = OpenDictUtil.getDeptLabel();
+
+        // 同步一级地区
+        SysDeptJl item = sysDeptJlMapper.selectSysDeptJlById(OtherConstants.DEPT_ID_JL);
+//        SysDept sysDept1 = syncD(item, 0L, map);
+        // 同步二级地区
+        List<SysDeptJl> sysDeptJls2 = sysDeptJlMapper.selectSysDeptJlByParentId(item.getId());
+        for (SysDeptJl item2 : sysDeptJls2) {
+            SysDept sysDept2 = syncD(item2, 1L, map);
+
+            List<SysDeptJl> sysDeptJls3 = sysDeptJlMapper.selectSysDeptJlByParentId(item2.getId());
+            for (SysDeptJl item3 : sysDeptJls3) {
+                SysDept sysDept3 = syncD(item3, sysDept2.getDeptId(), map);
+
+                List<SysDeptJl> sysDeptJls4 = sysDeptJlMapper.selectSysDeptJlByParentId(item3.getId());
+                for (SysDeptJl item4 : sysDeptJls4) {
+                    SysDept sysDept4 = syncD(item4, sysDept3.getDeptId(), map);
+
+                    List<SysDeptJl> sysDeptJls5 = sysDeptJlMapper.selectSysDeptJlByParentId(item4.getId());
+                    for (SysDeptJl item5 : sysDeptJls5) {
+                        syncD(item5, sysDept4.getDeptId(), map);
+
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    private SysDept syncD(SysDeptJl item, Long parentId, HashMap<String, String> map) {
+        try {
+            SysDept sysDept = new SysDept();
+//            sysDept.setDeptId(Long.parseLong(item.getCode()));
+            sysDept.setDeptName(item.getName() + map.get(item.getLevel()));
+            sysDept.setOrderNum(0);
+            sysDept.setStatus("0");
+            sysDept.setParentId(parentId);
+            sysDept.setLocationCode(item.getCode());
+            this.insertDept(sysDept);
+            return sysDept;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
 }
 }

+ 5 - 5
ruoyi-ui/src/views/system/role/index.vue

@@ -300,23 +300,23 @@ export default {
           label: "全部数据权限"
           label: "全部数据权限"
         },
         },
         {
         {
-          value: "30",
+          value: "2",
           label: "自定数据权限"
           label: "自定数据权限"
         },
         },
         {
         {
-          value: "20",
+          value: "3",
           label: "本部门数据权限"
           label: "本部门数据权限"
         },
         },
         {
         {
-          value: "10",
+          value: "4",
           label: "本部门及以下数据权限"
           label: "本部门及以下数据权限"
         },
         },
         {
         {
-          value: "60",
+          value: "5",
           label: "仅本人数据权限"
           label: "仅本人数据权限"
         },
         },
         {
         {
-          value: "50",
+          value: "6",
           label: "仅本机构数据权限"
           label: "仅本机构数据权限"
         }
         }
       ],
       ],