From 5b07634cfd5b9bdf576fa95701a8fcdaea1e4046 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sat, 2 Mar 2019 20:41:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E5=92=8C=E8=A7=92=E8=89=B2=E6=8B=A5=E6=9C=89=E7=9A=84=E6=8E=88?= =?UTF-8?q?=E6=9D=83~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AdminController.java | 17 +++++--- .../controller/RoleController.java | 14 ++++-- .../application/convert/AdminConvert.java | 7 +++ .../admin/application/vo/AdminRoleVO.java | 43 +++++++++++++++++++ .../vo/RoleResourceTreeNodeVO.java | 10 +++++ .../iocoder/mall/admin/api/RoleService.java | 13 +++++- .../cn/iocoder/mall/admin/dao/RoleMapper.java | 2 + .../mall/admin/service/RoleServiceImpl.java | 12 +++++- .../src/main/resources/mapper/RoleMapper.xml | 7 +++ .../resources/mapper/RoleResourceMapper.xml | 12 +++--- .../common/framework/util/CollectionUtil.java | 9 +++- 11 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminRoleVO.java diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java index 75c1144af..abd6fe5c9 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java @@ -6,6 +6,7 @@ import cn.iocoder.mall.admin.api.ResourceService; import cn.iocoder.mall.admin.api.RoleService; import cn.iocoder.mall.admin.api.bo.AdminPageBO; import cn.iocoder.mall.admin.api.bo.ResourceBO; +import cn.iocoder.mall.admin.api.bo.RoleBO; import cn.iocoder.mall.admin.api.constant.ResourceConstants; import cn.iocoder.mall.admin.api.dto.AdminAddDTO; import cn.iocoder.mall.admin.api.dto.AdminPageDTO; @@ -14,8 +15,8 @@ import cn.iocoder.mall.admin.application.convert.AdminConvert; import cn.iocoder.mall.admin.application.convert.ResourceConvert; import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO; import cn.iocoder.mall.admin.application.vo.AdminPageVO; +import cn.iocoder.mall.admin.application.vo.AdminRoleVO; import cn.iocoder.mall.admin.application.vo.AdminVO; -import cn.iocoder.mall.admin.application.vo.RoleVO; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import com.alibaba.dubbo.config.annotation.Reference; import io.swagger.annotations.Api; @@ -143,10 +144,16 @@ public class AdminController { @GetMapping("/role_list") @ApiOperation(value = "指定管理员拥有的角色列表") @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1") - public CommonResult> roleList(@RequestParam("id") Integer id) { -// return RoleConvert.INSTANCE.convert() - // TODO 需要讨论下 api 提供的方式 - return null; + public CommonResult> roleList(@RequestParam("id") Integer id) { + // 获得管理员拥有的角色集合 + Set adminRoleIdSet = roleService.getRoleList(id).getData(); + // 获得所有角色数组 + List allRoleList = roleService.getRoleList().getData(); + // 转换出返回结果 + List result = AdminConvert.INSTANCE.convert(allRoleList); + // 设置每个角色是否赋予给改管理员 + result.forEach(adminRoleVO -> adminRoleVO.setAssigned(adminRoleIdSet.contains(adminRoleVO.getId()))); + return CommonResult.success(result); } @PostMapping("/assign_role") diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java index e09a557b5..5298fc2a6 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java @@ -1,5 +1,6 @@ package cn.iocoder.mall.admin.application.controller; +import cn.iocoder.common.framework.util.CollectionUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.admin.api.ResourceService; import cn.iocoder.mall.admin.api.RoleService; @@ -82,11 +83,13 @@ public class RoleController { @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1") public CommonResult> resourceTree(@RequestParam("id") Integer id) { // 芋艿:此处,严格来说可以在校验下角色是否存在。不过呢,校验了也没啥意义,因为一般不存在这个情况,且不会有业务上的影响。并且,反倒多了一次 rpc 调用。 - Set roleIds = new HashSet<>(); - roleIds.add(id); - List resources = resourceService.getResourcesByTypeAndRoleIds(null, roleIds); + // 第一步,获得角色拥有的资源数组 + Set roleResources = resourceService.getResourcesByTypeAndRoleIds(null, CollectionUtil.asSet(id)) + .stream().map(ResourceBO::getId).collect(Collectors.toSet()); + // 第二步,获得资源树 + List allResources = resourceService.getResourcesByType(null); // 创建 AdminMenuTreeNodeVO Map - Map treeNodeMap = resources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert4)); + Map treeNodeMap = allResources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert4)); // 处理父子关系 treeNodeMap.values().stream() .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) @@ -104,6 +107,9 @@ public class RoleController { .filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT)) .sorted(Comparator.comparing(RoleResourceTreeNodeVO::getSort)) .collect(Collectors.toList()); + // 第三步,设置角色是否有该角色 + treeNodeMap.values().forEach(nodeVO -> nodeVO.setAssigned(roleResources.contains(nodeVO.getId()))); + // 返回结果 return CommonResult.success(rootNodes); } diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java index 3c0a4ce51..4a06d9022 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java @@ -3,14 +3,18 @@ package cn.iocoder.mall.admin.application.convert; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.admin.api.bo.AdminBO; import cn.iocoder.mall.admin.api.bo.AdminPageBO; +import cn.iocoder.mall.admin.api.bo.RoleBO; import cn.iocoder.mall.admin.application.vo.AdminInfoVO; import cn.iocoder.mall.admin.application.vo.AdminPageVO; +import cn.iocoder.mall.admin.application.vo.AdminRoleVO; import cn.iocoder.mall.admin.application.vo.AdminVO; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContext; import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; +import java.util.List; + @Mapper public interface AdminConvert { @@ -28,4 +32,7 @@ public interface AdminConvert { @Mappings({}) CommonResult convert(CommonResult result); + @Mappings({}) + List convert(List roleList); + } \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminRoleVO.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminRoleVO.java new file mode 100644 index 000000000..a29ce2dcd --- /dev/null +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminRoleVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.mall.admin.application.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel("管理员拥有的角色 VO") +public class AdminRoleVO { + + @ApiModelProperty(value = "角色编号", required = true, example = "1") + private Integer id; + @ApiModelProperty(value = "角色名字", required = true, example = "系统管理员") + private String name; + @ApiModelProperty(value = "是否授权", required = true, example = "true") + private Boolean assigned; + + public Integer getId() { + return id; + } + + public AdminRoleVO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public AdminRoleVO setName(String name) { + this.name = name; + return this; + } + + public Boolean getAssigned() { + return assigned; + } + + public AdminRoleVO setAssigned(Boolean assigned) { + this.assigned = assigned; + return this; + } + +} \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/RoleResourceTreeNodeVO.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/RoleResourceTreeNodeVO.java index 1f5d321f2..afdd46a53 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/RoleResourceTreeNodeVO.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/RoleResourceTreeNodeVO.java @@ -22,6 +22,8 @@ public class RoleResourceTreeNodeVO { private String displayName; @ApiModelProperty(value = "子节点数组") private List children; + @ApiModelProperty(value = "是否授权", required = true, example = "true") + private Boolean assigned; public Integer getId() { return id; @@ -77,4 +79,12 @@ public class RoleResourceTreeNodeVO { return this; } + public Boolean getAssigned() { + return assigned; + } + + public RoleResourceTreeNodeVO setAssigned(Boolean assigned) { + this.assigned = assigned; + return this; + } } \ No newline at end of file diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java index 7b65cbd6f..bb8c92f06 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java @@ -14,7 +14,18 @@ public interface RoleService { CommonResult getRolePage(RolePageDTO rolePageDTO); - CommonResult> getRoleList(Integer adminId); + /** + * 获得指定管理员拥有的角色编号数组 + * + * @param adminId 指定管理员 + * @return 角色编号数组 + */ + CommonResult> getRoleList(Integer adminId); + + /** + * @return 返回角色列表 + */ + CommonResult> getRoleList(); CommonResult addRole(Integer adminId, RoleAddDTO roleAddDTO); diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java index c57530103..fce5428d8 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java @@ -24,4 +24,6 @@ public interface RoleMapper { List selectListByIds(@Param("ids") Set ids); + List selectList(); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java index 918c112cd..2889d15df 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java @@ -15,6 +15,7 @@ import cn.iocoder.mall.admin.convert.RoleConvert; import cn.iocoder.mall.admin.dao.AdminRoleMapper; import cn.iocoder.mall.admin.dao.RoleMapper; import cn.iocoder.mall.admin.dao.RoleResourceMapper; +import cn.iocoder.mall.admin.dataobject.AdminRoleDO; import cn.iocoder.mall.admin.dataobject.ResourceDO; import cn.iocoder.mall.admin.dataobject.RoleDO; import cn.iocoder.mall.admin.dataobject.RoleResourceDO; @@ -63,8 +64,15 @@ public class RoleServiceImpl implements RoleService { } @Override - public CommonResult> getRoleList(Integer adminId) { - return null; + public CommonResult> getRoleList(Integer adminId) { + List adminRoleDOs = adminRoleMapper.selectByAdminId(adminId); + return CommonResult.success(adminRoleDOs.stream().map(AdminRoleDO::getRoleId).collect(Collectors.toSet())); + } + + @Override + public CommonResult> getRoleList() { + List roleList = roleMapper.selectList(); + return CommonResult.success(RoleConvert.INSTANCE.convert(roleList)); } @Override diff --git a/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml index f053c1a36..329c960bc 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml @@ -71,4 +71,11 @@ AND deleted = 0 + + \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml index 467b64ee9..3f480aab6 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml @@ -11,12 +11,12 @@