parent
18180b3a01
commit
983c01d709
@ -1,10 +1,19 @@
|
||||
import { stringify } from '@/utils/request.qs';
|
||||
import request from '@/utils/request';
|
||||
|
||||
// ========== OAuth2 模块 ==========
|
||||
|
||||
export async function usernameAuthenticate(params) {
|
||||
export async function oauth2UsernameAuthenticate(params) {
|
||||
return request(`/system-api/admins/oauth2/username-authenticate?${stringify(params)}`, {
|
||||
method: 'POST',
|
||||
body: {},
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Authorization 模块 ==========
|
||||
|
||||
export async function authorizationMenuResourceTree() {
|
||||
return request('/system-api/admins/authorization/menu-resource-tree', {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
package cn.iocoder.mall.system.biz.enums.authorization;
|
||||
|
||||
public enum ResourceIdEnum {
|
||||
|
||||
ROOT(0);
|
||||
|
||||
private final Integer id;
|
||||
|
||||
ResourceIdEnum(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package cn.iocoder.mall.system.biz.enums.authorization;
|
||||
|
||||
public enum RoleTypeEnum {
|
||||
|
||||
/**
|
||||
* 内置角色
|
||||
*/
|
||||
SYSTEM(1),
|
||||
/**
|
||||
* 自定义角色
|
||||
*/
|
||||
CUSTOM(2);
|
||||
|
||||
private final Integer type;
|
||||
|
||||
RoleTypeEnum(Integer type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Integer getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package cn.iocoder.mall.system.biz.dto.authorization;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 授权模块 - 获得账号所拥有的资源 DTO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AuthorizationGetResourcesByAccountIdDTO {
|
||||
|
||||
@NotNull(message = "账号编号不能为空")
|
||||
private Integer accountId;
|
||||
/**
|
||||
* 资源类型
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package cn.iocoder.mall.system.biz.dto.authorization;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 资源模块 - 获得资源列表 DTO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ResourceGetListDTO {
|
||||
|
||||
/**
|
||||
* 资源编号数组
|
||||
*/
|
||||
private Collection<Integer> ids;
|
||||
|
||||
/**
|
||||
* 资源类型
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
}
|
||||
@ -1,12 +1,15 @@
|
||||
package cn.iocoder.mall.system.biz.service.authorization;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
|
||||
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetListDTO;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ResourceService {
|
||||
|
||||
List<ResourceBO> getListByPermissions(Collection<String> permissions);
|
||||
List<ResourceBO> getResourcesByPermissions(Collection<String> permissions);
|
||||
|
||||
List<ResourceBO> getResources(ResourceGetListDTO getListDTO);
|
||||
|
||||
}
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.admin.dao.ResourceMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, type, sort, display_name, icon, permissions,
|
||||
create_time, pid, handler
|
||||
</sql>
|
||||
|
||||
<select id="selectListByTypeAndRoleIds" resultType="ResourceDO">
|
||||
SELECT
|
||||
r.id, r.type, r.sort, r.display_name,
|
||||
r.create_time, r.pid, r.handler
|
||||
FROM resource r, role_resource rr
|
||||
WHERE r.deleted = 0
|
||||
AND rr.deleted = 0
|
||||
<if test="type != null">
|
||||
AND r.type = #{type}
|
||||
</if>
|
||||
AND rr.role_id IN
|
||||
<foreach item="roleId" collection="roleIds" separator="," open="(" close=")" index="">
|
||||
#{roleId}
|
||||
</foreach>
|
||||
AND r.id = rr.resource_id
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,66 @@
|
||||
package cn.iocoder.mall.system.rest.controller.authorization;
|
||||
|
||||
import cn.iocoder.common.framework.constant.MallConstants;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.security.core.context.AdminSecurityContextHolder;
|
||||
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
|
||||
import cn.iocoder.mall.system.biz.dto.authorization.AuthorizationGetResourcesByAccountIdDTO;
|
||||
import cn.iocoder.mall.system.biz.enums.authorization.ResourceIdEnum;
|
||||
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
|
||||
import cn.iocoder.mall.system.biz.service.authorization.AuthorizationService;
|
||||
import cn.iocoder.mall.system.rest.convert.authorization.AdminsAuthorizationConvert;
|
||||
import cn.iocoder.mall.system.rest.response.authorization.AdminsAuthorizationMenuTreeResponse;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/authorization")
|
||||
@Api(tags = "管理员 - 授权 API")
|
||||
@Slf4j
|
||||
public class AdminsAuthorizationController {
|
||||
|
||||
@Autowired
|
||||
private AuthorizationService authorizationService;
|
||||
|
||||
@GetMapping("/menu-resource-tree")
|
||||
@ApiOperation(value = "获得当前账号的菜单资源树", notes = "以树结构返回")
|
||||
public CommonResult<List<AdminsAuthorizationMenuTreeResponse>> menuResourceTree() {
|
||||
List<ResourceBO> resources = authorizationService.getResourcesByAccountId(new AuthorizationGetResourcesByAccountIdDTO()
|
||||
.setAccountId(AdminSecurityContextHolder.getAccountId()).setType(ResourceTypeEnum.MENU.getValue()));
|
||||
// 创建 AdminMenuTreeNodeVO Map
|
||||
// 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。
|
||||
Map<Integer, AdminsAuthorizationMenuTreeResponse> treeNodeMap = new LinkedHashMap<>();
|
||||
resources.stream().sorted(Comparator.comparing(ResourceBO::getSort))
|
||||
.forEach(resourceBO -> treeNodeMap.put(resourceBO.getId(), AdminsAuthorizationConvert.INSTANCE.convert(resourceBO)));
|
||||
// 处理父子关系
|
||||
treeNodeMap.values().stream()
|
||||
.filter(node -> !node.getPid().equals(ResourceIdEnum.ROOT.getId()))
|
||||
.forEach((childNode) -> {
|
||||
// 获得父节点
|
||||
AdminsAuthorizationMenuTreeResponse parentNode = treeNodeMap.get(childNode.getPid());
|
||||
if (parentNode == null) {
|
||||
log.error("[menuResourceTree][resource({}) 找不到父资源({})]", childNode.getId(), childNode.getPid());
|
||||
return;
|
||||
}
|
||||
if (parentNode.getChildren() == null) { // 初始化 children 数组
|
||||
parentNode.setChildren(new ArrayList<>());
|
||||
}
|
||||
// 将自己添加到父节点中
|
||||
parentNode.getChildren().add(childNode);
|
||||
});
|
||||
// 获得到所有的根节点
|
||||
List<AdminsAuthorizationMenuTreeResponse> rootNodes = treeNodeMap.values().stream()
|
||||
.filter(node -> node.getPid().equals(ResourceIdEnum.ROOT.getId()))
|
||||
.collect(Collectors.toList());
|
||||
return CommonResult.success(rootNodes);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package cn.iocoder.mall.system.rest.convert.authorization;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
|
||||
import cn.iocoder.mall.system.rest.response.authorization.AdminsAuthorizationMenuTreeResponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface AdminsAuthorizationConvert {
|
||||
|
||||
AdminsAuthorizationConvert INSTANCE = Mappers.getMapper(AdminsAuthorizationConvert.class);
|
||||
|
||||
AdminsAuthorizationMenuTreeResponse convert(ResourceBO bean);
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue