mirovit/nova-notifications

View on GitHub
src/Http/Middleware/Authorize.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Mirovit\NovaNotifications\Http\Middleware;

use Laravel\Nova\Nova;
use Mirovit\NovaNotifications\NovaNotifications;

class Authorize
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return \Illuminate\Http\Response
     */
    public function handle($request, $next)
    {
        $tool = collect(Nova::registeredTools())->first([$this, 'matchesTool']);

        return optional($tool)->authorize($request) ? $next($request) : abort(403);
    }

    /**
     * Determine whether this tool belongs to the package.
     *
     * @param  \Laravel\Nova\Tool  $tool
     * @return bool
     */
    public function matchesTool($tool)
    {
        return $tool instanceof NovaNotifications;
    }
}