<?php

namespace App\Http\Middleware;

use Closure;

class CheckRole
{
    /**
     * Handle an incoming request.
     * Redirect user to home page if he has not the previliage to make the next action
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $action=explode('.',\Route::currentRouteName())[0];
        $canAccess=$request->user()->canAccess($action);
        // dd($action,$request->user()->getRole(), $canAccess);
        if(!$canAccess){
            return redirect('/admin/');
        }

        return $next($request);
    }
}
