mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-07 10:32:48 +08:00
feat: 发布智能体版
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace base\common\command\admin\role;
|
||||
|
||||
use app\admin\model\SystemAuth;
|
||||
use app\common\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
|
||||
class AdminRoleCreateBase extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setName('admin:role:create')
|
||||
->addOption('title', null, Option::VALUE_REQUIRED, '角色名称')
|
||||
->addOption('remark', null, Option::VALUE_OPTIONAL, '角色备注')
|
||||
->setDescription('创建角色');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$title = $input->getOption('title');
|
||||
$remark = $input->getOption('remark');
|
||||
|
||||
if (empty($title)) {
|
||||
$output->error('角色名称不能为空');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$auth = new SystemAuth();
|
||||
$auth->title = $title;
|
||||
$auth->remark = $remark ?? '';
|
||||
$auth->sort = 0;
|
||||
$auth->status = 1;
|
||||
$auth->save();
|
||||
|
||||
$output->info('角色创建成功');
|
||||
$output->info('角色ID: ' . $auth->id);
|
||||
$output->info('角色名称: ' . $auth->title);
|
||||
} catch (\Throwable $e) {
|
||||
$output->error('创建角色失败: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
65
extend/base/common/command/admin/role/AdminRoleInfoBase.php
Normal file
65
extend/base/common/command/admin/role/AdminRoleInfoBase.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace base\common\command\admin\role;
|
||||
|
||||
use app\admin\model\SystemAuth;
|
||||
use app\admin\model\SystemAuthNode;
|
||||
use app\common\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
|
||||
class AdminRoleInfoBase extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setName('admin:role:info')
|
||||
->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 {
|
||||
$role = SystemAuth::find($roleId);
|
||||
if (empty($role)) {
|
||||
$output->error('角色ID ' . $roleId . ' 不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
$nodes = SystemAuthNode::where('auth_id', $roleId)
|
||||
->column('node');
|
||||
|
||||
$output->info('角色详情');
|
||||
$output->writeln('================================');
|
||||
$output->info('角色ID: ' . $role->id);
|
||||
$output->comment('角色名称: ' . $role->title);
|
||||
$output->comment('状态: ' . ($role->status == 1 ? '启用' : '禁用'));
|
||||
$output->comment('排序: ' . $role->sort);
|
||||
if (!empty($role->remark)) {
|
||||
$output->comment('备注: ' . $role->remark);
|
||||
}
|
||||
$output->writeln('--------------------------------');
|
||||
$output->info('权限节点列表 (' . count($nodes) . ' 个):');
|
||||
foreach ($nodes as $node) {
|
||||
$output->comment(' - ' . $node);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$output->error('获取角色详情失败: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
64
extend/base/common/command/admin/role/AdminRoleListBase.php
Normal file
64
extend/base/common/command/admin/role/AdminRoleListBase.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace base\common\command\admin\role;
|
||||
|
||||
use app\admin\model\SystemAuth;
|
||||
use app\admin\model\SystemAuthNode;
|
||||
use app\common\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
|
||||
class AdminRoleListBase extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setName('admin:role:list')
|
||||
->setDescription('查看角色列表');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
try {
|
||||
$roles = SystemAuth::where('delete_time', 0)
|
||||
->field('id,title,sort,status,remark')
|
||||
->order('sort', 'asc')
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
if (empty($roles)) {
|
||||
$output->comment('没有找到任何角色');
|
||||
return true;
|
||||
}
|
||||
|
||||
$output->info('角色列表');
|
||||
$output->writeln('================================');
|
||||
|
||||
foreach ($roles as $role) {
|
||||
$nodeCount = SystemAuthNode::where('auth_id', $role['id'])->count();
|
||||
$statusText = $role['status'] == 1 ? '启用' : '禁用';
|
||||
|
||||
$output->info('角色ID: ' . $role['id']);
|
||||
$output->comment(' 角色名称: ' . $role['title']);
|
||||
$output->comment(' 权限节点数: ' . $nodeCount);
|
||||
$output->comment(' 状态: ' . $statusText);
|
||||
$output->comment(' 排序: ' . $role['sort']);
|
||||
if (!empty($role['remark'])) {
|
||||
$output->comment(' 备注: ' . $role['remark']);
|
||||
}
|
||||
$output->writeln('--------------------------------');
|
||||
}
|
||||
|
||||
$output->info('总计: ' . count($roles) . ' 个角色');
|
||||
} catch (\Throwable $e) {
|
||||
$output->error('获取角色列表失败: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace base\common\command\admin\role;
|
||||
|
||||
use app\admin\model\SystemAuth;
|
||||
use app\admin\model\SystemAuthNode;
|
||||
use app\common\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
|
||||
class AdminRolePermissionListBase extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setName('admin:role:permission:list')
|
||||
->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 {
|
||||
$role = SystemAuth::find($roleId);
|
||||
if (empty($role)) {
|
||||
$output->error('角色ID ' . $roleId . ' 不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
$nodes = SystemAuthNode::where('auth_id', $roleId)
|
||||
->column('node');
|
||||
|
||||
$output->info('角色权限列表');
|
||||
$output->writeln('================================');
|
||||
$output->info('角色ID: ' . $role->id);
|
||||
$output->comment('角色名称: ' . $role->title);
|
||||
$output->writeln('--------------------------------');
|
||||
|
||||
if (empty($nodes)) {
|
||||
$output->comment('该角色没有分配任何权限节点');
|
||||
} else {
|
||||
$output->info('权限节点 (' . count($nodes) . ' 个):');
|
||||
foreach ($nodes as $node) {
|
||||
$output->comment(' - ' . $node);
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$output->error('获取角色权限失败: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user