Search in sources :

Example 11 with Wrappers

use of com.baomidou.mybatisplus.core.toolkit.Wrappers in project matecloud by matevip.

the class SysDepartServiceImpl method getDepart.

private DepartDTO getDepart(Long id) {
    List<SysDepart> departs = this.list(Wrappers.<SysDepart>query().lambda().select(SysDepart::getId, SysDepart::getName, SysDepart::getParentId, SysDepart::getSort, SysDepart::getCreateTime));
    List<DepartDTO> sysDeparts = departs.stream().map(sysDepart -> {
        DepartDTO departDTO = new DepartDTO();
        BeanUtils.copyProperties(sysDepart, departDTO);
        return departDTO;
    }).collect(Collectors.toList());
    Map<Long, DepartDTO> map = sysDeparts.stream().collect(Collectors.toMap(SysDepart::getId, department -> department));
    for (DepartDTO dept : map.values()) {
        DepartDTO parent = map.get(dept.getParentId());
        if (parent != null) {
            List<DepartDTO> children = parent.getChildren() == null ? new ArrayList<>() : parent.getChildren();
            children.add(dept);
            parent.setChildren(children);
        }
    }
    return map.get(id);
}
Also used : ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) SysDepart(vip.mate.system.entity.SysDepart) SysDepartMapper(vip.mate.system.mapper.SysDepartMapper) SysDepartVO(vip.mate.system.vo.SysDepartVO) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) StrUtil(cn.hutool.core.util.StrUtil) ISysDepartService(vip.mate.system.service.ISysDepartService) List(java.util.List) Service(org.springframework.stereotype.Service) Map(java.util.Map) DepartDTO(vip.mate.system.dto.DepartDTO) ForestNodeMerger(vip.mate.core.web.tree.ForestNodeMerger) SysDepartPOI(vip.mate.system.poi.SysDepartPOI) BeanUtils(org.springframework.beans.BeanUtils) SysDepart(vip.mate.system.entity.SysDepart) DepartDTO(vip.mate.system.dto.DepartDTO)

Example 12 with Wrappers

use of com.baomidou.mybatisplus.core.toolkit.Wrappers in project pig by pig-mesh.

the class SysDeptRelationServiceImpl method saveDeptRelation.

/**
 * 维护部门关系
 * @param sysDept 部门
 */
@Override
@Transactional(rollbackFor = Exception.class)
public void saveDeptRelation(SysDept sysDept) {
    // 增加部门关系表
    List<SysDeptRelation> relationList = sysDeptRelationMapper.selectList(Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getDescendant, sysDept.getParentId())).stream().map(relation -> {
        relation.setDescendant(sysDept.getDeptId());
        return relation;
    }).collect(Collectors.toList());
    if (CollUtil.isNotEmpty(relationList)) {
        this.saveBatch(relationList);
    }
    // 自己也要维护到关系表中
    SysDeptRelation own = new SysDeptRelation();
    own.setDescendant(sysDept.getDeptId());
    own.setAncestor(sysDept.getDeptId());
    sysDeptRelationMapper.insert(own);
}
Also used : ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) SysDept(com.pig4cloud.pig.admin.api.entity.SysDept) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) CollUtil(cn.hutool.core.collection.CollUtil) SysDeptRelationMapper(com.pig4cloud.pig.admin.mapper.SysDeptRelationMapper) SysDeptRelationService(com.pig4cloud.pig.admin.service.SysDeptRelationService) List(java.util.List) SysDeptRelation(com.pig4cloud.pig.admin.api.entity.SysDeptRelation) Service(org.springframework.stereotype.Service) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Collectors(java.util.stream.Collectors) Transactional(org.springframework.transaction.annotation.Transactional) SysDeptRelation(com.pig4cloud.pig.admin.api.entity.SysDeptRelation) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with Wrappers

use of com.baomidou.mybatisplus.core.toolkit.Wrappers in project pig by pig-mesh.

the class SysRoleMenuServiceImpl method saveRoleMenus.

/**
 * @param roleId 角色
 * @param menuIds 菜单ID拼成的字符串,每个id之间根据逗号分隔
 * @return
 */
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean saveRoleMenus(Long roleId, String menuIds) {
    this.remove(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getRoleId, roleId));
    if (StrUtil.isBlank(menuIds)) {
        return Boolean.TRUE;
    }
    List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds.split(StrUtil.COMMA)).map(menuId -> {
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenu.setMenuId(Long.valueOf(menuId));
        return roleMenu;
    }).collect(Collectors.toList());
    // 清空userinfo
    Objects.requireNonNull(cacheManager.getCache(CacheConstants.USER_DETAILS)).clear();
    // 清空全部的菜单缓存 fix #I4BM58
    Objects.requireNonNull(cacheManager.getCache(CacheConstants.MENU_DETAILS)).clear();
    return this.saveBatch(roleMenuList);
}
Also used : ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) Arrays(java.util.Arrays) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SysRoleMenuMapper(com.pig4cloud.pig.admin.mapper.SysRoleMenuMapper) CacheConstants(com.pig4cloud.pig.common.core.constant.CacheConstants) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) SysRoleMenuService(com.pig4cloud.pig.admin.service.SysRoleMenuService) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) SysRoleMenu(com.pig4cloud.pig.admin.api.entity.SysRoleMenu) Transactional(org.springframework.transaction.annotation.Transactional) SysRoleMenu(com.pig4cloud.pig.admin.api.entity.SysRoleMenu) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with Wrappers

use of com.baomidou.mybatisplus.core.toolkit.Wrappers in project doorkeeper by limbo-world.

the class ResourceChecker method check.

/**
 * 进行权限校验,是否有资格访问
 *
 * @param userId     用户id
 * @param checkParam 用于获取资源的参数
 * @return
 */
public ResourceCheckResult check(Long userId, ResourceCheckParam checkParam) {
    ClientPO client = getClient(checkParam.getClientId());
    UserPO user = getUser(userId);
    if (!user.getIsEnabled()) {
        return emptyResult();
    }
    try {
        // 找到待检测的启用资源
        List<ResourceVO> resources = findResources(client.getRealmId(), client.getClientId(), checkParam);
        if (CollectionUtils.isEmpty(resources)) {
            return emptyResult();
        }
        // 找到资源权限关系
        List<PermissionResourcePO> permissionResources = permissionResourceMapper.selectList(Wrappers.<PermissionResourcePO>lambdaQuery().in(PermissionResourcePO::getResourceId, resources.stream().map(ResourceVO::getResourceId).collect(Collectors.toList())));
        if (CollectionUtils.isEmpty(permissionResources)) {
            return checkResourceRefuseResult(resources);
        }
        Set<Long> permissionIds = new HashSet<>();
        Map<Long, List<Long>> resourcePermissionMap = new HashMap<>();
        for (PermissionResourcePO permissionResource : permissionResources) {
            permissionIds.add(permissionResource.getPermissionId());
            if (!resourcePermissionMap.containsKey(permissionResource.getResourceId())) {
                resourcePermissionMap.put(permissionResource.getResourceId(), new ArrayList<>());
            }
            resourcePermissionMap.get(permissionResource.getResourceId()).add(permissionResource.getPermissionId());
        }
        // 查询权限
        List<PermissionVO> allPermissions = getPermissions(client.getRealmId(), client.getClientId(), new ArrayList<>(permissionIds));
        if (CollectionUtils.isEmpty(allPermissions)) {
            return checkResourceRefuseResult(resources);
        }
        // 获取策略ID
        Map<Long, PermissionVO> permissionMap = new HashMap<>();
        Set<Long> policyIds = new HashSet<>();
        for (PermissionVO permission : allPermissions) {
            if (Logic.parse(permission.getLogic()) == null) {
                throw new IllegalArgumentException("无法解析权限的策略,permission=" + permission);
            }
            permissionMap.put(permission.getPermissionId(), permission);
            if (CollectionUtils.isNotEmpty(permission.getPolicies())) {
                policyIds.addAll(permission.getPolicies().stream().map(PermissionPolicyVO::getPolicyId).collect(Collectors.toList()));
            }
        }
        if (CollectionUtils.isEmpty(policyIds)) {
            return checkResourceRefuseResult(resources);
        }
        // 获取策略
        List<PolicyVO> allPolicies = policyDao.getVOSByPolicyIds(client.getRealmId(), client.getClientId(), new ArrayList<>(policyIds), true);
        if (CollectionUtils.isEmpty(allPolicies)) {
            return checkResourceRefuseResult(resources);
        }
        Map<Long, PolicyVO> policyMap = allPolicies.stream().collect(Collectors.toMap(PolicyVO::getPolicyId, policyVO -> policyVO));
        // 获取策略校验器
        PolicyChecker checker = policyCheckerFactory.newPolicyChecker(user);
        List<ResourceVO> result = new ArrayList<>();
        ASSIGNER_ITER: for (ResourceVO resource : resources) {
            // 获取资源权限ID
            List<Long> resourcePermissionIds = resourcePermissionMap.get(resource.getResourceId());
            if (CollectionUtils.isEmpty(resourcePermissionIds)) {
                if (refuseWhenUnauthorized) {
                    continue;
                } else {
                    result.add(resource);
                }
            }
            // 获取资源权限
            List<PermissionVO> permissionVOS = new ArrayList<>();
            for (Long permissionId : resourcePermissionIds) {
                if (permissionMap.containsKey(permissionId)) {
                    permissionVOS.add(permissionMap.get(permissionId));
                }
            }
            if (CollectionUtils.isEmpty(permissionVOS)) {
                if (refuseWhenUnauthorized) {
                    continue;
                } else {
                    result.add(resource);
                }
            }
            // 对Permission的Intention进行分组
            Map<Intention, Set<PermissionVO>> intentGroupedPerms = permissionVOS.stream().collect(Collectors.groupingBy(permissionVO -> Intention.parse(permissionVO.getIntention()), Collectors.mapping(Function.identity(), Collectors.toSet())));
            // 先检测 REFUSE 的权限,如果存在一个 REFUSE 的权限校验通过,则此资源约束被看作拒绝
            Set<PermissionVO> refusedPerms = intentGroupedPerms.getOrDefault(Intention.REFUSE, new HashSet<>());
            for (PermissionVO permission : refusedPerms) {
                if (checkPermissionLogic(checker, checkParam, permission, policyMap)) {
                    continue ASSIGNER_ITER;
                }
            }
            // 再检测 ALLOW 的权限
            Set<PermissionVO> allowedPerms = intentGroupedPerms.getOrDefault(Intention.ALLOW, new HashSet<>());
            for (PermissionVO permission : allowedPerms) {
                if (checkPermissionLogic(checker, checkParam, permission, policyMap)) {
                    result.add(resource);
                    continue ASSIGNER_ITER;
                }
            }
        }
        return new ResourceCheckResult(result);
    } catch (Exception e) {
        log.error("鉴权校验失败", e);
        throw new AuthorizationException(e.getMessage());
    }
}
Also used : PolicyVO(org.limbo.doorkeeper.api.model.vo.policy.PolicyVO) java.util(java.util) PolicyDao(org.limbo.doorkeeper.server.infrastructure.dao.PolicyDao) PermissionVO(org.limbo.doorkeeper.api.model.vo.PermissionVO) Intention(org.limbo.doorkeeper.api.constants.Intention) Autowired(org.springframework.beans.factory.annotation.Autowired) ResourceCheckResult(org.limbo.doorkeeper.api.model.vo.check.ResourceCheckResult) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ResourceVO(org.limbo.doorkeeper.api.model.vo.ResourceVO) ResourceQueryParam(org.limbo.doorkeeper.api.model.param.query.ResourceQueryParam) org.limbo.doorkeeper.server.infrastructure.mapper(org.limbo.doorkeeper.server.infrastructure.mapper) org.limbo.doorkeeper.server.infrastructure.po(org.limbo.doorkeeper.server.infrastructure.po) DoorkeeperConstants(org.limbo.doorkeeper.api.constants.DoorkeeperConstants) PermissionPolicyVO(org.limbo.doorkeeper.api.model.vo.PermissionPolicyVO) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) PolicyCheckerParam(org.limbo.doorkeeper.api.model.param.query.PolicyCheckerParam) Collectors(java.util.stream.Collectors) PermissionQueryParam(org.limbo.doorkeeper.api.model.param.query.PermissionQueryParam) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) ResourceCheckParam(org.limbo.doorkeeper.api.model.param.query.ResourceCheckParam) UriMethod(org.limbo.doorkeeper.api.constants.UriMethod) Logic(org.limbo.doorkeeper.api.constants.Logic) AuthorizationException(org.limbo.doorkeeper.server.infrastructure.exception.AuthorizationException) AuthorizationException(org.limbo.doorkeeper.server.infrastructure.exception.AuthorizationException) ResourceCheckResult(org.limbo.doorkeeper.api.model.vo.check.ResourceCheckResult) PermissionPolicyVO(org.limbo.doorkeeper.api.model.vo.PermissionPolicyVO) AuthorizationException(org.limbo.doorkeeper.server.infrastructure.exception.AuthorizationException) PolicyVO(org.limbo.doorkeeper.api.model.vo.policy.PolicyVO) PermissionPolicyVO(org.limbo.doorkeeper.api.model.vo.PermissionPolicyVO) ResourceVO(org.limbo.doorkeeper.api.model.vo.ResourceVO) PermissionVO(org.limbo.doorkeeper.api.model.vo.PermissionVO)

Example 15 with Wrappers

use of com.baomidou.mybatisplus.core.toolkit.Wrappers in project XHuiCloud by sindaZeng.

the class SysRoleMenuServiceImpl method saveRoleMenus.

@Override
@Transactional(rollbackFor = Exception.class)
public Boolean saveRoleMenus(Integer roleId, String menuIds) {
    this.remove(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getRoleId, roleId));
    if (StringUtils.isEmpty(menuIds)) {
        return Boolean.TRUE;
    }
    List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds.split(",")).map(menuId -> {
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenu.setMenuId(Integer.valueOf(menuId));
        return roleMenu;
    }).collect(Collectors.toList());
    return saveBatch(roleMenuList);
}
Also used : ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) SysRoleMenu(com.xhuicloud.upms.entity.SysRoleMenu) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) Arrays(java.util.Arrays) List(java.util.List) SysRoleMenuService(com.xhuicloud.upms.service.SysRoleMenuService) Service(org.springframework.stereotype.Service) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) SysRoleMenuMapper(com.xhuicloud.upms.mapper.SysRoleMenuMapper) Transactional(org.springframework.transaction.annotation.Transactional) SysRoleMenu(com.xhuicloud.upms.entity.SysRoleMenu) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)16 Collectors (java.util.stream.Collectors)15 Service (org.springframework.stereotype.Service)14 List (java.util.List)13 Transactional (org.springframework.transaction.annotation.Transactional)11 ServiceImpl (com.baomidou.mybatisplus.extension.service.impl.ServiceImpl)8 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 ArrayList (java.util.ArrayList)4 RequiredArgsConstructor (lombok.RequiredArgsConstructor)4 StrUtil (cn.hutool.core.util.StrUtil)3 Arrays (java.util.Arrays)3 Slf4j (lombok.extern.slf4j.Slf4j)3 SecurityContextUtils (cn.surveyking.server.core.uitls.SecurityContextUtils)2 BaseService (cn.surveyking.server.service.BaseService)2 UserService (cn.surveyking.server.service.UserService)2 RoleMenuDo (com.albedo.java.modules.sys.domain.RoleMenuDo)2 RoleMenuService (com.albedo.java.modules.sys.service.RoleMenuService)2 SysCacheUtil (com.albedo.java.modules.sys.util.SysCacheUtil)2 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)2