Search in sources :

Example 1 with LabelValue

use of com.diboot.core.vo.LabelValue in project diboot by dibo-software.

the class BaseJwtRealm method doGetAuthenticationInfo.

/**
 * 获取认证信息
 * @param token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    BaseJwtAuthToken jwtToken = (BaseJwtAuthToken) token;
    String authAccount = (String) jwtToken.getPrincipal();
    if (V.isEmpty(authAccount)) {
        throw new AuthenticationException("无效的用户标识");
    } else {
        // 获取认证方式
        AuthService authService = AuthServiceFactory.getAuthService(jwtToken.getAuthType());
        if (authService == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("认证类型: " + jwtToken.getAuthType() + " 的AccountAuthService未实现!");
        }
        IamAccount account = authService.getAccount(jwtToken);
        // 登录失败则抛出相关异常
        if (account == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("用户账号或密码错误!");
        }
        // 获取当前user对象并缓存
        BaseLoginUser loginUser = null;
        BaseService userService = ContextHelper.getBaseServiceByEntity(jwtToken.getUserTypeClass());
        if (userService != null) {
            loginUser = (BaseLoginUser) userService.getEntity(account.getUserId());
        } else {
            throw new AuthenticationException("用户 " + jwtToken.getUserTypeClass().getName() + " 相关的Service未定义!");
        }
        if (loginUser == null) {
            throw new AuthenticationException("用户不存在");
        }
        loginUser.setAuthToken(jwtToken.getAuthtoken());
        IamExtensible iamExtensible = getIamUserRoleService().getIamExtensible();
        if (iamExtensible != null) {
            LabelValue extentionObj = iamExtensible.getUserExtentionObj(jwtToken.getUserTypeClass().getSimpleName(), account.getUserId(), jwtToken.getExtObj());
            if (extentionObj != null) {
                loginUser.setExtentionObj(extentionObj);
            }
        }
        // 清空当前用户缓存
        this.clearCachedAuthorizationInfo(IamSecurityUtils.getSubject().getPrincipals());
        return new SimpleAuthenticationInfo(loginUser, jwtToken.getCredentials(), this.getName());
    }
}
Also used : IamAccount(com.diboot.iam.entity.IamAccount) IamExtensible(com.diboot.iam.auth.IamExtensible) LabelValue(com.diboot.core.vo.LabelValue) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) BaseLoginUser(com.diboot.iam.entity.BaseLoginUser) AuthService(com.diboot.iam.auth.AuthService) BaseService(com.diboot.core.service.BaseService)

Example 2 with LabelValue

use of com.diboot.core.vo.LabelValue in project diboot by dibo-software.

the class BaseJwtRealm method doGetAuthorizationInfo.

/**
 * 获取授权信息
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    BaseLoginUser currentUser = (BaseLoginUser) principals.getPrimaryPrincipal();
    // 根据用户类型与用户id获取roleList
    Long extentionObjId = null;
    LabelValue extentionObj = currentUser.getExtentionObj();
    if (extentionObj != null) {
        extentionObjId = (Long) extentionObj.getValue();
    }
    // 获取角色列表
    List<IamRole> roleList = getIamUserRoleService().getUserRoleList(currentUser.getClass().getSimpleName(), currentUser.getId(), extentionObjId);
    // 如果没有任何角色,返回
    if (V.isEmpty(roleList)) {
        return authorizationInfo;
    }
    // 整理所有角色许可列表
    Set<String> allRoleCodes = new HashSet<>();
    List<Long> roleIds = new ArrayList<>();
    roleList.stream().forEach(role -> {
        // 添加当前角色到角色列表中
        allRoleCodes.add(role.getCode());
        roleIds.add(role.getId());
    });
    // 整理所有权限许可列表,从缓存匹配
    Set<String> allPermissionCodes = new HashSet<>();
    List<String> apiUrlList = getIamRoleResourceService().getApiUrlList(Cons.APPLICATION, roleIds);
    if (V.notEmpty(apiUrlList)) {
        apiUrlList.stream().forEach(set -> {
            for (String uri : set.split(Cons.SEPARATOR_COMMA)) {
                String permissionCode = IamCacheManager.getPermissionCode(uri);
                if (permissionCode != null) {
                    allPermissionCodes.add(permissionCode);
                }
            }
        });
    }
    // 将所有角色和权限许可授权给用户
    authorizationInfo.setRoles(allRoleCodes);
    authorizationInfo.setStringPermissions(allPermissionCodes);
    return authorizationInfo;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) LabelValue(com.diboot.core.vo.LabelValue) BaseLoginUser(com.diboot.iam.entity.BaseLoginUser) IamRole(com.diboot.iam.entity.IamRole) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with LabelValue

use of com.diboot.core.vo.LabelValue in project diboot by dibo-software.

the class BaseServiceImpl method getLabelValueList.

@Override
public List<LabelValue> getLabelValueList(Wrapper queryWrapper) {
    String sqlSelect = queryWrapper.getSqlSelect();
    // 最多支持3个属性:label, value, ext
    if (V.isEmpty(sqlSelect) || S.countMatches(sqlSelect, Cons.SEPARATOR_COMMA) > 2) {
        log.error("调用错误: getLabelValueList必须用select依次指定返回的Label,Value, ext键值字段,如: new QueryWrapper<Dictionary>().lambda().select(Dictionary::getItemName, Dictionary::getItemValue)");
        return Collections.emptyList();
    }
    // 获取mapList
    List<Map<String, Object>> mapList = super.listMaps(queryWrapper);
    if (mapList == null) {
        return Collections.emptyList();
    } else if (mapList.size() > BaseConfig.getBatchSize()) {
        log.warn("单次查询记录数量过大,建议您及时检查优化。返回结果数={}", mapList.size());
    }
    // 转换为LabelValue
    String[] selectArray = sqlSelect.split(Cons.SEPARATOR_COMMA);
    // 是否有ext字段
    boolean hasExt = selectArray.length > 2;
    List<LabelValue> labelValueList = new ArrayList<>(mapList.size());
    for (Map<String, Object> map : mapList) {
        // 如果key和value的的值都为null的时候map也为空,则不处理此项
        if (V.isEmpty(map)) {
            continue;
        }
        String label = selectArray[0], value = selectArray[1], ext;
        // 兼容oracle大写
        if (map.containsKey(label) || map.containsKey(label = label.toUpperCase())) {
            LabelValue labelValue = new LabelValue();
            // 设置label
            labelValue.setLabel(S.valueOf(map.get(label)));
            // 设置value
            if (map.containsKey(value) || map.containsKey(value = value.toUpperCase())) {
                labelValue.setValue(map.get(value));
            }
            // 设置ext
            if (hasExt) {
                ext = selectArray[2];
                if (map.containsKey(ext) || map.containsKey(ext = ext.toUpperCase())) {
                    labelValue.setExt(map.get(ext));
                }
            }
            labelValueList.add(labelValue);
        }
    }
    return labelValueList;
}
Also used : LabelValue(com.diboot.core.vo.LabelValue)

Example 4 with LabelValue

use of com.diboot.core.vo.LabelValue in project diboot by dibo-software.

the class ExcelBindAnnoHandler method convertToNameValueMap.

/**
 * 转换为name-value map
 * @param annotation
 * @param nameList
 * @return
 */
public static Map<String, List> convertToNameValueMap(Annotation annotation, List<String> nameList) {
    // 字典
    if (annotation instanceof ExcelBindDict || annotation instanceof BindDict) {
        String dictType = null;
        if (annotation instanceof ExcelBindDict) {
            dictType = ((ExcelBindDict) annotation).type();
        } else {
            dictType = ((BindDict) annotation).type();
        }
        DictionaryServiceExtProvider bindDictService = ContextHelper.getBean(DictionaryServiceExtProvider.class);
        if (bindDictService == null) {
            throw new InvalidUsageException("DictionaryService未实现,无法使用ExcelBindDict注解!");
        }
        List<LabelValue> list = bindDictService.getLabelValueList(dictType);
        return convertLabelValueListToMap(list);
    } else if (annotation instanceof ExcelBindField) {
        ExcelBindField bindField = (ExcelBindField) annotation;
        return executeBindField(bindField, nameList);
    } else {
        return Collections.emptyMap();
    }
}
Also used : ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) BindDict(com.diboot.core.binding.annotation.BindDict) DictionaryServiceExtProvider(com.diboot.core.service.DictionaryServiceExtProvider) LabelValue(com.diboot.core.vo.LabelValue) ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField)

Example 5 with LabelValue

use of com.diboot.core.vo.LabelValue in project diboot by dibo-software.

the class BaseController method attachMoreRelatedData.

/**
 * 通用的attachMore获取数据
 *
 * @param attachMore  相应的attachMore
 * @param parentValue 父级值
 * @return labelValue集合
 */
protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMore, String parentValue) {
    String entityClassName = S.capFirst(S.toLowerCaseCamel(attachMore.getTarget()));
    Class<?> entityClass = BindingCacheManager.getEntityClassBySimpleName(entityClassName);
    if (V.isEmpty(entityClass)) {
        throw new BusinessException("attachMore: " + attachMore.getTarget() + " 不存在");
    }
    BaseService<?> baseService = ContextHelper.getBaseServiceByEntity(entityClass);
    if (baseService == null) {
        throw new BusinessException("attachMore: " + attachMore.getTarget() + " 的Service不存在 ");
    }
    PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entityClass);
    Function<String, String> field2column = field -> {
        if (V.notEmpty(field)) {
            String column = propInfoCache.getColumnByField(field);
            if (V.notEmpty(column)) {
                return column;
            } else {
                throw new BusinessException("attachMore: " + attachMore.getTarget() + " 无 `" + field + "` 属性");
            }
        }
        return null;
    };
    String label = field2column.apply(S.defaultIfBlank(attachMore.getLabel(), "label"));
    String value = S.defaultString(field2column.apply(attachMore.getValue()), propInfoCache.getIdColumn());
    String ext = field2column.apply(attachMore.getExt());
    // 构建查询条件
    QueryWrapper<?> queryWrapper = Wrappers.query().select(V.isEmpty(ext) ? new String[] { label, value } : new String[] { label, value, ext }).like(V.notEmpty(attachMore.getKeyword()), label, attachMore.getKeyword());
    // 解析排序
    if (V.notEmpty(attachMore.getOrderBy())) {
        String[] orderByFields = S.split(attachMore.getOrderBy(), ",");
        for (String field : orderByFields) {
            V.securityCheck(field);
            String[] fieldAndOrder = field.split(":");
            String columnName = field2column.apply(fieldAndOrder[0]);
            if (fieldAndOrder.length > 1 && Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
                queryWrapper.orderByDesc(columnName);
            } else {
                queryWrapper.orderByAsc(columnName);
            }
        }
    }
    // 父级限制
    String parentColumn = field2column.apply(S.defaultIfBlank(attachMore.getParent(), attachMore.isTree() ? "parentId" : null));
    if (V.notEmpty(parentColumn)) {
        if (V.notEmpty(parentValue)) {
            queryWrapper.eq(parentColumn, parentValue);
        } else {
            queryWrapper.and(e -> e.isNull(parentColumn).or().eq(parentColumn, 0));
        }
    }
    // 构建附加条件
    buildAttachMoreCondition(attachMore, queryWrapper, field2column);
    // 获取数据并做相应填充
    List<LabelValue> labelValueList = baseService.getLabelValueList(queryWrapper);
    if (V.notEmpty(parentColumn) || attachMore.getNext() != null) {
        Boolean leaf = !attachMore.isTree() && attachMore.getNext() == null ? true : null;
        // 第一层tree与最后一层无需返回type
        String type = attachMore.isTree() || Boolean.TRUE.equals(leaf) ? null : attachMore.getTarget();
        labelValueList.forEach(item -> {
            item.setType(type);
            item.setLeaf(leaf);
            // 非异步加载
            if (!attachMore.isLazy()) {
                List<LabelValue> children = attachMoreRelatedData(attachMore, S.valueOf(item.getValue()), type);
                item.setChildren(children.isEmpty() ? null : children);
                item.setDisabled(children.isEmpty() && attachMore.getNext() != null ? true : null);
            }
        });
    }
    return labelValueList;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) java.util(java.util) Logger(org.slf4j.Logger) ValidList(com.diboot.core.entity.ValidList) DictionaryService(com.diboot.core.service.DictionaryService) Cons(com.diboot.core.config.Cons) LoggerFactory(org.slf4j.LoggerFactory) BaseService(com.diboot.core.service.BaseService) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) LabelValue(com.diboot.core.vo.LabelValue) BusinessException(com.diboot.core.exception.BusinessException) Function(java.util.function.Function) Binder(com.diboot.core.binding.Binder) ContextHelper(com.diboot.core.util.ContextHelper) PropInfo(com.diboot.core.binding.parser.PropInfo) BindingCacheManager(com.diboot.core.binding.cache.BindingCacheManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) AttachMoreDTO(com.diboot.core.dto.AttachMoreDTO) S(com.diboot.core.util.S) V(com.diboot.core.util.V) QueryBuilder(com.diboot.core.binding.QueryBuilder) BusinessException(com.diboot.core.exception.BusinessException) LabelValue(com.diboot.core.vo.LabelValue) PropInfo(com.diboot.core.binding.parser.PropInfo)

Aggregations

LabelValue (com.diboot.core.vo.LabelValue)7 BaseService (com.diboot.core.service.BaseService)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 BaseLoginUser (com.diboot.iam.entity.BaseLoginUser)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 Binder (com.diboot.core.binding.Binder)1 QueryBuilder (com.diboot.core.binding.QueryBuilder)1 BindDict (com.diboot.core.binding.annotation.BindDict)1 BindingCacheManager (com.diboot.core.binding.cache.BindingCacheManager)1 PropInfo (com.diboot.core.binding.parser.PropInfo)1 Cons (com.diboot.core.config.Cons)1 AttachMoreDTO (com.diboot.core.dto.AttachMoreDTO)1 ValidList (com.diboot.core.entity.ValidList)1 BusinessException (com.diboot.core.exception.BusinessException)1 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)1 DictionaryService (com.diboot.core.service.DictionaryService)1 DictionaryServiceExtProvider (com.diboot.core.service.DictionaryServiceExtProvider)1 ContextHelper (com.diboot.core.util.ContextHelper)1 S (com.diboot.core.util.S)1