Quellcode durchsuchen

新增角色数据权限配(仅本人数据权限)

RuoYi vor 5 Jahren
Ursprung
Commit
0df49fcfe8

+ 1 - 0
ruoyi-admin/src/main/resources/templates/system/role/dataScope.html

@@ -28,6 +28,7 @@
 						<option value="2" th:field="*{dataScope}">自定数据权限</option>
 						<option value="3" th:field="*{dataScope}">本部门数据权限</option>
 						<option value="4" th:field="*{dataScope}">本部门及以下数据权限</option>
+						<option value="5" th:field="*{dataScope}">仅本人数据权限</option>
 					</select>
 					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 特殊情况下,设置为“自定数据权限”</span>
 				</div>

+ 7 - 2
ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataScope.java

@@ -17,7 +17,12 @@ import java.lang.annotation.Target;
 public @interface DataScope
 {
     /**
-     * 表的别名
+     * 部门表的别名
      */
-    public String tableAlias() default "";
+    public String deptAlias() default "";
+
+    /**
+     * 用户表的别名
+     */
+    public String userAlias() default "";
 }

+ 22 - 5
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java

@@ -44,6 +44,11 @@ public class DataScopeAspect
      */
     public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
 
+    /**
+     * 仅本人数据权限
+     */
+    public static final String DATA_SCOPE_SELF = "5";
+
     /**
      * 数据权限过滤关键字
      */
@@ -76,7 +81,8 @@ public class DataScopeAspect
             // 如果是超级管理员,则不过滤数据
             if (!currentUser.isAdmin())
             {
-                dataScopeFilter(joinPoint, currentUser, controllerDataScope.tableAlias());
+                dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
+                        controllerDataScope.userAlias());
             }
         }
     }
@@ -88,7 +94,7 @@ public class DataScopeAspect
      * @param user 用户
      * @param alias 别名
      */
-    public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String alias)
+    public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias)
     {
         StringBuilder sqlString = new StringBuilder();
 
@@ -103,19 +109,30 @@ public class DataScopeAspect
             else if (DATA_SCOPE_CUSTOM.equals(dataScope))
             {
                 sqlString.append(StringUtils.format(
-                        " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", alias,
+                        " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
                         role.getRoleId()));
             }
             else if (DATA_SCOPE_DEPT.equals(dataScope))
             {
-                sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", alias, user.getDeptId()));
+                sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
             }
             else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
             {
                 String deptChild = user.getDept().getParentId() + "," + user.getDeptId();
                 sqlString.append(StringUtils.format(
                         " OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or ancestors LIKE '%{}%' )",
-                        alias, user.getDeptId(), deptChild));
+                        deptAlias, user.getDeptId(), deptChild));
+            }
+            else if (DATA_SCOPE_SELF.equals(dataScope))
+            {
+                if (StringUtils.isNotBlank(userAlias))
+                {
+                    sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
+                }
+                else
+                {
+                    sqlString.append(StringUtils.format(" OR {}.dept_id IS NULL ", deptAlias));
+                }
             }
         }
 

+ 2 - 2
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -33,7 +33,7 @@ public class SysDeptServiceImpl implements ISysDeptService
      * @return 部门信息集合
      */
     @Override
-    @DataScope(tableAlias = "d")
+    @DataScope(deptAlias = "d")
     public List<SysDept> selectDeptList(SysDept dept)
     {
         return deptMapper.selectDeptList(dept);
@@ -46,7 +46,7 @@ public class SysDeptServiceImpl implements ISysDeptService
      * @return 所有部门信息
      */
     @Override
-    @DataScope(tableAlias = "d")
+    @DataScope(deptAlias = "d")
     public List<Ztree> selectDeptTree(SysDept dept)
     {
         List<SysDept> deptList = deptMapper.selectDeptList(dept);

+ 1 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java

@@ -51,7 +51,7 @@ public class SysRoleServiceImpl implements ISysRoleService
      * @return 角色数据集合信息
      */
     @Override
-    @DataScope(tableAlias = "u")
+    @DataScope(deptAlias = "d")
     public List<SysRole> selectRoleList(SysRole role)
     {
         return roleMapper.selectRoleList(role);

+ 3 - 3
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java

@@ -61,7 +61,7 @@ public class SysUserServiceImpl implements ISysUserService
      * @return 用户信息集合信息
      */
     @Override
-    @DataScope(tableAlias = "u")
+    @DataScope(deptAlias = "d", userAlias = "u")
     public List<SysUser> selectUserList(SysUser user)
     {
         return userMapper.selectUserList(user);
@@ -73,7 +73,7 @@ public class SysUserServiceImpl implements ISysUserService
      * @param user 用户信息
      * @return 用户信息集合信息
      */
-    @DataScope(tableAlias = "u")
+    @DataScope(deptAlias = "d", userAlias = "u")
     public List<SysUser> selectAllocatedList(SysUser user)
     {
         return userMapper.selectAllocatedList(user);
@@ -85,7 +85,7 @@ public class SysUserServiceImpl implements ISysUserService
      * @param user 用户信息
      * @return 用户信息集合信息
      */
-    @DataScope(tableAlias = "u")
+    @DataScope(deptAlias = "d", userAlias = "u")
     public List<SysUser> selectUnallocatedList(SysUser user)
     {
         return userMapper.selectUnallocatedList(user);