移除BeanUtils,新增IamHelper

This commit is contained in:
mazhicheng 2020-06-28 22:40:46 +08:00
parent 7f9299e6b0
commit 51f1b7e283
8 changed files with 83 additions and 119 deletions

View File

@ -15,13 +15,13 @@
*/
package com.diboot.iam.annotation.process;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHelper;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.config.Cons;
import com.diboot.iam.util.AnnotationUtils;
import com.diboot.iam.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;

View File

@ -15,9 +15,11 @@
*/
package com.diboot.iam.service;
import com.diboot.core.entity.BaseEntity;
import com.diboot.iam.auth.IamExtensible;
import com.diboot.iam.entity.IamRole;
import com.diboot.iam.entity.IamUserRole;
import com.diboot.iam.vo.IamRoleVO;
import java.util.List;
@ -64,6 +66,13 @@ public interface IamUserRoleService extends BaseIamService<IamUserRole> {
*/
boolean updateUserRoleRelations(String userType, Long userId, List<Long> roleIds);
/***
* 获取用户的所有角色列表包括扩展的关联角色
* @param userObject
* @return
*/
List<IamRoleVO> getAllRoleVOList(BaseEntity userObject);
/**
* 获取Iam扩展实现
* @return

View File

@ -18,6 +18,7 @@ package com.diboot.iam.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.Status;
@ -26,7 +27,6 @@ import com.diboot.iam.dto.IamFrontendPermissionDTO;
import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.mapper.IamFrontendPermissionMapper;
import com.diboot.iam.service.IamFrontendPermissionService;
import com.diboot.iam.util.BeanUtils;
import com.diboot.iam.vo.IamFrontendPermissionListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

View File

@ -16,6 +16,7 @@
package com.diboot.iam.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.V;
import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.entity.IamRolePermission;
@ -23,7 +24,6 @@ import com.diboot.iam.mapper.IamRolePermissionMapper;
import com.diboot.iam.service.IamFrontendPermissionService;
import com.diboot.iam.service.IamRolePermissionService;
import com.diboot.iam.service.IamRoleService;
import com.diboot.iam.util.BeanUtils;
import com.diboot.iam.util.IamSecurityUtils;
import com.diboot.iam.vo.IamFrontendPermissionVO;
import lombok.extern.slf4j.Slf4j;

View File

@ -17,6 +17,9 @@ package com.diboot.iam.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.entity.BaseEntity;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHelper;
import com.diboot.core.util.V;
import com.diboot.iam.auth.IamExtensible;
@ -28,8 +31,8 @@ import com.diboot.iam.mapper.IamUserRoleMapper;
import com.diboot.iam.service.IamAccountService;
import com.diboot.iam.service.IamRoleService;
import com.diboot.iam.service.IamUserRoleService;
import com.diboot.iam.util.BeanUtils;
import com.diboot.iam.util.IamSecurityUtils;
import com.diboot.iam.vo.IamRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -207,6 +210,14 @@ public class IamUserRoleServiceImpl extends BaseIamServiceImpl<IamUserRoleMapper
return success;
}
@Override
public List<IamRoleVO> getAllRoleVOList(BaseEntity userObject) {
List<IamRole> roleList = getUserRoleList(userObject.getClass().getSimpleName(), userObject.getId());
if (V.isEmpty(roleList)){
return null;
}
return Binder.convertAndBindRelations(roleList, IamRoleVO.class);
}
// 扩展接口检查标记
private boolean iamExtensibleImplChecked = false;

View File

@ -16,20 +16,21 @@
package com.diboot.iam.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.Status;
import com.diboot.iam.config.Cons;
import com.diboot.iam.dto.IamUserAccountDTO;
import com.diboot.iam.entity.*;
import com.diboot.iam.entity.IamAccount;
import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.entity.IamUser;
import com.diboot.iam.entity.IamUserRole;
import com.diboot.iam.mapper.IamUserMapper;
import com.diboot.iam.service.IamAccountService;
import com.diboot.iam.service.IamFrontendPermissionService;
import com.diboot.iam.service.IamUserRoleService;
import com.diboot.iam.service.IamUserService;
import com.diboot.iam.util.BeanUtils;
import com.diboot.iam.util.IamHelper;
import com.diboot.iam.util.IamSecurityUtils;
import com.diboot.iam.vo.IamRoleVO;
import lombok.extern.slf4j.Slf4j;
@ -37,7 +38,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
@ -67,34 +67,13 @@ public class IamUserServiceImpl extends BaseIamServiceImpl<IamUserMapper, IamUse
}
// 附加额外的一些权限给与特性的角色
attachExtraPermissions(roleVOList);
// 对RoleList做聚合处理以适配前端
List<String> nameList = new ArrayList<>();
List<String> codeList = new ArrayList<>();
List<IamFrontendPermission> allPermissionList = new ArrayList<>();
roleVOList.forEach(vo -> {
nameList.add(vo.getName());
codeList.add(vo.getCode());
if (V.notEmpty(vo.getPermissionList())){
allPermissionList.addAll(vo.getPermissionList());
}
});
// 对permissionList进行去重
List permissionList = BeanUtils.distinctByKey(allPermissionList, IamFrontendPermission::getId);
IamRoleVO roleVO = new IamRoleVO();
roleVO.setName(S.join(nameList));
roleVO.setCode(S.join(codeList));
roleVO.setPermissionList(permissionList);
return roleVO;
// 组合为前端格式
return IamHelper.buildRoleVo4FrontEnd(roleVOList);
}
@Override
public List<IamRoleVO> getAllRoleVOList(IamUser iamUser) {
List<IamRole> roleList = iamUserRoleService.getUserRoleList(IamUser.class.getSimpleName(), iamUser.getId());
if (V.isEmpty(roleList)){
return null;
}
return Binder.convertAndBindRelations(roleList, IamRoleVO.class);
return iamUserRoleService.getAllRoleVOList(iamUser);
}
@Override

View File

@ -1,86 +0,0 @@
/*
* Copyright (c) 2015-2020, www.dibo.ltd (service@dibo.ltd).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.diboot.iam.util;
import com.diboot.core.util.IGetter;
import com.diboot.core.util.V;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BeanUtils extends com.diboot.core.util.BeanUtils {
private static final Logger log = LoggerFactory.getLogger(BeanUtils.class);
/**
* 从list对象列表中提取指定属性值到新的List
* @param objectList 对象list
* @param getterFn get方法
* @param <T>
* @return
*/
public static <E,T> List collectToList(List<E> objectList, IGetter<T> getterFn){
if(V.isEmpty(objectList)){
return Collections.emptyList();
}
String getterPropName = convertToFieldName(getterFn);
return collectToList(objectList, getterPropName);
}
/***
* 从list对象列表中提取指定属性值到新的List
* @param objectList
* @param getterPropName
* @param <E>
* @return
*/
public static <E> List collectToList(List<E> objectList, String getterPropName){
List fieldValueList = new ArrayList();
try{
for(E object : objectList){
Object fieldValue = getProperty(object, getterPropName);
if(fieldValue != null && !fieldValueList.contains(fieldValue)){
fieldValueList.add(fieldValue);
}
}
}
catch (Exception e){
log.warn("提取属性值异常, getterPropName="+getterPropName, e);
}
return fieldValueList;
}
/***
* 获取对象的属性值
* @param obj
* @param field
* @return
*/
public static Object getProperty(Object obj, String field){
try {
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(obj);
return wrapper.getPropertyValue(field);
} catch (Exception e) {
log.error("获取对象属性值出错返回null", e);
}
return null;
}
}

View File

@ -0,0 +1,51 @@
package com.diboot.iam.util;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.vo.IamRoleVO;
import java.util.ArrayList;
import java.util.List;
/**
* IAM相关辅助类
*
* @author mazc@dibo.ltd
* @version v1.0
* @date 2020/06/28
*/
public class IamHelper {
/**
* 构建role-permission角色权限数据格式(合并role等)用于前端适配
* @param roleVOList
* @return
*/
public static IamRoleVO buildRoleVo4FrontEnd(List<IamRoleVO> roleVOList) {
if (V.isEmpty(roleVOList)){
return null;
}
// 对RoleList做聚合处理以适配前端
List<String> nameList = new ArrayList<>();
List<String> codeList = new ArrayList<>();
List<IamFrontendPermission> allPermissionList = new ArrayList<>();
roleVOList.forEach(vo -> {
nameList.add(vo.getName());
codeList.add(vo.getCode());
if (V.notEmpty(vo.getPermissionList())){
allPermissionList.addAll(vo.getPermissionList());
}
});
// 对permissionList进行去重
List permissionList = BeanUtils.distinctByKey(allPermissionList, IamFrontendPermission::getId);
IamRoleVO roleVO = new IamRoleVO();
roleVO.setName(S.join(nameList));
roleVO.setCode(S.join(codeList));
roleVO.setPermissionList(permissionList);
return roleVO;
}
}