修改管理员和角色拥有的授权~

This commit is contained in:
YunaiV 2019-03-02 20:41:53 +08:00
parent b3b3b8154b
commit 5b07634cfd
11 changed files with 127 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import cn.iocoder.mall.admin.api.ResourceService;
import cn.iocoder.mall.admin.api.RoleService; import cn.iocoder.mall.admin.api.RoleService;
import cn.iocoder.mall.admin.api.bo.AdminPageBO; import cn.iocoder.mall.admin.api.bo.AdminPageBO;
import cn.iocoder.mall.admin.api.bo.ResourceBO; 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.constant.ResourceConstants;
import cn.iocoder.mall.admin.api.dto.AdminAddDTO; import cn.iocoder.mall.admin.api.dto.AdminAddDTO;
import cn.iocoder.mall.admin.api.dto.AdminPageDTO; 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.convert.ResourceConvert;
import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO; import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO;
import cn.iocoder.mall.admin.application.vo.AdminPageVO; 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.AdminVO;
import cn.iocoder.mall.admin.application.vo.RoleVO;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -143,10 +144,16 @@ public class AdminController {
@GetMapping("/role_list") @GetMapping("/role_list")
@ApiOperation(value = "指定管理员拥有的角色列表") @ApiOperation(value = "指定管理员拥有的角色列表")
@ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1") @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1")
public CommonResult<List<RoleVO>> roleList(@RequestParam("id") Integer id) { public CommonResult<List<AdminRoleVO>> roleList(@RequestParam("id") Integer id) {
// return RoleConvert.INSTANCE.convert() // 获得管理员拥有的角色集合
// TODO 需要讨论下 api 提供的方式 Set<Integer> adminRoleIdSet = roleService.getRoleList(id).getData();
return null; // 获得所有角色数组
List<RoleBO> allRoleList = roleService.getRoleList().getData();
// 转换出返回结果
List<AdminRoleVO> result = AdminConvert.INSTANCE.convert(allRoleList);
// 设置每个角色是否赋予给改管理员
result.forEach(adminRoleVO -> adminRoleVO.setAssigned(adminRoleIdSet.contains(adminRoleVO.getId())));
return CommonResult.success(result);
} }
@PostMapping("/assign_role") @PostMapping("/assign_role")

View File

@ -1,5 +1,6 @@
package cn.iocoder.mall.admin.application.controller; package cn.iocoder.mall.admin.application.controller;
import cn.iocoder.common.framework.util.CollectionUtil;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.ResourceService; import cn.iocoder.mall.admin.api.ResourceService;
import cn.iocoder.mall.admin.api.RoleService; import cn.iocoder.mall.admin.api.RoleService;
@ -82,11 +83,13 @@ public class RoleController {
@ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1") @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1")
public CommonResult<List<RoleResourceTreeNodeVO>> resourceTree(@RequestParam("id") Integer id) { public CommonResult<List<RoleResourceTreeNodeVO>> resourceTree(@RequestParam("id") Integer id) {
// 芋艿此处严格来说可以在校验下角色是否存在不过呢校验了也没啥意义因为一般不存在这个情况且不会有业务上的影响并且反倒多了一次 rpc 调用 // 芋艿此处严格来说可以在校验下角色是否存在不过呢校验了也没啥意义因为一般不存在这个情况且不会有业务上的影响并且反倒多了一次 rpc 调用
Set<Integer> roleIds = new HashSet<>(); // 第一步获得角色拥有的资源数组
roleIds.add(id); Set<Integer> roleResources = resourceService.getResourcesByTypeAndRoleIds(null, CollectionUtil.asSet(id))
List<ResourceBO> resources = resourceService.getResourcesByTypeAndRoleIds(null, roleIds); .stream().map(ResourceBO::getId).collect(Collectors.toSet());
// 第二步获得资源树
List<ResourceBO> allResources = resourceService.getResourcesByType(null);
// 创建 AdminMenuTreeNodeVO Map // 创建 AdminMenuTreeNodeVO Map
Map<Integer, RoleResourceTreeNodeVO> treeNodeMap = resources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert4)); Map<Integer, RoleResourceTreeNodeVO> treeNodeMap = allResources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert4));
// 处理父子关系 // 处理父子关系
treeNodeMap.values().stream() treeNodeMap.values().stream()
.filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT))
@ -104,6 +107,9 @@ public class RoleController {
.filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT)) .filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT))
.sorted(Comparator.comparing(RoleResourceTreeNodeVO::getSort)) .sorted(Comparator.comparing(RoleResourceTreeNodeVO::getSort))
.collect(Collectors.toList()); .collect(Collectors.toList());
// 第三步设置角色是否有该角色
treeNodeMap.values().forEach(nodeVO -> nodeVO.setAssigned(roleResources.contains(nodeVO.getId())));
// 返回结果
return CommonResult.success(rootNodes); return CommonResult.success(rootNodes);
} }

View File

@ -3,14 +3,18 @@ package cn.iocoder.mall.admin.application.convert;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.bo.AdminBO; import cn.iocoder.mall.admin.api.bo.AdminBO;
import cn.iocoder.mall.admin.api.bo.AdminPageBO; 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.AdminInfoVO;
import cn.iocoder.mall.admin.application.vo.AdminPageVO; 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.AdminVO;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContext; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContext;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper @Mapper
public interface AdminConvert { public interface AdminConvert {
@ -28,4 +32,7 @@ public interface AdminConvert {
@Mappings({}) @Mappings({})
CommonResult<AdminPageVO> convert(CommonResult<AdminPageBO> result); CommonResult<AdminPageVO> convert(CommonResult<AdminPageBO> result);
@Mappings({})
List<AdminRoleVO> convert(List<RoleBO> roleList);
} }

View File

@ -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;
}
}

View File

@ -22,6 +22,8 @@ public class RoleResourceTreeNodeVO {
private String displayName; private String displayName;
@ApiModelProperty(value = "子节点数组") @ApiModelProperty(value = "子节点数组")
private List<RoleResourceTreeNodeVO> children; private List<RoleResourceTreeNodeVO> children;
@ApiModelProperty(value = "是否授权", required = true, example = "true")
private Boolean assigned;
public Integer getId() { public Integer getId() {
return id; return id;
@ -77,4 +79,12 @@ public class RoleResourceTreeNodeVO {
return this; return this;
} }
public Boolean getAssigned() {
return assigned;
}
public RoleResourceTreeNodeVO setAssigned(Boolean assigned) {
this.assigned = assigned;
return this;
}
} }

View File

@ -14,7 +14,18 @@ public interface RoleService {
CommonResult<RolePageBO> getRolePage(RolePageDTO rolePageDTO); CommonResult<RolePageBO> getRolePage(RolePageDTO rolePageDTO);
CommonResult<List<RoleBO>> getRoleList(Integer adminId); /**
* 获得指定管理员拥有的角色编号数组
*
* @param adminId 指定管理员
* @return 角色编号数组
*/
CommonResult<Set<Integer>> getRoleList(Integer adminId);
/**
* @return 返回角色列表
*/
CommonResult<List<RoleBO>> getRoleList();
CommonResult<RoleBO> addRole(Integer adminId, RoleAddDTO roleAddDTO); CommonResult<RoleBO> addRole(Integer adminId, RoleAddDTO roleAddDTO);

View File

@ -24,4 +24,6 @@ public interface RoleMapper {
List<RoleDO> selectListByIds(@Param("ids") Set<Integer> ids); List<RoleDO> selectListByIds(@Param("ids") Set<Integer> ids);
List<RoleDO> selectList();
} }

View File

@ -15,6 +15,7 @@ import cn.iocoder.mall.admin.convert.RoleConvert;
import cn.iocoder.mall.admin.dao.AdminRoleMapper; import cn.iocoder.mall.admin.dao.AdminRoleMapper;
import cn.iocoder.mall.admin.dao.RoleMapper; import cn.iocoder.mall.admin.dao.RoleMapper;
import cn.iocoder.mall.admin.dao.RoleResourceMapper; 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.ResourceDO;
import cn.iocoder.mall.admin.dataobject.RoleDO; import cn.iocoder.mall.admin.dataobject.RoleDO;
import cn.iocoder.mall.admin.dataobject.RoleResourceDO; import cn.iocoder.mall.admin.dataobject.RoleResourceDO;
@ -63,8 +64,15 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
public CommonResult<List<RoleBO>> getRoleList(Integer adminId) { public CommonResult<Set<Integer>> getRoleList(Integer adminId) {
return null; List<AdminRoleDO> adminRoleDOs = adminRoleMapper.selectByAdminId(adminId);
return CommonResult.success(adminRoleDOs.stream().map(AdminRoleDO::getRoleId).collect(Collectors.toSet()));
}
@Override
public CommonResult<List<RoleBO>> getRoleList() {
List<RoleDO> roleList = roleMapper.selectList();
return CommonResult.success(RoleConvert.INSTANCE.convert(roleList));
} }
@Override @Override

View File

@ -71,4 +71,11 @@
AND deleted = 0 AND deleted = 0
</select> </select>
<select id="selectList" resultType="RoleDO">
SELECT
<include refid="FIELDS"/>
FROM role
WHERE deleted = 0
</select>
</mapper> </mapper>

View File

@ -11,12 +11,12 @@
<!--</insert>--> <!--</insert>-->
<select id="selectByResourceHandler" parameterType="String" resultType="RoleResourceDO"> <select id="selectByResourceHandler" parameterType="String" resultType="RoleResourceDO">
SELECT SELECT
rr.id, rr.role_id, rr.resource_id rr.id, rr.role_id, rr.resource_id
FROM resource r, role_resource rr FROM resource r, role_resource rr
WHERE r.handler = #{resourceHandler} WHERE r.handler = #{resourceHandler}
AND r.id = rr.resource_id AND r.id = rr.resource_id
AND rr.deleted = 0; AND rr.deleted = 0;
</select> </select>
<select id="selectByResourceId" parameterType="Integer" resultType="RoleResourceDO"> <select id="selectByResourceId" parameterType="Integer" resultType="RoleResourceDO">

View File

@ -1,6 +1,9 @@
package cn.iocoder.common.framework.util; package cn.iocoder.common.framework.util;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class CollectionUtil { public class CollectionUtil {
@ -8,4 +11,8 @@ public class CollectionUtil {
return collection == null || collection.isEmpty(); return collection == null || collection.isEmpty();
} }
} public static <T> Set<T> asSet(T... objs) {
return new HashSet<>(Arrays.asList(objs));
}
}