laravel 5.1使用Entrust在中间件中的管理员角色

时间:2016-05-12 15:35:20

标签: php laravel-5.1 middleware entrust

我正在尝试使用' auth'来过滤路线。和' auth.admin'中间件应该像laravel 4.2' Route :: filter。但它不起作用。 这是我的路线

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

1 个答案:

答案 0 :(得分:0)

正如你所说,dashboard路由是经过身份验证的用户, 但是你检查用户是否不在admin role发送到仪表板,当他被发送到仪表板时,他被重定向回来,可能是由于另一个中间件启动,并且发送回登录并再次登录到仪表板,所以只要从条件中删除!