Files
ulthon_admin/extend/base/common/command/admin/role/AdminRoleDeleteBase.php
2026-03-26 20:22:34 +08:00

64 lines
1.7 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\model\SystemAdmin;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class AdminRoleDeleteBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('admin:role:delete')
->addOption('role-id', null, Option::VALUE_REQUIRED, '角色ID')
->setDescription('删除角色');
}
protected function execute(Input $input, Output $output)
{
$roleId = $input->getOption('role-id');
if (empty($roleId)) {
$output->error('角色ID不能为空');
return false;
}
try {
$auth = SystemAuth::find($roleId);
if (empty($auth)) {
$output->error('角色ID ' . $roleId . ' 不存在');
return false;
}
$checkUsers = SystemAdmin::where('auth_ids', 'like', '%' . $roleId . '%')
->where('delete_time', 0)
->count();
if ($checkUsers > 0) {
$output->error('角色ID ' . $roleId . ' 已分配给 ' . $checkUsers . ' 个用户,无法删除');
return false;
}
SystemAuthNode::where('auth_id', $roleId)->delete();
$auth->delete();
$output->info('角色删除成功');
$output->info('角色ID: ' . $roleId);
} catch (\Throwable $e) {
$output->error('删除角色失败: ' . $e->getMessage());
return false;
}
return true;
}
}