同样的页面,不同的账号登录进去,看到的数据不一样。

角色与数据权限的关系:
比如管理员角色,他的数据权限是全部,那么拥有该角色的用户,所能看到的数据权限就是全部部门的数据;
比如部门领导的角色,如(朝阳区经信局的部门领导),能看到整个部门以及下属部门的数据;
比如某部门处理专员角色,仅能看到该部门的数据。

- public @interface DataAuthority {
-
- }
- @Data
- public class BaseDeptParam {
- /**
- * 可以查看的部门数据
- */
- private List<Long> deptIds;
-
- }
- @Data
- public class RequestParam extends BaseDeptParam {
-
- private String id;
-
- private String name;
-
- }
在需要数据权限的接口上,加上@DataAuthority的注解
- @DataAuthority
- @GetMapping(value = "/userList")
- public R
> userList(RequestParam param) { - return taskStatisticsService.handlingList(param);
- }
- select * from user b
- where b.flag = 0
- <if test="deptIds != null and deptIds.size()>0">
- AND b.dept_id IN
- <foreach collection="deptIds" open="(" close=")" item="item" index="index" separator=",">
- #{item}
- </foreach>
- </if>
- @Aspect
- @Component
- public class DataAuthorityService {
-
- @Pointcut("@annotation(com.xx.xxx.xx.annotation.DataAuthority)")
- public void dataAuthorityPointCut() {
- }
-
- @Before("dataAuthorityPointCut()")
- public void before(JoinPoint joinPoint) {
- // 根据当前登录的用户id 查询用户的角色,再根据角色id,查询角色管理的部门ids,给deptIds赋值
- Long userId = getUserId();
- List<Long> roleIds = getRoleIdsByUserId();
- List<Long> deptIds = getDeptIdsByRoleIds();
- for (Object arg : args) {
- if (arg instanceof BaseDeptParam) {
- ((BaseDeptParam) arg).setDeptIds(deptIds);
- }
- }