| | |
| | | package com.oying.modules.system.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.oying.modules.system.domain.Dept; |
| | | import com.oying.modules.system.domain.Role; |
| | | import com.oying.modules.system.domain.User; |
| | | import lombok.RequiredArgsConstructor; |
| | | import com.oying.modules.system.service.DataService; |
| | | import com.oying.modules.system.service.DeptService; |
| | | import com.oying.modules.system.service.RoleService; |
| | | import com.oying.utils.CacheKey; |
| | | import com.oying.utils.RedisUtils; |
| | |
| | | |
| | | private final RedisUtils redisUtils; |
| | | private final RoleService roleService; |
| | | private final DeptService deptService; |
| | | |
| | | /** |
| | | * 用户角色和用户机构改变时需清理缓存 |
| | |
| | | * @return / |
| | | */ |
| | | @Override |
| | | public List<Long> getDeptIds(User user) { |
| | | public List<Long> getDataIds(User user) { |
| | | String key = CacheKey.DATA_USER + user.getId(); |
| | | List<Long> ids = redisUtils.getList(key, Long.class); |
| | | if (CollUtil.isEmpty(ids)) { |
| | | Set<Long> deptIds = new HashSet<>(); |
| | | Set<Long> dataIds = new HashSet<>(); |
| | | // 查询用户角色 |
| | | List<Role> roleList = roleService.findByUsersId(user.getId()); |
| | | // 获取对应的机构ID |
| | |
| | | DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope()); |
| | | switch (Objects.requireNonNull(dataScopeEnum)) { |
| | | case THIS_LEVEL: |
| | | deptIds.add(user.getDept().getId()); |
| | | break; |
| | | case CUSTOMIZE: |
| | | deptIds.addAll(getCustomize(deptIds, role)); |
| | | dataIds.addAll(getCustomize(dataIds, role)); |
| | | break; |
| | | default: |
| | | return new ArrayList<>(); |
| | | } |
| | | } |
| | | ids = new ArrayList<>(deptIds); |
| | | ids = new ArrayList<>(dataIds); |
| | | redisUtils.set(key, ids, 1, TimeUnit.DAYS); |
| | | } |
| | | return ids; |
| | |
| | | |
| | | /** |
| | | * 获取自定义的数据权限 |
| | | * @param deptIds 机构ID |
| | | * @param dataIds 机构ID |
| | | * @param role 角色 |
| | | * @return 数据权限ID |
| | | */ |
| | | public Set<Long> getCustomize(Set<Long> deptIds, Role role){ |
| | | Set<Dept> depts = deptService.findByRoleId(role.getId()); |
| | | for (Dept dept : depts) { |
| | | deptIds.add(dept.getId()); |
| | | List<Dept> deptChildren = deptService.findByPid(dept.getId()); |
| | | if (CollUtil.isNotEmpty(deptChildren)) { |
| | | deptIds.addAll(deptService.getDeptChildren(deptChildren)); |
| | | } |
| | | } |
| | | return deptIds; |
| | | public Set<Long> getCustomize(Set<Long> dataIds, Role role){ |
| | | System.out.println(role); |
| | | return dataIds; |
| | | } |
| | | } |