feat(auth): AuthService 新增 getAuthPayload 权限载荷构建

This commit is contained in:
augushong
2026-10-04 00:47:11 +00:00
parent 1a27454c38
commit d3a44a2e1c
2 changed files with 171 additions and 0 deletions

View File

@@ -181,6 +181,45 @@ class AuthServiceBase
return $allowedNodes;
}
/**
* 构建当前管理员的权限载荷.
*
* 形状契约(全局按钮显隐等前端消费):
* - 超管:['all' => true, 'nodes' => []]
* - 普通管理员:['all' => false, 'nodes' => [控制器 => [动作, ...]]]
*
* 允许集合完全复用 getAdminAllowedNodes(内部走 checkNodeResolved 判定链),
* 本方法不复制判定逻辑;底层 nodeList/adminNode 已带缓存,不再叠加独立缓存。
* 无 '/' 或动作段为空的畸形节点(如控制器级节点)跳过。
* @return array
*/
public function getAuthPayload(): array
{
if ($this->isSuperAdmin()) {
return ['all' => true, 'nodes' => []];
}
// 候选仅保留动作节点(含 '/'):控制器级节点无 auth 键,进判定链会触发
// Undefined array key,且它们本就因无 '/' 不会进入载荷,提前过滤语义等价。
$candidates = array_filter(
array_keys($this->getNodeList()),
fn ($node) => strpos((string) $node, '/') !== false
);
$allowed = $this->getAdminAllowedNodes($candidates);
$nodes = [];
foreach ($allowed as $node) {
$parts = explode('/', $node, 2);
if (count($parts) !== 2 || $parts[1] === '') {
continue;
}
$nodes[$parts[0]][] = $parts[1];
}
return ['all' => false, 'nodes' => $nodes];
}
/**
* 获取当前节点.
* @return string