Search in sources :

Example 1 with BusinessException

use of com.hb0730.boot.admin.exceptions.BusinessException in project boot-admin by hb0730.

the class LogAspectj method handleLog.

/**
 * 日志处理
 */
void handleLog(final JoinPoint joinPoint, Object jsonResult, final Exception e) {
    try {
        Log log = getAnnotationLog(joinPoint);
        if (check(joinPoint, log)) {
            return;
        }
        // 当前用户
        OperLogEntity entity = new OperLogEntity();
        // 状态
        entity.setStatus(StatusEnum.SUCCESS.getValue());
        // 操作类型
        entity.setOperType(log.businessType().getValue());
        // 请求用户
        User currentUser = SecurityUtils.getCurrentUser();
        if (null != currentUser) {
            entity.setUsername(currentUser.getUsername());
            entity.setCreateUserId(currentUser.getId());
            entity.setCreateTime(LocalDateTimeUtils.now());
        }
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        // 请求ip
        String ip = "";
        // 请求地址
        String requestUrl = "";
        // 请求方式
        String requestMethod = "";
        if (attributes instanceof ServletRequestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
            ip = ServletUtil.getClientIP(request);
            // 请求地址
            requestUrl = request.getRequestURI();
            requestMethod = request.getMethod();
        }
        entity.setOperIp(ip);
        // 描述
        // 类描述
        String controllerDescription = "";
        ClassDescribe classDescribe = joinPoint.getTarget().getClass().getAnnotation(ClassDescribe.class);
        if (null != classDescribe) {
            controllerDescription = classDescribe.value();
        }
        // 方法描述
        String controllerMethodDescription = getDescribe(log);
        if (log.controllerApiValue()) {
            entity.setDescription(StringUtils.join(controllerDescription, controllerMethodDescription));
        } else {
            entity.setDescription(controllerMethodDescription);
        }
        // 请求地址
        entity.setRequestUrl(requestUrl);
        // 请求方式
        entity.setRequestMethod(requestMethod);
        // 操作方法
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        entity.setOperMethod(className + "." + methodName);
        // 请求参数
        if (log.request()) {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            String requestParamsValue = getRequestValue(joinPoint, request, log.paramsName());
            entity.setRequestParams(requestParamsValue);
        }
        // 返回参数
        if (log.response()) {
            String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
            entity.setRequestResult(result);
        }
        if (null != e) {
            if (log.requestByError()) {
                String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
                entity.setRequestResult(result);
                String message = ExceptionUtils.getExceptionMessage(e);
                entity.setErrorMessage(StringUtils.substring(message, 0, 2000));
                entity.setStatus(StatusEnum.FAIL.getValue());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordOperLog(entity));
    } catch (Exception e1) {
        e1.printStackTrace();
        if (null != e) {
            e.printStackTrace();
        }
        // 记录本地异常日志
        LOGGER.error("==前置通知异常==");
        LOGGER.error("异常信息:{}", e1.getMessage());
        if (null != e) {
            throw new BusinessException(e.getMessage());
        } else {
            throw new BusinessException(e1.getMessage());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) User(com.hb0730.boot.admin.security.model.User) Log(com.hb0730.boot.admin.annotation.Log) OperLogEntity(com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ClassDescribe(com.hb0730.boot.admin.annotation.ClassDescribe) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException)

Example 2 with BusinessException

use of com.hb0730.boot.admin.exceptions.BusinessException in project boot-admin by hb0730.

the class RolePermissionServiceImpl method savePermissionIdByRoleId.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean savePermissionIdByRoleId(@Nonnull Long id, @Nonnull Collection<Long> permissionIds) {
    Assert.notNull(id, "角色id为空");
    Assert.notEmpty(permissionIds, "权限id为空");
    LambdaQueryWrapper<PermissionEntity> queryWrapper = Wrappers.lambdaQuery(PermissionEntity.class).in(PermissionEntity::getId, permissionIds).select(PermissionEntity::getId);
    List<PermissionEntity> entities = permissionMapper.selectList(queryWrapper);
    if (CollectionUtils.isEmpty(entities)) {
        throw new BusinessException("请传入正确的权限id");
    }
    Set<Long> ids = entities.parallelStream().map(PermissionEntity::getId).collect(Collectors.toSet());
    List<RolePermissionEntity> rolePermissionEntities = new ArrayList<>(ids.size());
    for (Long permissionId : ids) {
        RolePermissionEntity entity = new RolePermissionEntity();
        entity.setPermissionId(permissionId);
        entity.setRoleId(id);
        rolePermissionEntities.add(entity);
    }
    return super.saveBatch(rolePermissionEntities);
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BusinessException

use of com.hb0730.boot.admin.exceptions.BusinessException in project boot-admin by hb0730.

the class UserInfoServiceImpl method save.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(@Nonnull UserInfoDTO dto) {
    UserInfoEntity entity = dto.convertTo();
    // 1.账号是否存在
    String username = dto.getUsername();
    if (StringUtils.isBlank(username)) {
        throw new BusinessException("用户账号不为空");
    }
    UserAccountEntity account = accountService.findUserAccountByUsername(username);
    if (Objects.nonNull(account)) {
        throw new BusinessException("用户账号已存在,请重新设置");
    }
    boolean result = super.save(entity);
    // 保存 用户账号
    account = new UserAccountEntity();
    account.setUsername(username);
    // 默认密码
    String password = DictUtils.getEntryValue(TYPE, DEFAULT_PASSWORD);
    if (StringUtils.isBlank(password)) {
        password = DEFAULT_PASSWORD_VALUE;
    }
    account.setPassword(PasswordSecurityUtils.encode(SecurityUtils.getPasswordEncoder(), password));
    account.setUserId(entity.getId());
    accountService.save(account);
    // 保存 用户角色
    Collection<Long> roleIds = dto.getRoleIds();
    if (!CollectionUtils.isEmpty(roleIds)) {
        userRoleService.updateUserRole(entity.getId(), roleIds);
    }
    // 保存 用户岗位
    Collection<Long> postIds = dto.getPostIds();
    if (!CollectionUtils.isEmpty(postIds)) {
        userPostService.updateUserPost(entity.getId(), postIds);
    }
    return result;
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity) UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with BusinessException

use of com.hb0730.boot.admin.exceptions.BusinessException in project boot-admin by hb0730.

the class DictServiceImpl method verify.

private void verify(DictEntity entity, boolean isUpdate) {
    if (null == entity) {
        throw new BusinessException("数据字典信息为空");
    }
    String type = entity.getType();
    LambdaQueryWrapper<DictEntity> queryWrapper = Wrappers.lambdaQuery(DictEntity.class).eq(DictEntity::getType, type);
    if (isUpdate) {
        queryWrapper.ne(DictEntity::getId, entity.getId());
    }
    long count = super.count(queryWrapper);
    if (isUpdate && count > 0) {
        throw new BusinessException("数据字典类型已存在");
    } else if (!isUpdate && count > 0) {
        throw new BusinessException("数据字典类型已存在");
    }
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) DictEntity(com.hb0730.boot.admin.project.system.dict.model.entity.DictEntity)

Example 5 with BusinessException

use of com.hb0730.boot.admin.exceptions.BusinessException in project boot-admin by hb0730.

the class UserAccountServiceImpl method save.

@Override
public boolean save(@NonNull UserAccountDTO dto) {
    ValidatorUtils.validate(dto);
    String username = dto.getUsername();
    Long userId = dto.getUserId();
    UserAccountEntity entity = userAccountByUserId(userId);
    if (null != entity) {
        throw new BusinessException("用户id已绑定账号,请勿重新绑定");
    }
    entity = findUserAccountByUsername(username);
    if (null != entity) {
        throw new BusinessException("用户账号已存在,请重新设置");
    }
    entity = dto.convertTo();
    return super.save(entity);
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)

Aggregations

BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)7 Log (com.hb0730.boot.admin.annotation.Log)2 UserAccountEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)2 UserInfoEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ClassDescribe (com.hb0730.boot.admin.annotation.ClassDescribe)1 OperLogEntity (com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity)1 DictEntity (com.hb0730.boot.admin.project.system.dict.model.entity.DictEntity)1 PermissionEntity (com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)1 PostEntity (com.hb0730.boot.admin.project.system.post.model.entity.PostEntity)1 RolePermissionEntity (com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity)1 User (com.hb0730.boot.admin.security.model.User)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1