fix(admin): remove is_admin self-heal — RBAC is the only console boundary (no revocation bypass)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
feat/portal-design
nexxo 2026-07-25 18:35:14 +02:00
parent 22cbe9ac39
commit 406f753311
2 changed files with 11 additions and 20 deletions

View File

@ -2,7 +2,6 @@
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
@ -10,22 +9,14 @@ use Symfony\Component\HttpFoundation\Response;
class EnsureAdmin
{
/**
* Only operators (a user holding an RBAC role) may reach the admin console.
* A legacy is_admin account with no role is self-healed into Owner on access
* migrating it into RBAC rather than permanently trusting the old flag, so
* such accounts are never locked out yet the role boundary still governs.
* Only operators with the console.view capability may reach the admin console.
* Legacy is_admin accounts are migrated to roles by the seed_roles_and_permissions
* migration and the seeder the gate never trusts the bare is_admin flag, so
* an RBAC revocation is never silently undone here.
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
// Only a genuinely un-migrated legacy admin (is_admin, but NO roles at
// all) is promoted — never override an intentional demotion/other role.
if ($user instanceof User && $user->is_admin && $user->roles->isEmpty()) {
$user->assignRole('Owner');
}
abort_unless((bool) $user?->can('console.view'), 403);
abort_unless((bool) $request->user()?->can('console.view'), 403);
return $next($request);
}

View File

@ -20,13 +20,13 @@ it('grants console access to every operator role but not to customers', function
$this->actingAs($customer)->get(route('admin.overview'))->assertForbidden();
});
it('self-heals a legacy is_admin account with no role into Owner', function () {
$legacy = User::factory()->create(['is_admin' => true]);
$legacy->syncRoles([]); // simulate a pre-RBAC admin with no operator role
it('denies console access to a role-less account even if is_admin is set', function () {
// RBAC is the boundary — the bare legacy flag must never grant access, so a
// revoked operator (roles removed) cannot reach the console.
$noRole = User::factory()->create(['is_admin' => true]);
$noRole->syncRoles([]);
$this->actingAs($legacy)->get(route('admin.overview'))->assertOk();
expect($legacy->fresh()->hasRole('Owner'))->toBeTrue();
$this->actingAs($noRole)->get(route('admin.overview'))->assertForbidden();
});
it('denies datacenter management to roles without the capability', function () {