houzhongjian
2024-12-26 b482efaf698d5fd3920ff63ccde1c819f9eb34ba
iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/PermissionServiceImpl.java
@@ -8,15 +8,22 @@
import com.iailab.framework.common.util.collection.CollectionUtils;
import com.iailab.framework.datapermission.core.annotation.DataPermission;
import com.iailab.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
import com.iailab.module.system.dal.dataobject.app.AppDO;
import com.iailab.module.system.dal.dataobject.permission.MenuDO;
import com.iailab.module.system.dal.dataobject.permission.RoleDO;
import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO;
import com.iailab.module.system.dal.dataobject.permission.UserRoleDO;
import com.iailab.module.system.dal.dataobject.tenant.TenantDO;
import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO;
import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper;
import com.iailab.module.system.dal.mysql.permission.UserRoleMapper;
import com.iailab.module.system.dal.redis.RedisKeyConstants;
import com.iailab.module.system.enums.permission.DataScopeEnum;
import com.iailab.module.system.service.app.AppService;
import com.iailab.module.system.service.dept.DeptService;
import com.iailab.module.system.service.tenant.TenantPackageService;
import com.iailab.module.system.service.tenant.TenantService;
import com.iailab.module.system.service.user.AdminUserService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.google.common.annotations.VisibleForTesting;
@@ -35,6 +42,7 @@
import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet;
import static com.iailab.framework.common.util.json.JsonUtils.toJsonString;
import static com.iailab.framework.tenant.core.context.TenantContextHolder.getTenantId;
/**
 * 权限 Service 实现类
@@ -58,6 +66,13 @@
    private DeptService deptService;
    @Resource
    private AdminUserService userService;
    @Resource
    private TenantService tenantService;
    @Resource
    private TenantPackageService tenantPackageService;
    @Resource
    private AppService appService;
    @Override
    public boolean hasAnyPermissions(Long userId, String... permissions) {
@@ -155,6 +170,37 @@
        }
    }
    // ========== 角色-菜单的相关方法  ==========
//    @Override
//    @DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
//    @CacheEvict(value = RedisKeyConstants.MENU_ROLE_ID_LIST,
//            allEntries = true) // allEntries 清空所有缓存,主要一次更新涉及到的 menuIds 较多,反倒批量会更快
//    public void assignRoleAppMenu(Long roleId, Set<Long> menuIds) {
//        // 获得角色拥有应用菜单编号
//        MenuListReqVO reqVO = new MenuListReqVO();
//        List<MenuDO> appMenuList = menuService.getAppMenuList(reqVO);
//        Set<Long> appMenuIds = convertSet(appMenuList, MenuDO::getId);
//        Set<Long> dbMenuIds = convertSet(roleMenuMapper.selectListByRoleId(roleId), RoleMenuDO::getMenuId);
//        dbMenuIds.retainAll(appMenuIds);
//        // 计算新增和删除的菜单编号
//        Set<Long> menuIdList = CollUtil.emptyIfNull(menuIds);
//        Collection<Long> createMenuIds = CollUtil.subtract(menuIdList, dbMenuIds);
//        Collection<Long> deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIdList);
//        // 执行新增和删除。对于已经授权的菜单,不用做任何处理
//        if (CollUtil.isNotEmpty(createMenuIds)) {
//            roleMenuMapper.insertBatch(CollectionUtils.convertList(createMenuIds, menuId -> {
//                RoleMenuDO entity = new RoleMenuDO();
//                entity.setRoleId(roleId);
//                entity.setMenuId(menuId);
//                return entity;
//            }));
//        }
//        if (CollUtil.isNotEmpty(deleteMenuIds)) {
//            roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
//        }
//    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
@@ -181,16 +227,35 @@
        if (CollUtil.isEmpty(roleIds)) {
            return Collections.emptySet();
        }
        // 如果是管理员的情况下,获取全部菜单编号
        if (roleService.hasAnySuperAdmin(roleIds)) {
            return convertSet(menuService.getMenuList(), MenuDO::getId);
        }
        // 如果是非管理员的情况下,获得拥有的菜单编号
        return convertSet(roleMenuMapper.selectListByRoleId(roleIds), RoleMenuDO::getMenuId);
    }
    @Override
    public Set<Long> getRoleAppMenuListByRoleId(Collection<Long> roleIds) {
        if (CollUtil.isEmpty(roleIds)) {
            return Collections.emptySet();
        }
        // 获取 tenantId
        Long tenantId = getTenantId();
        // 如果是管理员的情况下,获取全部应用菜单编号
        if (roleService.hasAnySuperAdmin(roleIds)) {
            MenuListReqVO reqVO = new MenuListReqVO();
            return convertSet(menuService.getAppMenuList(tenantId, reqVO), MenuDO::getId);
        }
        // 如果是非管理员的情况下,获得拥有的应用菜单编号
        TenantDO tenant = tenantService.getTenant(tenantId);
        TenantPackageDO tenantPackage = tenantPackageService.getTenantPackage(tenant.getPackageId());
        Set<Long> menuIds = tenantPackage.getMenuIds();
        Set<Long> longs = convertSet(roleMenuMapper.selectListByRoleId(roleIds), RoleMenuDO::getMenuId);
        longs.retainAll(menuIds);
        return longs;
    }
    @Override
    @Cacheable(value = RedisKeyConstants.MENU_ROLE_ID_LIST, key = "#menuId")
    public Set<Long> getMenuRoleIdListByMenuIdFromCache(Long menuId) {
        return convertSet(roleMenuMapper.selectListByMenuId(menuId), RoleMenuDO::getRoleId);