简体   繁体   English

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

[英]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. 我正在尝试使用'auth'和'auth.admin'中间件来过滤路由,该中间件应该类似于laravel 4.2的Route :: filter。 But it's not working. 但这不起作用。 Here is my route 这是我的路线

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

Kernel.php 内核.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 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 ! 正如您所说的,您的dashboard路线是针对经过身份验证的用户的,但是您要检查用户是否不具有admin role ,然后将其发送到仪表板,并在将其发送到仪表板时将其重定向回去,这可能是由于另一个中间件的介入而导致的要登录,然后从再次登录到仪表板,只需删除! from ur if condition. 从ur如果条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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