Search in sources :

Example 1 with RoleDTO

use of com.ruoyi.system.api.model.RoleDTO in project RuoYi-Cloud-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 loginUser = DataPermissionHelper.getVariable("user");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(beanResolver);
    DataPermissionHelper.getContext().forEach(context::setVariable);
    for (RoleDTO role : loginUser.getRoles()) {
        loginUser.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.system.api.model.RoleDTO) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DataScopeType(com.ruoyi.common.mybatis.enums.DataScopeType) ServiceException(com.ruoyi.common.core.exception.ServiceException) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) LoginUser(com.ruoyi.system.api.model.LoginUser)

Example 2 with RoleDTO

use of com.ruoyi.system.api.model.RoleDTO in project RuoYi-Cloud-Plus by JavaLionLi.

the class RemoteUserServiceImpl method buildLoginUser.

/**
 * 构建登录用户
 */
private LoginUser buildLoginUser(SysUser user) {
    LoginUser loginUser = new LoginUser();
    loginUser.setUserId(user.getUserId());
    loginUser.setDeptId(user.getDeptId());
    loginUser.setUsername(user.getUserName());
    loginUser.setPassword(user.getPassword());
    loginUser.setUserType(user.getUserType());
    loginUser.setMenuPermission(permissionService.getMenuPermission(user.getUserId()));
    loginUser.setRolePermission(permissionService.getRolePermission(user.getUserId()));
    loginUser.setDeptName(user.getDept().getDeptName());
    List<RoleDTO> roles = BeanUtil.copyToList(user.getRoles(), RoleDTO.class);
    loginUser.setRoles(roles);
    return loginUser;
}
Also used : RoleDTO(com.ruoyi.system.api.model.RoleDTO) XcxLoginUser(com.ruoyi.system.api.model.XcxLoginUser) LoginUser(com.ruoyi.system.api.model.LoginUser)

Aggregations

LoginUser (com.ruoyi.system.api.model.LoginUser)2 RoleDTO (com.ruoyi.system.api.model.RoleDTO)2 ServiceException (com.ruoyi.common.core.exception.ServiceException)1 DataColumn (com.ruoyi.common.mybatis.annotation.DataColumn)1 DataScopeType (com.ruoyi.common.mybatis.enums.DataScopeType)1 XcxLoginUser (com.ruoyi.system.api.model.XcxLoginUser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1