From 51f1b7e283f1af0255b8b828921d793adfe6fbb2 Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sun, 28 Jun 2020 22:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4BeanUtils=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9EIamHelper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../process/ApiPermissionExtractor.java | 2 +- .../iam/service/IamUserRoleService.java | 9 ++ .../IamFrontendPermissionServiceImpl.java | 2 +- .../impl/IamRolePermissionServiceImpl.java | 2 +- .../service/impl/IamUserRoleServiceImpl.java | 13 ++- .../iam/service/impl/IamUserServiceImpl.java | 37 ++------ .../java/com/diboot/iam/util/BeanUtils.java | 86 ------------------- .../java/com/diboot/iam/util/IamHelper.java | 51 +++++++++++ 8 files changed, 83 insertions(+), 119 deletions(-) delete mode 100644 iam-base-starter/src/main/java/com/diboot/iam/util/BeanUtils.java create mode 100644 iam-base-starter/src/main/java/com/diboot/iam/util/IamHelper.java diff --git a/iam-base-starter/src/main/java/com/diboot/iam/annotation/process/ApiPermissionExtractor.java b/iam-base-starter/src/main/java/com/diboot/iam/annotation/process/ApiPermissionExtractor.java index 9f85d63..9badbc4 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/annotation/process/ApiPermissionExtractor.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/annotation/process/ApiPermissionExtractor.java @@ -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; diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/IamUserRoleService.java b/iam-base-starter/src/main/java/com/diboot/iam/service/IamUserRoleService.java index b084844..b8b3218 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/IamUserRoleService.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/IamUserRoleService.java @@ -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 { */ boolean updateUserRoleRelations(String userType, Long userId, List roleIds); + /*** + * 获取用户的所有角色列表(包括扩展的关联角色) + * @param userObject + * @return + */ + List getAllRoleVOList(BaseEntity userObject); + /** * 获取Iam扩展实现 * @return diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java index a5b622f..a89ac27 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java @@ -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; diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamRolePermissionServiceImpl.java b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamRolePermissionServiceImpl.java index 6d97e59..e829394 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamRolePermissionServiceImpl.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamRolePermissionServiceImpl.java @@ -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; diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserRoleServiceImpl.java b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserRoleServiceImpl.java index b787d58..f88d971 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserRoleServiceImpl.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserRoleServiceImpl.java @@ -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 getAllRoleVOList(BaseEntity userObject) { + List roleList = getUserRoleList(userObject.getClass().getSimpleName(), userObject.getId()); + if (V.isEmpty(roleList)){ + return null; + } + return Binder.convertAndBindRelations(roleList, IamRoleVO.class); + } // 扩展接口检查标记 private boolean iamExtensibleImplChecked = false; diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserServiceImpl.java b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserServiceImpl.java index 6a515f0..3e30cbe 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserServiceImpl.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamUserServiceImpl.java @@ -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 nameList = new ArrayList<>(); - List codeList = new ArrayList<>(); - List 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 getAllRoleVOList(IamUser iamUser) { - List 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 diff --git a/iam-base-starter/src/main/java/com/diboot/iam/util/BeanUtils.java b/iam-base-starter/src/main/java/com/diboot/iam/util/BeanUtils.java deleted file mode 100644 index c010a0d..0000000 --- a/iam-base-starter/src/main/java/com/diboot/iam/util/BeanUtils.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2015-2020, www.dibo.ltd (service@dibo.ltd). - *

- * 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 - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * 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 - * @return - */ - public static List collectToList(List objectList, IGetter getterFn){ - if(V.isEmpty(objectList)){ - return Collections.emptyList(); - } - String getterPropName = convertToFieldName(getterFn); - return collectToList(objectList, getterPropName); - } - - /*** - * 从list对象列表中提取指定属性值到新的List - * @param objectList - * @param getterPropName - * @param - * @return - */ - public static List collectToList(List 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; - } -} diff --git a/iam-base-starter/src/main/java/com/diboot/iam/util/IamHelper.java b/iam-base-starter/src/main/java/com/diboot/iam/util/IamHelper.java new file mode 100644 index 0000000..09b3573 --- /dev/null +++ b/iam-base-starter/src/main/java/com/diboot/iam/util/IamHelper.java @@ -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 roleVOList) { + if (V.isEmpty(roleVOList)){ + return null; + } + // 对RoleList做聚合处理,以适配前端 + List nameList = new ArrayList<>(); + List codeList = new ArrayList<>(); + List 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; + } + +}