Quellcode durchsuchen

修改时检查用户数据权限范围

RuoYi vor 4 Jahren
Ursprung
Commit
67df97d5a7

+ 1 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDeptController.java

@@ -75,6 +75,7 @@ public class SysDeptController extends BaseController
75 75
     @GetMapping(value = "/{deptId}")
76 76
     public AjaxResult getInfo(@PathVariable Long deptId)
77 77
     {
78
+        deptService.checkDeptDataScope(deptId);
78 79
         return AjaxResult.success(deptService.selectDeptById(deptId));
79 80
     }
80 81
 

+ 1 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java

@@ -69,6 +69,7 @@ public class SysRoleController extends BaseController
69 69
     @GetMapping(value = "/{roleId}")
70 70
     public AjaxResult getInfo(@PathVariable Long roleId)
71 71
     {
72
+        roleService.checkRoleDataScope(roleId);
72 73
         return AjaxResult.success(roleService.selectRoleById(roleId));
73 74
     }
74 75
 

+ 1 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java

@@ -172,6 +172,7 @@ public class SysUserController extends BaseController
172 172
     @GetMapping(value = { "/", "/{userId}" })
173 173
     public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
174 174
     {
175
+        userService.checkUserDataScope(userId);
175 176
         AjaxResult ajax = AjaxResult.success();
176 177
         List<SysRole> roles = roleService.selectRoleAll();
177 178
         ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));

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

@@ -85,6 +85,13 @@ public interface ISysDeptService
85 85
     public String checkDeptNameUnique(SysDept dept);
86 86
 
87 87
     /**
88
+     * 校验部门是否有数据权限
89
+     * 
90
+     * @param deptId 部门id
91
+     */
92
+    public void checkDeptDataScope(Long deptId);
93
+
94
+    /**
88 95
      * 新增保存部门信息
89 96
      * 
90 97
      * @param dept 部门信息

+ 7 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java

@@ -83,6 +83,13 @@ public interface ISysRoleService
83 83
     public void checkRoleAllowed(SysRole role);
84 84
 
85 85
     /**
86
+     * 校验角色是否有数据权限
87
+     * 
88
+     * @param roleId 角色id
89
+     */
90
+    public void checkRoleDataScope(Long roleId);
91
+
92
+    /**
86 93
      * 通过角色ID查询角色使用数量
87 94
      * 
88 95
      * @param roleId 角色ID

+ 7 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java

@@ -98,6 +98,13 @@ public interface ISysUserService
98 98
     public void checkUserAllowed(SysUser user);
99 99
 
100 100
     /**
101
+     * 校验用户是否有数据权限
102
+     * 
103
+     * @param userId 用户id
104
+     */
105
+    public void checkUserDataScope(Long userId);
106
+
107
+    /**
101 108
      * 新增用户信息
102 109
      * 
103 110
      * @param user 用户信息

+ 23 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -9,10 +9,13 @@ import org.springframework.stereotype.Service;
9 9
 import com.ruoyi.common.core.constant.UserConstants;
10 10
 import com.ruoyi.common.core.exception.ServiceException;
11 11
 import com.ruoyi.common.core.text.Convert;
12
+import com.ruoyi.common.core.utils.SecurityUtils;
13
+import com.ruoyi.common.core.utils.SpringUtils;
12 14
 import com.ruoyi.common.core.utils.StringUtils;
13 15
 import com.ruoyi.common.datascope.annotation.DataScope;
14 16
 import com.ruoyi.system.api.domain.SysDept;
15 17
 import com.ruoyi.system.api.domain.SysRole;
18
+import com.ruoyi.system.api.domain.SysUser;
16 19
 import com.ruoyi.system.domain.vo.TreeSelect;
17 20
 import com.ruoyi.system.mapper.SysDeptMapper;
18 21
 import com.ruoyi.system.mapper.SysRoleMapper;
@@ -172,6 +175,26 @@ public class SysDeptServiceImpl implements ISysDeptService
172 175
     }
173 176
 
174 177
     /**
178
+     * 校验部门是否有数据权限
179
+     * 
180
+     * @param deptId 部门id
181
+     */
182
+    @Override
183
+    public void checkDeptDataScope(Long deptId)
184
+    {
185
+        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
186
+        {
187
+            SysDept dept = new SysDept();
188
+            dept.setDeptId(deptId);
189
+            List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
190
+            if (StringUtils.isEmpty(depts))
191
+            {
192
+                throw new ServiceException("没有权限访问部门数据!");
193
+            }
194
+        }
195
+    }
196
+
197
+    /**
175 198
      * 新增保存部门信息
176 199
      * 
177 200
      * @param dept 部门信息

+ 22 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java

@@ -10,10 +10,12 @@ import org.springframework.stereotype.Service;
10 10
 import org.springframework.transaction.annotation.Transactional;
11 11
 import com.ruoyi.common.core.constant.UserConstants;
12 12
 import com.ruoyi.common.core.exception.ServiceException;
13
+import com.ruoyi.common.core.utils.SecurityUtils;
13 14
 import com.ruoyi.common.core.utils.SpringUtils;
14 15
 import com.ruoyi.common.core.utils.StringUtils;
15 16
 import com.ruoyi.common.datascope.annotation.DataScope;
16 17
 import com.ruoyi.system.api.domain.SysRole;
18
+import com.ruoyi.system.api.domain.SysUser;
17 19
 import com.ruoyi.system.domain.SysRoleDept;
18 20
 import com.ruoyi.system.domain.SysRoleMenu;
19 21
 import com.ruoyi.system.domain.SysUserRole;
@@ -188,6 +190,26 @@ public class SysRoleServiceImpl implements ISysRoleService
188 190
     }
189 191
 
190 192
     /**
193
+     * 校验角色是否有数据权限
194
+     * 
195
+     * @param roleId 角色id
196
+     */
197
+    @Override
198
+    public void checkRoleDataScope(Long roleId)
199
+    {
200
+        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
201
+        {
202
+            SysRole role = new SysRole();
203
+            role.setRoleId(roleId);
204
+            List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
205
+            if (StringUtils.isEmpty(roles))
206
+            {
207
+                throw new ServiceException("没有权限访问角色数据!");
208
+            }
209
+        }
210
+    }
211
+
212
+    /**
191 213
      * 通过角色ID查询角色使用数量
192 214
      * 
193 215
      * @param roleId 角色ID

+ 21 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
10 10
 import com.ruoyi.common.core.constant.UserConstants;
11 11
 import com.ruoyi.common.core.exception.ServiceException;
12 12
 import com.ruoyi.common.core.utils.SecurityUtils;
13
+import com.ruoyi.common.core.utils.SpringUtils;
13 14
 import com.ruoyi.common.core.utils.StringUtils;
14 15
 import com.ruoyi.common.datascope.annotation.DataScope;
15 16
 import com.ruoyi.system.api.domain.SysRole;
@@ -228,6 +229,26 @@ public class SysUserServiceImpl implements ISysUserService
228 229
     }
229 230
 
230 231
     /**
232
+     * 校验用户是否有数据权限
233
+     * 
234
+     * @param userId 用户id
235
+     */
236
+    @Override
237
+    public void checkUserDataScope(Long userId)
238
+    {
239
+        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
240
+        {
241
+            SysUser user = new SysUser();
242
+            user.setUserId(userId);
243
+            List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
244
+            if (StringUtils.isEmpty(users))
245
+            {
246
+                throw new ServiceException("没有权限访问用户数据!");
247
+            }
248
+        }
249
+    }
250
+
251
+    /**
231 252
      * 新增保存用户信息
232 253
      * 
233 254
      * @param user 用户信息

+ 5 - 2
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml

@@ -30,6 +30,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
30 30
 	<select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
31 31
         <include refid="selectDeptVo"/>
32 32
         where d.del_flag = '0'
33
+		<if test="deptId != null and deptId != 0">
34
+			AND dept_id = #{deptId}
35
+		</if>
33 36
         <if test="parentId != null and parentId != 0">
34 37
 			AND parent_id = #{parentId}
35 38
 		</if>
@@ -66,14 +69,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
66 69
 	
67 70
 	<select id="hasChildByDeptId" parameterType="Long" resultType="int">
68 71
 		select count(1) from sys_dept
69
-		where del_flag = '0' and parent_id = #{deptId}
72
+		where del_flag = '0' and parent_id = #{deptId} limit 1
70 73
 	</select>
71 74
 	
72 75
 	<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
73 76
 		select * from sys_dept where find_in_set(#{deptId}, ancestors)
74 77
 	</select>
75 78
 	
76
-	<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="java.lang.Integer">
79
+	<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
77 80
 		select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
78 81
 	</select>
79 82
 	

+ 3 - 0
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml

@@ -33,6 +33,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
33 33
     <select id="selectRoleList" parameterType="SysRole" resultMap="SysRoleResult">
34 34
 		<include refid="selectRoleVo"/>
35 35
 		where r.del_flag = '0'
36
+		<if test="roleId != null and roleId != 0">
37
+			AND r.role_id = #{roleId}
38
+		</if>
36 39
 		<if test="roleName != null and roleName != ''">
37 40
 			AND r.role_name like concat('%', #{roleName}, '%')
38 41
 		</if>

+ 3 - 0
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -59,6 +59,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
59 59
 		select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from sys_user u
60 60
 		left join sys_dept d on u.dept_id = d.dept_id
61 61
 		where u.del_flag = '0'
62
+		<if test="userId != null and userId != 0">
63
+			AND u.user_id = #{userId}
64
+		</if>
62 65
 		<if test="userName != null and userName != ''">
63 66
 			AND u.user_name like concat('%', #{userName}, '%')
64 67
 		</if>