feat: 增强管理员权限与角色管理功能
- 在 SyncAdminAuthorizationCommand 中新增对代理和抽奖菜单操作的同步功能,确保缺失的菜单操作行能够被创建。 - 更新多个控制器中的权限检查逻辑,使用 hasPermissionCode 替代原有的权限验证方式,提升权限管理的灵活性。 - 引入 ApiMessage 统一错误响应格式,确保在权限不足时返回一致的错误信息。 - 更新 AdminRole 和 AdminUser 模型,增强角色与用户的权限管理功能,支持更细粒度的权限控制。
This commit is contained in:
62
tests/Feature/AdminAgentRolePermissionPersistTest.php
Normal file
62
tests/Feature/AdminAgentRolePermissionPersistTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AdminRole;
|
||||
use App\Models\AdminUser;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
|
||||
});
|
||||
|
||||
test('platform role sync persists agent role and user manage slugs', function (): void {
|
||||
$super = AdminUser::query()->create([
|
||||
'username' => 'persist_super',
|
||||
'name' => 'Super',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($super);
|
||||
$token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
$role = AdminRole::query()->create([
|
||||
'slug' => 'agent_persist_test',
|
||||
'name' => 'Agent Persist Test',
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->putJson('/api/v1/admin/admin-roles/'.$role->id.'/permissions', [
|
||||
'permission_slugs' => [
|
||||
'prd.agent.view',
|
||||
'prd.agent.manage',
|
||||
'prd.agent.role.view',
|
||||
'prd.agent.role.manage',
|
||||
'prd.agent.user.view',
|
||||
'prd.agent.user.manage',
|
||||
],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.permission_slugs', function ($slugs): bool {
|
||||
$list = is_array($slugs) ? $slugs : [];
|
||||
|
||||
return in_array('prd.agent.role.manage', $list, true)
|
||||
&& in_array('prd.agent.user.manage', $list, true);
|
||||
});
|
||||
});
|
||||
|
||||
test('sync fails with clear error when agent manage menu actions are missing', function (): void {
|
||||
DB::table('admin_menu_actions')
|
||||
->whereIn('permission_code', ['agent.role.manage', 'agent.user.manage'])
|
||||
->delete();
|
||||
|
||||
$role = AdminRole::query()->create([
|
||||
'slug' => 'agent_missing_catalog',
|
||||
'name' => 'Missing Catalog',
|
||||
]);
|
||||
|
||||
expect(fn () => $role->syncLegacyPermissionSlugs(['prd.agent.role.manage']))
|
||||
->toThrow(\Illuminate\Validation\ValidationException::class);
|
||||
});
|
||||
Reference in New Issue
Block a user