From a181c0978697e56d26b623487fafa9602b911717 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期六, 08 五月 2021 23:35:39 +0800
Subject: [PATCH] fix 修复数据权限问题

---
 ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml                  |   22 +++++++++++
 ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java            |    8 ++++
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java |   10 ----
 ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java   |   13 ++++--
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java |   15 -------
 ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml                  |   23 +++++++++++
 ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java            |    7 +++
 7 files changed, 71 insertions(+), 27 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
index ffd4718..4d2f9bb 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
@@ -3,7 +3,6 @@
 import cn.hutool.core.lang.Validator;
 import cn.hutool.core.util.StrUtil;
 import com.ruoyi.common.annotation.DataScope;
-import com.ruoyi.common.core.domain.BaseEntity;
 import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.domain.model.LoginUser;
@@ -19,6 +18,7 @@
 import org.springframework.stereotype.Component;
 
 import java.lang.reflect.Method;
+import java.util.Map;
 
 /**
  * 鏁版嵁杩囨护澶勭悊
@@ -145,10 +145,15 @@
         if (StrUtil.isNotBlank(sqlString.toString()))
         {
             Object params = joinPoint.getArgs()[0];
-            if (Validator.isNotNull(params) && params instanceof BaseEntity)
+            if (Validator.isNotNull(params))
             {
-                BaseEntity baseEntity = (BaseEntity) params;
-                baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
+                try {
+                    Method getParams = params.getClass().getDeclaredMethod("getParams", null);
+                    Map<String, Object> invoke = (Map<String, Object>) getParams.invoke(params, null);
+                    invoke.put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
             }
         }
     }
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
index c2619d3..b59afae 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java
@@ -14,6 +14,14 @@
 public interface SysDeptMapper extends BaseMapper<SysDept> {
 
     /**
+     * 鏌ヨ閮ㄩ棬绠$悊鏁版嵁
+     *
+     * @param dept 閮ㄩ棬淇℃伅
+     * @return 閮ㄩ棬淇℃伅闆嗗悎
+     */
+    public List<SysDept> selectDeptList(SysDept dept);
+
+    /**
      * 鏍规嵁瑙掕壊ID鏌ヨ閮ㄩ棬鏍戜俊鎭�
      *
      * @param roleId            瑙掕壊ID
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java
index 1ed48b6..6bd05a1 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java
@@ -12,6 +12,13 @@
  */
 public interface SysRoleMapper extends BaseMapper<SysRole> {
 
+    /**
+     * 鏍规嵁鏉′欢鍒嗛〉鏌ヨ瑙掕壊鏁版嵁
+     *
+     * @param role 瑙掕壊淇℃伅
+     * @return 瑙掕壊鏁版嵁闆嗗悎淇℃伅
+     */
+    public List<SysRole> selectRoleList(SysRole role);
 
     /**
      * 鏍规嵁鐢ㄦ埛ID鏌ヨ瑙掕壊
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
index 98616e8..f25a90d 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
@@ -48,15 +48,7 @@
     @Override
     @DataScope(deptAlias = "d")
     public List<SysDept> selectDeptList(SysDept dept) {
-        Object dataScope = dept.getParams().get("dataScope");
-        return list(new LambdaQueryWrapper<SysDept>()
-                .eq(dept.getParentId() != null && dept.getParentId() != 0,
-                        SysDept::getParentId, dept.getParentId())
-                .like(StrUtil.isNotBlank(dept.getDeptName()), SysDept::getDeptName, dept.getDeptName())
-                .eq(StrUtil.isNotBlank(dept.getStatus()), SysDept::getStatus, dept.getStatus())
-                .apply(dataScope != null, dataScope != null ? dataScope.toString() : null)
-                .orderByAsc(SysDept::getParentId)
-                .orderByAsc(SysDept::getOrderNum));
+        return baseMapper.selectDeptList(dept);
     }
 
     /**
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java
index 2673c81..814d96f 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java
@@ -49,20 +49,7 @@
     @Override
     @DataScope(deptAlias = "d")
     public List<SysRole> selectRoleList(SysRole role) {
-        Map<String, Object> params = role.getParams();
-        Object dataScope = params.get("dataScope");
-        return list(new LambdaQueryWrapper<SysRole>()
-                .like(StrUtil.isNotBlank(role.getRoleName()), SysRole::getRoleName, role.getRoleName())
-                .eq(StrUtil.isNotBlank(role.getStatus()), SysRole::getStatus, role.getStatus())
-                .like(StrUtil.isNotBlank(role.getRoleKey()), SysRole::getRoleKey, role.getRoleKey())
-                .apply(Validator.isNotEmpty(params.get("beginTime")),
-                        "date_format(create_time,'%y%m%d') >= date_format({0},'%y%m%d')",
-                        params.get("beginTime"))
-                .apply(Validator.isNotEmpty(params.get("endTime")),
-                        "date_format(create_time,'%y%m%d') <= date_format({0},'%y%m%d')",
-                        params.get("endTime"))
-                .apply(dataScope != null, dataScope != null ? dataScope.toString() : null)
-                .orderByAsc(SysRole::getRoleSort));
+        return baseMapper.selectRoleList(role);
     }
 
     /**
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
index 07fc5d6..0f99671 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml
@@ -22,6 +22,28 @@
         <result property="updateTime" column="update_time"/>
     </resultMap>
 
+    <sql id="selectDeptVo">
+        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
+        from sys_dept d
+    </sql>
+
+    <select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
+        <include refid="selectDeptVo"/>
+        where d.del_flag = '0'
+        <if test="parentId != null and parentId != 0">
+            AND parent_id = #{parentId}
+        </if>
+        <if test="deptName != null and deptName != ''">
+            AND dept_name like concat('%', #{deptName}, '%')
+        </if>
+        <if test="status != null and status != ''">
+            AND status = #{status}
+        </if>
+        <!-- 鏁版嵁鑼冨洿杩囨护 -->
+        ${params.dataScope}
+        order by d.parent_id, d.order_num
+    </select>
+
     <select id="selectDeptListByRoleId" resultType="Integer">
         select d.dept_id
         from sys_dept d
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml
index 9362ebc..dca0ec0 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml
@@ -39,6 +39,29 @@
                  left join sys_dept d on u.dept_id = d.dept_id
     </sql>
 
+    <select id="selectRoleList" parameterType="SysRole" resultMap="SysRoleResult">
+        <include refid="selectRoleVo"/>
+        where r.del_flag = '0'
+        <if test="roleName != null and roleName != ''">
+            AND r.role_name like concat('%', #{roleName}, '%')
+        </if>
+        <if test="status != null and status != ''">
+            AND r.status = #{status}
+        </if>
+        <if test="roleKey != null and roleKey != ''">
+            AND r.role_key like concat('%', #{roleKey}, '%')
+        </if>
+        <if test="params.beginTime != null and params.beginTime != ''"><!-- 寮�濮嬫椂闂存绱� -->
+            and date_format(r.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
+        </if>
+        <if test="params.endTime != null and params.endTime != ''"><!-- 缁撴潫鏃堕棿妫�绱� -->
+            and date_format(r.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
+        </if>
+        <!-- 鏁版嵁鑼冨洿杩囨护 -->
+        ${params.dataScope}
+        order by r.role_sort
+    </select>
+
     <select id="selectRolePermissionByUserId" parameterType="Long" resultMap="SysRoleResult">
         <include refid="selectRoleVo"/>
         WHERE r.del_flag = '0' and ur.user_id = #{userId}

--
Gitblit v1.9.3