jeudi 12 mai 2016

laravel 5.1 admin role in middleware using Entrust

I am trying to filter route using 'auth' and 'auth.admin' middleware which should be like laravel 4.2's Route::filter. But it's not working. Here is my route

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'auth.admin']], function()
{
   // ... 
});

Kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'auth.admin' => \App\Http\Middleware\RedirectIfAdmin::class,
    'role' => Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => Zizaco\Entrust\Middleware\EntrustAbility::class,
];

RedirectIfAdmin.php

        <?php

        namespace App\Http\Middleware;

        use Closure;
        use Entrust;
        class RedirectIfAdmin
        {
            /**
             * Handle an incoming request.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Closure  $next
             * @return mixed
             */
            public function handle($request, Closure $next)
            {
                if (!Entrust::hasRole(config('customConfig.roles.admin'))) {
                    return redirect()->route('dashboard')
                                ->with('error', 'Access Denied');
                }
                return $next($request);
            }
        }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire