Search in sources :

Example 46 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class PlusDataPermissionHandler method getSqlSegment.

public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
    DataColumn[] dataColumns = findAnnotation(mappedStatementId);
    if (ArrayUtil.isEmpty(dataColumns)) {
        inavlidCacheSet.add(mappedStatementId);
        return where;
    }
    LoginUser currentUser = DataPermissionHelper.getVariable("user");
    if (ObjectUtil.isNull(currentUser)) {
        currentUser = LoginHelper.getLoginUser();
        DataPermissionHelper.setVariable("user", currentUser);
    }
    // 如果是超级管理员,则不过滤数据
    if (LoginHelper.isAdmin()) {
        return where;
    }
    String dataFilterSql = buildDataFilter(dataColumns, isSelect);
    if (StringUtils.isBlank(dataFilterSql)) {
        return where;
    }
    try {
        Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql);
        // 数据权限使用单独的括号 防止与其他条件冲突
        Parenthesis parenthesis = new Parenthesis(expression);
        if (ObjectUtil.isNotNull(where)) {
            return new AndExpression(where, parenthesis);
        } else {
            return parenthesis;
        }
    } catch (JSQLParserException e) {
        throw new ServiceException("数据权限解析异常 => " + e.getMessage());
    }
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) ServiceException(com.ruoyi.common.exception.ServiceException) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) JSQLParserException(net.sf.jsqlparser.JSQLParserException) DataColumn(com.ruoyi.common.annotation.DataColumn) LoginUser(com.ruoyi.common.core.domain.model.LoginUser)

Example 47 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class PlusDataPermissionHandler method buildDataFilter.

/**
 * 构造数据过滤sql
 */
private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) {
    StringBuilder sqlString = new StringBuilder();
    // 更新或删除需满足所有条件
    String joinStr = isSelect ? " OR " : " AND ";
    LoginUser user = DataPermissionHelper.getVariable("user");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(beanResolver);
    DataPermissionHelper.getContext().forEach(context::setVariable);
    for (RoleDTO role : user.getRoles()) {
        user.setRoleId(role.getRoleId());
        // 获取角色权限泛型
        DataScopeType type = DataScopeType.findCode(role.getDataScope());
        if (ObjectUtil.isNull(type)) {
            throw new ServiceException("角色数据范围异常 => " + role.getDataScope());
        }
        // 全部数据权限直接返回
        if (type == DataScopeType.ALL) {
            return "";
        }
        boolean isSuccess = false;
        for (DataColumn dataColumn : dataColumns) {
            // 不包含 key 变量 则不处理
            if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) {
                continue;
            }
            // 设置注解变量 key 为表达式变量 value 为变量值
            context.setVariable(dataColumn.key(), dataColumn.value());
            // 解析sql模板并填充
            String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class);
            sqlString.append(joinStr).append(sql);
            isSuccess = true;
        }
        // 未处理成功则填充兜底方案
        if (!isSuccess) {
            sqlString.append(joinStr).append(type.getElseSql());
        }
    }
    if (StringUtils.isNotBlank(sqlString.toString())) {
        return sqlString.substring(joinStr.length());
    }
    return "";
}
Also used : RoleDTO(com.ruoyi.common.core.domain.dto.RoleDTO) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DataScopeType(com.ruoyi.common.enums.DataScopeType) ServiceException(com.ruoyi.common.exception.ServiceException) DataColumn(com.ruoyi.common.annotation.DataColumn) LoginUser(com.ruoyi.common.core.domain.model.LoginUser)

Example 48 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class RateLimiterAspect method doBefore.

@Before("@annotation(rateLimiter)")
public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
    int time = rateLimiter.time();
    int count = rateLimiter.count();
    String combineKey = getCombineKey(rateLimiter, point);
    try {
        RateType rateType = RateType.OVERALL;
        if (rateLimiter.limitType() == LimitType.CLUSTER) {
            rateType = RateType.PER_CLIENT;
        }
        long number = RedisUtils.rateLimiter(combineKey, rateType, count, time);
        if (number == -1) {
            throw new ServiceException("访问过于频繁,请稍候再试");
        }
        log.info("限制令牌 => {}, 剩余令牌 => {}, 缓存key => '{}'", count, number, combineKey);
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("服务器限流异常,请稍候再试");
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) RateType(org.redisson.api.RateType) JoinPoint(org.aspectj.lang.JoinPoint) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 49 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class RepeatSubmitAspect method doBefore.

@Before("@annotation(repeatSubmit)")
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
    // 如果注解不为0 则使用注解数值
    long interval = repeatSubmitProperties.getInterval();
    if (repeatSubmit.interval() > 0) {
        interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval());
    }
    if (interval < 1000) {
        throw new ServiceException("重复提交间隔时间不能小于'1'秒");
    }
    HttpServletRequest request = ServletUtils.getRequest();
    String nowParams = argsArrayToString(point.getArgs());
    // 请求地址(作为存放cache的key值)
    String url = request.getRequestURI();
    // 唯一值(没有消息头则使用请求地址)
    String submitKey = StringUtils.trimToEmpty(request.getHeader(SaManager.getConfig().getTokenName()));
    submitKey = SecureUtil.md5(submitKey + ":" + nowParams);
    // 唯一标识(指定key + url + 消息头)
    String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey;
    String key = RedisUtils.getCacheObject(cacheRepeatKey);
    if (key == null) {
        RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS);
    } else {
        throw new ServiceException(repeatSubmit.message());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 50 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class SysOssServiceImpl method upload.

@Override
public SysOss upload(MultipartFile file) {
    String originalfileName = file.getOriginalFilename();
    String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
    IOssStrategy storage = OssFactory.instance();
    UploadResult uploadResult;
    try {
        uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
    } catch (IOException e) {
        throw new ServiceException(e.getMessage());
    }
    // 保存文件信息
    SysOss oss = new SysOss();
    oss.setUrl(uploadResult.getUrl());
    oss.setFileSuffix(suffix);
    oss.setFileName(uploadResult.getFilename());
    oss.setOriginalName(originalfileName);
    oss.setService(storage.getServiceType().getValue());
    baseMapper.insert(oss);
    return oss;
}
Also used : SysOss(com.ruoyi.system.domain.SysOss) ServiceException(com.ruoyi.common.exception.ServiceException) IOssStrategy(com.ruoyi.oss.service.IOssStrategy) UploadResult(com.ruoyi.oss.entity.UploadResult) IOException(java.io.IOException)

Aggregations

ServiceException (com.ruoyi.common.exception.ServiceException)109 IOException (java.io.IOException)21 Transactional (org.springframework.transaction.annotation.Transactional)21 GenTable (com.ruoyi.generator.domain.GenTable)12 StringWriter (java.io.StringWriter)12 Template (org.apache.velocity.Template)12 VelocityContext (org.apache.velocity.VelocityContext)12 SysRole (com.ruoyi.common.core.domain.entity.SysRole)10 File (java.io.File)10 SysUser (com.ruoyi.common.core.domain.entity.SysUser)9 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)8 Before (org.aspectj.lang.annotation.Before)8 JSONObject (com.alibaba.fastjson.JSONObject)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)6 SysDept (com.ruoyi.common.core.domain.entity.SysDept)6 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 SysOss (com.ruoyi.system.domain.SysOss)5