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

pull/1/head
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.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<List<RoleVO>> roleList(@RequestParam("id") Integer id) {
// return RoleConvert.INSTANCE.convert()
// TODO 需要讨论下 api 提供的方式
return null;
public CommonResult<List<AdminRoleVO>> roleList(@RequestParam("id") Integer id) {
// 获得管理员拥有的角色集合
Set<Integer> adminRoleIdSet = roleService.getRoleList(id).getData();
// 获得所有角色数组
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")

View File

@ -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<List<RoleResourceTreeNodeVO>> resourceTree(@RequestParam("id") Integer id) {
// 芋艿:此处,严格来说可以在校验下角色是否存在。不过呢,校验了也没啥意义,因为一般不存在这个情况,且不会有业务上的影响。并且,反倒多了一次 rpc 调用。
Set<Integer> roleIds = new HashSet<>();
roleIds.add(id);
List<ResourceBO> resources = resourceService.getResourcesByTypeAndRoleIds(null, roleIds);
// 第一步,获得角色拥有的资源数组
Set<Integer> roleResources = resourceService.getResourcesByTypeAndRoleIds(null, CollectionUtil.asSet(id))
.stream().map(ResourceBO::getId).collect(Collectors.toSet());
// 第二步,获得资源树
List<ResourceBO> allResources = resourceService.getResourcesByType(null);
// 创建 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()
.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);
}

View File

@ -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<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;
@ApiModelProperty(value = "子节点数组")
private List<RoleResourceTreeNodeVO> 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;
}
}

View File

@ -14,7 +14,18 @@ public interface RoleService {
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);

View File

@ -24,4 +24,6 @@ public interface RoleMapper {
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.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<List<RoleBO>> getRoleList(Integer adminId) {
return null;
public CommonResult<Set<Integer>> getRoleList(Integer adminId) {
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

View File

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

View File

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

View File

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