Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CheckPermission | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Middleware; |
| 4 | |
| 5 | use App\Services\Permission\PermissionService; |
| 6 | use Closure; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Symfony\Component\HttpFoundation\Response; |
| 9 | |
| 10 | /** |
| 11 | * Applied to the whole admin group. Looks up which ability guards the |
| 12 | * current route (config/permissions.php) and blocks the request with 403 |
| 13 | * if the staff member's role doesn't grant it. No mapping / rbac off -> |
| 14 | * passes straight through. |
| 15 | */ |
| 16 | class CheckPermission |
| 17 | { |
| 18 | public function __construct(private readonly PermissionService $permissions) {} |
| 19 | |
| 20 | public function handle(Request $request, Closure $next): Response |
| 21 | { |
| 22 | if (! $this->permissions->enforced()) { |
| 23 | return $next($request); |
| 24 | } |
| 25 | |
| 26 | $ability = $this->permissions->abilityForRoute($request->route()?->getName()); |
| 27 | |
| 28 | if ($ability && ! $this->permissions->allows($ability)) { |
| 29 | abort(403, 'Your role does not allow this. Ask an owner or manager for access.'); |
| 30 | } |
| 31 | |
| 32 | return $next($request); |
| 33 | } |
| 34 | } |