<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Route-level permission check: `->middleware('permission:applications.view')`.
 * Accepts several slugs, any one of which grants access.
 */
class RequirePermission
{
    public function handle(Request $request, Closure $next, string ...$permissions): Response
    {
        abort_unless(
            $request->user()?->hasAnyPermission(...$permissions),
            403,
            'You do not have permission to perform this action.',
        );

        return $next($request);
    }
}
