Optimierung 3.6: Zugriffs-Gates und getrennte Impersonation-Sperre
- Gate::define('access-admin'|'access-customer') als Single Source of Truth im
AppServiceProvider (auch via @can/Policies nutzbar). EnsureUserIsAdmin und
EnsureUserIsCustomer pruefen jetzt ueber diese Gates.
- Impersonation-Redirect aus EnsureUserIsAdmin in die eigene Middleware
BlockAdminDuringImpersonation ausgelagert (Sitzungs-Bedingung statt
Rollenfrage); laeuft vor der Zugriffspruefung und ist zusaetzlich als
persistente Livewire-Middleware registriert.
- Routen-Alias 'can:' bewusst NICHT verwendet: die Admin-Middleware ist
persistente Livewire-Middleware und braucht eine konkrete Klasse; der
parametrisierte Alias kann dort nicht stehen. EnsureApiUserIsActive bleibt
eigenstaendig (is_active-Pruefung mit getesteter JSON-Antwort, kein Gate).
- Tests: Auth/AccessGatesTest; UserImpersonationTest bleibt gruen.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
4466858aac
commit
0d0edf1a6c
7 changed files with 104 additions and 16 deletions
42
tests/Feature/Auth/AccessGatesTest.php
Normal file
42
tests/Feature/Auth/AccessGatesTest.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Database\Seeders\RolesAndPermissionsSeeder;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Tests\TestCase;
|
||||
|
||||
beforeEach(function (): void {
|
||||
/** @var TestCase $this */
|
||||
$this->seed(RolesAndPermissionsSeeder::class);
|
||||
});
|
||||
|
||||
test('access-admin gate mirrors canAccessAdmin', function () {
|
||||
$admin = User::factory()->create(['is_active' => true]);
|
||||
$admin->assignRole('admin');
|
||||
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
$customer->assignRole('customer');
|
||||
|
||||
expect(Gate::forUser($admin)->allows('access-admin'))->toBeTrue()
|
||||
->and(Gate::forUser($customer)->allows('access-admin'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('access-customer gate mirrors canAccessCustomer', function () {
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
$customer->assignRole('customer');
|
||||
|
||||
$apiOnly = User::factory()->create(['is_active' => true]);
|
||||
$apiOnly->assignRole('api-only');
|
||||
|
||||
expect(Gate::forUser($customer)->allows('access-customer'))->toBeTrue()
|
||||
->and(Gate::forUser($apiOnly)->allows('access-customer'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('a non-admin user is forbidden from the admin dashboard', function () {
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
$customer->assignRole('customer');
|
||||
|
||||
$this->actingAs($customer)
|
||||
->get(route('dashboard'))
|
||||
->assertForbidden();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue