mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
114 lines
3.3 KiB
PHP
114 lines
3.3 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 AdminRolePermissionRevokeBase extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
parent::configure();
|
|
|
|
$this->setName('admin:role:permission:revoke')
|
|
->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)) {
|
|
$output->comment('警告: 以下权限节点无效: ' . implode(', ', array_values($invalidNodes)));
|
|
}
|
|
|
|
if (empty($validNodes)) {
|
|
$output->error('没有有效的权限节点');
|
|
return false;
|
|
}
|
|
|
|
$deletedCount = SystemAuthNode::where('auth_id', $roleId)
|
|
->where('node', 'in', $validNodes)
|
|
->delete();
|
|
|
|
if ($deletedCount == 0) {
|
|
$output->error('没有权限可以撤回(角色不包含指定的权限节点)');
|
|
return false;
|
|
}
|
|
|
|
$remainingCount = SystemAuthNode::where('auth_id', $roleId)->count();
|
|
|
|
$output->info('权限撤回成功');
|
|
$output->info('角色ID: ' . $roleId);
|
|
$output->info('撤回的权限节点: ' . implode(', ', $validNodes));
|
|
$output->info('该角色剩余权限节点数: ' . $remainingCount);
|
|
} 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;
|
|
}
|
|
}
|