简体   繁体   中英

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);
            }
        }

As u said that ur dashboard route is for authenticated user, But ur checking if user is not in admin role send to dashboard, and when he is sent to dashboard he is redirected back, probably due to another middleware kick in, and that send back to login and from login again to dashboard, so just remove ! from ur if condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM