mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
128 lines
3.8 KiB
PHP
128 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace base\common\command\admin\role;
|
|
|
|
use app\admin\model\SystemAuth;
|
|
use app\admin\model\SystemAuthNode;
|
|
use app\admin\service\NodeService;
|
|
use app\common\console\Command;
|
|
use think\console\Input;
|
|
use think\console\input\Option;
|
|
use think\console\Output;
|
|
|
|
class AdminRolePermissionAssignBase extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
parent::configure();
|
|
|
|
$this->setName('admin:role:permission:assign')
|
|
->addOption('role-id', null, Option::VALUE_REQUIRED, '角色ID')
|
|
->addOption('nodes', null, Option::VALUE_REQUIRED, '权限节点列表(逗号分隔)')
|
|
->setDescription('给角色分配权限');
|
|
}
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
{
|
|
$roleId = $input->getOption('role-id');
|
|
$nodesParam = $input->getOption('nodes');
|
|
|
|
if (empty($roleId)) {
|
|
$output->error('角色ID不能为空');
|
|
return false;
|
|
}
|
|
|
|
if (empty($nodesParam)) {
|
|
$output->error('权限节点不能为空');
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$auth = SystemAuth::find($roleId);
|
|
if (empty($auth)) {
|
|
$output->error('角色ID ' . $roleId . ' 不存在');
|
|
return false;
|
|
}
|
|
|
|
$nodes = $this->parseNodes($nodesParam);
|
|
if (empty($nodes)) {
|
|
$output->error('权限节点列表为空');
|
|
return false;
|
|
}
|
|
|
|
$validNodes = $this->validateNodes($nodes);
|
|
$invalidNodes = array_diff($nodes, $validNodes);
|
|
|
|
if (!empty($invalidNodes)) {
|
|
$invalidCount = count($invalidNodes);
|
|
$output->error("[错误] 发现 {$invalidCount} 个无效的权限节点:");
|
|
foreach (array_values($invalidNodes) as $node) {
|
|
$output->error(" - {$node}");
|
|
}
|
|
$output->writeln('');
|
|
}
|
|
|
|
if (empty($validNodes)) {
|
|
$output->error("有效权限节点数: 0");
|
|
$output->error("分配失败");
|
|
return false;
|
|
}
|
|
|
|
$existingNodes = SystemAuthNode::where('auth_id', $roleId)
|
|
->column('node');
|
|
$newNodes = array_diff($validNodes, $existingNodes);
|
|
|
|
if (empty($newNodes)) {
|
|
$output->info('所有权限节点已存在,无需重复分配');
|
|
return true;
|
|
}
|
|
|
|
$authNodes = [];
|
|
foreach ($newNodes as $node) {
|
|
$authNodes[] = [
|
|
'auth_id' => $roleId,
|
|
'node' => $node,
|
|
];
|
|
}
|
|
|
|
(new SystemAuthNode())->saveAll($authNodes);
|
|
|
|
$output->info('权限分配成功');
|
|
$output->info('角色ID: ' . $roleId);
|
|
$output->info('新增的权限节点: ' . implode(', ', $newNodes));
|
|
$output->info('该角色总权限节点数: ' . (count($existingNodes) + count($newNodes)));
|
|
} catch (\Throwable $e) {
|
|
$output->error('分配权限失败: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function parseNodes(string $nodesParam): array
|
|
{
|
|
$nodes = explode(',', $nodesParam);
|
|
$nodes = array_map('trim', $nodes);
|
|
$nodes = array_filter($nodes);
|
|
|
|
return array_values($nodes);
|
|
}
|
|
|
|
protected function validateNodes(array $nodes): array
|
|
{
|
|
$nodeService = new NodeService();
|
|
$allNodes = $nodeService->getNodeParis();
|
|
|
|
$validNodes = [];
|
|
foreach ($nodes as $node) {
|
|
if (isset($allNodes[$node])) {
|
|
$validNodes[] = $node;
|
|
}
|
|
}
|
|
|
|
return $validNodes;
|
|
}
|
|
}
|