- 在 SyncAdminAuthorizationCommand 中新增对代理和抽奖菜单操作的同步功能,确保缺失的菜单操作行能够被创建。 - 更新多个控制器中的权限检查逻辑,使用 hasPermissionCode 替代原有的权限验证方式,提升权限管理的灵活性。 - 引入 ApiMessage 统一错误响应格式,确保在权限不足时返回一致的错误信息。 - 更新 AdminRole 和 AdminUser 模型,增强角色与用户的权限管理功能,支持更细粒度的权限控制。
86 lines
3.1 KiB
PHP
86 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* 确保代理「角色 / 账号」拆分后的 menu_action 行存在(migrate 未跑或漏跑时 auth-sync 可补救)。
|
||
*/
|
||
final class AdminAgentPermissionMenuActionSync
|
||
{
|
||
public static function syncMissing(): int
|
||
{
|
||
$now = Carbon::now();
|
||
$viewActionId = DB::table('admin_action_catalog')->where('code', 'view')->value('id');
|
||
$manageActionId = DB::table('admin_action_catalog')->where('code', 'manage')->value('id');
|
||
if ($viewActionId === null || $manageActionId === null) {
|
||
return 0;
|
||
}
|
||
|
||
$agentMenuId = (int) DB::table('admin_menus')->where('code', 'system.agents')->value('id');
|
||
if ($agentMenuId === 0) {
|
||
return 0;
|
||
}
|
||
|
||
$rolesMenuId = self::ensureChildMenu($agentMenuId, 'system.agents.roles', '代理角色', $now);
|
||
$usersMenuId = self::ensureChildMenu($agentMenuId, 'system.agents.users', '代理账号', $now);
|
||
|
||
$created = 0;
|
||
$created += self::ensureMenuAction((int) $rolesMenuId, (int) $viewActionId, 'agent.role.view', '代理角色查看', $now) ? 1 : 0;
|
||
$created += self::ensureMenuAction((int) $rolesMenuId, (int) $manageActionId, 'agent.role.manage', '代理角色管理', $now) ? 1 : 0;
|
||
$created += self::ensureMenuAction((int) $usersMenuId, (int) $viewActionId, 'agent.user.view', '代理账号查看', $now) ? 1 : 0;
|
||
$created += self::ensureMenuAction((int) $usersMenuId, (int) $manageActionId, 'agent.user.manage', '代理账号管理', $now) ? 1 : 0;
|
||
|
||
return $created;
|
||
}
|
||
|
||
private static function ensureChildMenu(int $parentId, string $code, string $name, Carbon $now): int
|
||
{
|
||
$existing = DB::table('admin_menus')->where('code', $code)->value('id');
|
||
if ($existing !== null) {
|
||
return (int) $existing;
|
||
}
|
||
|
||
return (int) DB::table('admin_menus')->insertGetId([
|
||
'parent_id' => $parentId,
|
||
'menu_type' => 'button',
|
||
'code' => $code,
|
||
'name' => $name,
|
||
'path' => null,
|
||
'route_name' => null,
|
||
'component' => null,
|
||
'icon' => null,
|
||
'active_menu_code' => 'system.agents',
|
||
'sort_order' => 0,
|
||
'is_visible' => false,
|
||
'is_cache' => false,
|
||
'is_external' => false,
|
||
'status' => 1,
|
||
'meta_json' => null,
|
||
'created_at' => $now,
|
||
'updated_at' => $now,
|
||
]);
|
||
}
|
||
|
||
private static function ensureMenuAction(int $menuId, int $actionId, string $permissionCode, string $name, Carbon $now): bool
|
||
{
|
||
if (DB::table('admin_menu_actions')->where('permission_code', $permissionCode)->exists()) {
|
||
return false;
|
||
}
|
||
|
||
DB::table('admin_menu_actions')->insert([
|
||
'menu_id' => $menuId,
|
||
'action_id' => $actionId,
|
||
'permission_code' => $permissionCode,
|
||
'name' => $name,
|
||
'status' => 1,
|
||
'created_at' => $now,
|
||
'updated_at' => $now,
|
||
]);
|
||
|
||
return true;
|
||
}
|
||
}
|