File size: 818 Bytes
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7550e0
 
 
 
 
b2dcf0f
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

namespace App\Http\Middleware;

use App\Domain\Identity\RbacService;
use App\Shared\Http\ApiResponse;
use Closure;
use Illuminate\Http\Request;

class RequirePermission
{
    public function __construct(private readonly RbacService $rbac)
    {
    }

    public function handle(Request $request, Closure $next, string $permission)
    {
        // Staging bypass: skip permission check when auth bypass is active.
        if (config('app.auth_bypass', false)) {
            return $next($request);
        }

        $userId = $request->attributes->get('actor_user_id');

        if ($userId === null || ! $this->rbac->can($userId, $permission)) {
            return ApiResponse::error('FORBIDDEN', 'You are not authorized to perform this action.', [], 403);
        }

        return $next($request);
    }
}