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

pull/108/head
RuoYi 2019-07-14 16:00:14 +08:00
parent fcde715553
commit 0df49fcfe8
6 changed files with 36 additions and 13 deletions

View File

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

View File

@ -17,7 +17,12 @@ import java.lang.annotation.Target;
public @interface DataScope public @interface DataScope
{ {
/** /**
* *
*/ */
public String tableAlias() default ""; public String deptAlias() default "";
/**
*
*/
public String userAlias() default "";
} }

View File

@ -44,6 +44,11 @@ public class DataScopeAspect
*/ */
public static final String DATA_SCOPE_DEPT_AND_CHILD = "4"; 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()) 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 user
* @param alias * @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(); StringBuilder sqlString = new StringBuilder();
@ -103,19 +109,30 @@ public class DataScopeAspect
else if (DATA_SCOPE_CUSTOM.equals(dataScope)) else if (DATA_SCOPE_CUSTOM.equals(dataScope))
{ {
sqlString.append(StringUtils.format( 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())); role.getRoleId()));
} }
else if (DATA_SCOPE_DEPT.equals(dataScope)) 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)) else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
{ {
String deptChild = user.getDept().getParentId() + "," + user.getDeptId(); String deptChild = user.getDept().getParentId() + "," + user.getDeptId();
sqlString.append(StringUtils.format( sqlString.append(StringUtils.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or ancestors LIKE '%{}%' )", " 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));
}
} }
} }

View File

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

View File

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

View File

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