<?php

namespace App\Http\Middleware;

use Closure, Auth, Redirect;

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param \role $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if (Auth::check()) {
            $user = Auth::user();
            $explode = explode("|", $role);

            if (in_array($user->role, $explode)) {
                return $next($request);
            }
        }

        return Redirect::to('login')
                ->with('messageFail', 'Fail')
                ->with('messageDetail', 'No permission please contact Administrator');
    }
}
