From 0df49fcfe8137c26595ba22b4011be7363359799 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 14 Jul 2019 16:00:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A7=92=E8=89=B2=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=83=E9=99=90=E9=85=8D=EF=BC=88=E4=BB=85=E6=9C=AC?= =?UTF-8?q?=E4=BA=BA=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/system/role/dataScope.html | 1 + .../ruoyi/common/annotation/DataScope.java | 9 +++++-- .../framework/aspectj/DataScopeAspect.java | 27 +++++++++++++++---- .../service/impl/SysDeptServiceImpl.java | 4 +-- .../service/impl/SysRoleServiceImpl.java | 2 +- .../service/impl/SysUserServiceImpl.java | 6 ++--- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/ruoyi-admin/src/main/resources/templates/system/role/dataScope.html b/ruoyi-admin/src/main/resources/templates/system/role/dataScope.html index 4e7f73226..7cb3d8cb1 100644 --- a/ruoyi-admin/src/main/resources/templates/system/role/dataScope.html +++ b/ruoyi-admin/src/main/resources/templates/system/role/dataScope.html @@ -28,6 +28,7 @@ + 特殊情况下,设置为“自定数据权限” diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataScope.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataScope.java index 007c3db0a..fe5a01f55 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataScope.java +++ b/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 ""; } 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 2dd1607e1..8f8872d53 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 @@ -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)); + } } } 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 11336457a..422b33643 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 @@ -33,7 +33,7 @@ public class SysDeptServiceImpl implements ISysDeptService * @return 部门信息集合 */ @Override - @DataScope(tableAlias = "d") + @DataScope(deptAlias = "d") public List 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 selectDeptTree(SysDept dept) { List deptList = deptMapper.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 d66afdd5b..caa9f5702 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 @@ -51,7 +51,7 @@ public class SysRoleServiceImpl implements ISysRoleService * @return 角色数据集合信息 */ @Override - @DataScope(tableAlias = "u") + @DataScope(deptAlias = "d") public List selectRoleList(SysRole role) { return roleMapper.selectRoleList(role); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 36299f998..dd62ddeac 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/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 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 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 selectUnallocatedList(SysUser user) { return userMapper.selectUnallocatedList(user);