Search in sources :

Example 1 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class IamPositionServiceImpl method updateUserPositionRelations.

@Transactional(rollbackFor = Exception.class)
@Override
public boolean updateUserPositionRelations(String userType, Long userId, List<IamUserPosition> userPositionList) {
    if (V.isEmpty(userType) || V.isEmpty(userId)) {
        throw new BusinessException(Status.FAIL_OPERATION, "参数错误");
    }
    // 校验用户ID是否存在
    if (V.notEmpty(userPositionList)) {
        for (IamUserPosition userPosition : userPositionList) {
            userPosition.setUserType(userType);
            userPosition.setUserId(userId);
        }
    }
    // 删除所有旧关联数据
    LambdaQueryWrapper deleteWrapper = Wrappers.<IamUserPosition>lambdaQuery().eq(IamUserPosition::getUserType, userType).eq(IamUserPosition::getUserId, userId);
    long count = iamUserPositionMapper.selectCount(deleteWrapper);
    if (count > 0) {
        iamUserPositionMapper.delete(deleteWrapper);
    }
    // 批量设置新的岗位列表
    if (V.isEmpty(userPositionList)) {
        return true;
    }
    for (IamUserPosition userPosition : userPositionList) {
        userPosition.setId(null);
        iamUserPositionMapper.insert(userPosition);
    }
    return true;
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamUserPosition(com.diboot.iam.entity.IamUserPosition) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BusinessException

use of com.diboot.core.exception.BusinessException 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)

Example 3 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class IamResourcePermissionServiceImpl method sortList.

@Override
public void sortList(List<IamResourcePermission> permissionList) {
    if (V.isEmpty(permissionList)) {
        throw new BusinessException(Status.FAIL_OPERATION, "排序列表不能为空");
    }
    List<Long> sortIdList = new ArrayList();
    // 先将所有序号重新设置为自身当前id
    for (IamResourcePermission item : permissionList) {
        item.setSortId(item.getId());
        sortIdList.add(item.getSortId());
    }
    // 将序号列表倒序排序
    sortIdList = sortIdList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    // 整理需要更新的列表
    List<IamResourcePermission> updateList = new ArrayList<>();
    for (int i = 0; i < permissionList.size(); i++) {
        IamResourcePermission item = permissionList.get(i);
        IamResourcePermission updateItem = new IamResourcePermission();
        updateItem.setId(item.getId());
        updateItem.setSortId(sortIdList.get(i));
        updateList.add(updateItem);
    }
    if (updateList.size() > 0) {
        super.updateBatchById(updateList);
    }
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamResourcePermission(com.diboot.iam.entity.IamResourcePermission)

Example 4 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class IamResourcePermissionServiceImpl method deepCreatePermissionAndChildren.

@Override
@Transactional(rollbackFor = Exception.class)
public void deepCreatePermissionAndChildren(IamResourcePermissionListVO iamResourcePermissionListVO) {
    if (iamResourcePermissionListVO == null) {
        return;
    }
    IamResourcePermission iamResourcePermission = (IamResourcePermission) iamResourcePermissionListVO;
    if (!super.createEntity(iamResourcePermission)) {
        log.warn("新建资源权限失败,displayType=" + iamResourcePermission.getDisplayType());
        throw new BusinessException(Status.FAIL_OPERATION, "新建资源权限失败");
    }
    List<IamResourcePermissionListVO> children = iamResourcePermissionListVO.getChildren();
    if (V.notEmpty(children)) {
        for (IamResourcePermissionListVO vo : children) {
            vo.setParentId(iamResourcePermission.getId());
            this.deepCreatePermissionAndChildren(vo);
        }
    }
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamResourcePermissionListVO(com.diboot.iam.vo.IamResourcePermissionListVO) IamResourcePermission(com.diboot.iam.entity.IamResourcePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class PwdAuthServiceImpl method applyToken.

@Override
public String applyToken(AuthCredential credential) {
    BaseJwtAuthToken authToken = initBaseJwtAuthToken(credential);
    try {
        Subject subject = SecurityUtils.getSubject();
        subject.login(authToken);
        if (subject.isAuthenticated()) {
            log.debug("申请token成功!authtoken={}", authToken.getCredentials());
            saveLoginTrace(authToken, true);
            // 跳转到首页
            return (String) authToken.getCredentials();
        } else {
            log.error("认证失败");
            saveLoginTrace(authToken, false);
            throw new BusinessException(Status.FAIL_OPERATION, "认证失败");
        }
    } catch (Exception e) {
        log.error("登录异常", e);
        saveLoginTrace(authToken, false);
        throw new BusinessException(Status.FAIL_OPERATION, e.getMessage());
    }
}
Also used : BaseJwtAuthToken(com.diboot.iam.jwt.BaseJwtAuthToken) BusinessException(com.diboot.core.exception.BusinessException) Subject(org.apache.shiro.subject.Subject) BusinessException(com.diboot.core.exception.BusinessException) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Aggregations

BusinessException (com.diboot.core.exception.BusinessException)29 Transactional (org.springframework.transaction.annotation.Transactional)8 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)5 IamAccount (com.diboot.iam.entity.IamAccount)5 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)4 IamMember (com.diboot.mobile.entity.IamMember)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 Dictionary (com.diboot.core.entity.Dictionary)3 UploadFile (com.diboot.file.entity.UploadFile)3 IamResourcePermission (com.diboot.iam.entity.IamResourcePermission)3 BaseJwtAuthToken (com.diboot.iam.jwt.BaseJwtAuthToken)3 IamResourcePermissionListVO (com.diboot.iam.vo.IamResourcePermissionListVO)3 ScheduleJob (com.diboot.scheduler.entity.ScheduleJob)3 HashMap (java.util.HashMap)3 WxOAuth2AccessToken (me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken)3 AuthenticationException (org.apache.shiro.authc.AuthenticationException)3 Subject (org.apache.shiro.subject.Subject)3 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)2 BaseService (com.diboot.core.service.BaseService)2 DictionaryService (com.diboot.core.service.DictionaryService)2