mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 23:42:48 +08:00
118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace base\common\command\admin\user;
|
||
|
||
use app\admin\model\SystemAdmin;
|
||
use app\admin\model\SystemAuth;
|
||
use app\common\console\Command;
|
||
use think\console\Input;
|
||
use think\console\input\Option;
|
||
use think\console\Output;
|
||
|
||
class AdminUserRoleRevokeBase extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
parent::configure();
|
||
|
||
$this->setName('admin:user:role:revoke')
|
||
->addOption('user-id', null, Option::VALUE_REQUIRED, '用户ID')
|
||
->addOption('role-ids', null, Option::VALUE_REQUIRED, '角色ID列表(逗号分隔)')
|
||
->setDescription('撤回用户角色');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
$userId = $input->getOption('user-id');
|
||
$roleIdsParam = $input->getOption('role-ids');
|
||
|
||
if (empty($userId)) {
|
||
$output->error('用户ID不能为空');
|
||
return false;
|
||
}
|
||
|
||
if (empty($roleIdsParam)) {
|
||
$output->error('角色ID不能为空');
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
$admin = SystemAdmin::find($userId);
|
||
if (empty($admin)) {
|
||
$output->error('用户ID ' . $userId . ' 不存在');
|
||
return false;
|
||
}
|
||
|
||
$roleIds = $this->parseRoleIds($roleIdsParam);
|
||
if (empty($roleIds)) {
|
||
$output->error('角色ID列表为空');
|
||
return false;
|
||
}
|
||
|
||
$validRoleIds = $this->validateRoleIds($roleIds);
|
||
$invalidRoleIds = array_diff($roleIds, $validRoleIds);
|
||
|
||
if (!empty($invalidRoleIds)) {
|
||
$output->comment('警告: 以下角色ID无效: ' . implode(', ', array_values($invalidRoleIds)));
|
||
}
|
||
|
||
if (empty($validRoleIds)) {
|
||
$output->error('没有有效的角色ID');
|
||
return false;
|
||
}
|
||
|
||
$existingAuthIds = $admin->auth_ids;
|
||
if (empty($existingAuthIds)) {
|
||
$output->error('用户未分配任何角色,无法撤回');
|
||
return false;
|
||
}
|
||
|
||
$existingAuthIdArray = array_filter(explode(',', $existingAuthIds));
|
||
|
||
$revokedRoleIds = array_intersect($validRoleIds, $existingAuthIdArray);
|
||
|
||
if (empty($revokedRoleIds)) {
|
||
$output->error('没有角色可以撤回(用户不拥有指定的角色)');
|
||
return false;
|
||
}
|
||
|
||
$remainingRoleIds = array_diff($existingAuthIdArray, $revokedRoleIds);
|
||
$admin->auth_ids = empty($remainingRoleIds) ? '' : implode(',', $remainingRoleIds);
|
||
$admin->save();
|
||
|
||
$output->info('角色撤回成功');
|
||
$output->info('用户ID: ' . $userId);
|
||
$output->info('撤回的角色ID: ' . implode(', ', $revokedRoleIds));
|
||
$output->info('该用户剩余角色数: ' . count($remainingRoleIds));
|
||
if (!empty($remainingRoleIds)) {
|
||
$output->info('剩余角色ID: ' . implode(', ', $remainingRoleIds));
|
||
}
|
||
} catch (\Throwable $e) {
|
||
$output->error('撤回角色失败: ' . $e->getMessage());
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
protected function parseRoleIds(string $roleIdsParam): array
|
||
{
|
||
$roleIds = explode(',', $roleIdsParam);
|
||
$roleIds = array_map('trim', $roleIds);
|
||
$roleIds = array_filter($roleIds);
|
||
|
||
return array_values($roleIds);
|
||
}
|
||
|
||
protected function validateRoleIds(array $roleIds): array
|
||
{
|
||
$validRoleIds = SystemAuth::whereIn('id', $roleIds)
|
||
->where('delete_time', 0)
|
||
->column('id');
|
||
|
||
return $validRoleIds;
|
||
}
|
||
}
|