6
jinlin
2023-12-03 c8d8a511f45c96ed3a5123a88e48de2ffdbf632a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.zt.modules.sys.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.CacheKey;
import com.zt.common.constant.Constant;
import com.zt.common.service.BaseService;
import com.zt.common.utils.CacheUtils;
import com.zt.core.context.UserContext;
import com.zt.modules.sys.dao.SysRoleDataScopeDao;
import com.zt.modules.sys.model.SysInterface;
import com.zt.modules.sys.model.SysRoleDataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 角色数据权限
 *
 * @author hehz
 * @since 1.0.0
 */
@Service
public class SysRoleDataScopeService extends BaseService<SysRoleDataScopeDao, SysRoleDataScope> {
 
    @Autowired
    private SysInterfaceService sysInterfaceService;
 
    /**
     * 获取当前登录人员拥有的数据范围
     *
     * @param url
     * @return
     */
    public List<SysRoleDataScope> getDataScopeOrCurrentUser(String url) {
        SysInterface interfaceEntity = sysInterfaceService.getInterfaceByUrl(url);
        if (interfaceEntity == null) {
            return null;
        }
        return baseDao.getAuthorizedDataScope(interfaceEntity.getId(), UserContext.getUser().getRoleIdList());
    }
 
    /**
     * 根据角色ID,获取数据权限列表
     *
     * @param roleId
     * @return
     */
    public List<SysRoleDataScope> getByRoleId(Long roleId) {
        return baseDao.getByRoleId(roleId);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<SysRoleDataScope> list) {
        for (SysRoleDataScope scope : list) {
            scope.setRoleId(roleId);
        }
        this.saveRelatedDatas("role_id", roleId, "field_id", list);
 
    }
 
    /**
     * 根据角色id,删除角色数据权限关系
     *
     * @param roleIds
     *            角色ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleIds(Long[] roleIds) {
        this.deleteLogic(baseDao.selectList(new QueryWrapper<SysRoleDataScope>().lambda().in(
                SysRoleDataScope::getRoleId, roleIds)));
 
        CacheUtils.removeAll(Constant.Cache.TOKEN);// 清理用户权限缓存
    }
}