Search in sources :

Example 1 with IamResourcePermissionDTO

use of com.diboot.iam.dto.IamResourcePermissionDTO in project diboot by dibo-software.

the class IamResourcePermissionServiceImpl method updateMenuAndPermissions.

@Override
@Transactional(rollbackFor = Exception.class)
public void updateMenuAndPermissions(IamResourcePermissionDTO iamResourcePermissionDTO) {
    // 检查是否设置了自身id为parentId,如果设置parentId与自身id相同,将会导致非常严重的潜在隐患
    if (V.equals(iamResourcePermissionDTO.getId(), iamResourcePermissionDTO.getParentId())) {
        throw new BusinessException(Status.FAIL_OPERATION, "不可设置父级菜单资源为自身");
    }
    // 设置menu的接口列表
    if (V.notEmpty(iamResourcePermissionDTO.getApiSetList())) {
        iamResourcePermissionDTO.setApiSet(S.join(iamResourcePermissionDTO.getApiSetList(), ","));
    }
    // 更新menu
    this.updateEntity(iamResourcePermissionDTO);
    List<IamResourcePermissionDTO> permissionList = iamResourcePermissionDTO.getPermissionList();
    permissionList.forEach(p -> {
        p.setParentId(iamResourcePermissionDTO.getId());
        p.setDisplayType(Cons.RESOURCE_PERMISSION_DISPLAY_TYPE.PERMISSION.name());
    });
    // 需要更新的列表
    List<IamResourcePermissionDTO> updatePermissionList = permissionList.stream().filter(p -> V.notEmpty(p.getId())).collect(Collectors.toList());
    // 需要新建的列表
    List<IamResourcePermissionDTO> createPermissionDTOList = permissionList.stream().filter(p -> V.isEmpty(p.getId())).collect(Collectors.toList());
    List<Long> updatePermissionIdList = updatePermissionList.stream().map(IamResourcePermission::getId).collect(Collectors.toList());
    // 批量删除不存在的按钮/权限列表
    List<IamResourcePermission> oldPermissionList = this.getEntityList(Wrappers.<IamResourcePermission>lambdaQuery().eq(IamResourcePermission::getParentId, iamResourcePermissionDTO.getId()).eq(IamResourcePermission::getDisplayType, Cons.RESOURCE_PERMISSION_DISPLAY_TYPE.PERMISSION));
    if (V.notEmpty(oldPermissionList)) {
        LambdaQueryWrapper<IamResourcePermission> deleteWrapper = Wrappers.<IamResourcePermission>lambdaQuery().eq(IamResourcePermission::getParentId, iamResourcePermissionDTO.getId()).eq(IamResourcePermission::getDisplayType, Cons.RESOURCE_PERMISSION_DISPLAY_TYPE.PERMISSION);
        if (V.notEmpty(updatePermissionIdList)) {
            deleteWrapper.notIn(IamResourcePermission::getId, updatePermissionIdList);
        }
        this.deleteEntities(deleteWrapper);
    }
    // 批量新建按钮/权限列表
    if (V.notEmpty(createPermissionDTOList)) {
        List<IamResourcePermission> createPermissionList = BeanUtils.convertList(createPermissionDTOList, IamResourcePermission.class);
        this.createEntities(createPermissionList);
    }
    // 批量更新按钮/权限列表
    if (V.notEmpty(updatePermissionList)) {
        for (IamResourcePermissionDTO updatePermission : updatePermissionList) {
            this.updateEntity(updatePermission);
        }
    }
    // 检测是否有脏数据存在
    if (hasDirtyData()) {
        throw new BusinessException(Status.FAIL_OPERATION, "父级节点不可设置在自己的子节点上");
    }
// 清理脏数据
// this.clearDirtyData();
}
Also used : Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) RelationsBinder(com.diboot.core.binding.RelationsBinder) InvalidResourcePermissionVO(com.diboot.iam.vo.InvalidResourcePermissionVO) java.util(java.util) IamCacheManager(com.diboot.iam.cache.IamCacheManager) Status(com.diboot.core.vo.Status) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) BusinessException(com.diboot.core.exception.BusinessException) IamResourcePermissionMapper(com.diboot.iam.mapper.IamResourcePermissionMapper) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) S(com.diboot.core.util.S) V(com.diboot.core.util.V) Service(org.springframework.stereotype.Service) Cons(com.diboot.iam.config.Cons) IamResourcePermissionService(com.diboot.iam.service.IamResourcePermissionService) IamResourcePermissionDTO(com.diboot.iam.dto.IamResourcePermissionDTO) IamResourcePermissionListVO(com.diboot.iam.vo.IamResourcePermissionListVO) BeanUtils(com.diboot.core.util.BeanUtils) IamResourcePermission(com.diboot.iam.entity.IamResourcePermission) Transactional(org.springframework.transaction.annotation.Transactional) BusinessException(com.diboot.core.exception.BusinessException) IamResourcePermissionDTO(com.diboot.iam.dto.IamResourcePermissionDTO) IamResourcePermission(com.diboot.iam.entity.IamResourcePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 RelationsBinder (com.diboot.core.binding.RelationsBinder)1 BusinessException (com.diboot.core.exception.BusinessException)1 BeanUtils (com.diboot.core.util.BeanUtils)1 S (com.diboot.core.util.S)1 V (com.diboot.core.util.V)1 Status (com.diboot.core.vo.Status)1 IamCacheManager (com.diboot.iam.cache.IamCacheManager)1 Cons (com.diboot.iam.config.Cons)1 IamResourcePermissionDTO (com.diboot.iam.dto.IamResourcePermissionDTO)1 IamResourcePermission (com.diboot.iam.entity.IamResourcePermission)1 IamResourcePermissionMapper (com.diboot.iam.mapper.IamResourcePermissionMapper)1 IamResourcePermissionService (com.diboot.iam.service.IamResourcePermissionService)1 IamResourcePermissionListVO (com.diboot.iam.vo.IamResourcePermissionListVO)1 InvalidResourcePermissionVO (com.diboot.iam.vo.InvalidResourcePermissionVO)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Service (org.springframework.stereotype.Service)1