【问题标题】:laravel 5.1 admin role in middleware using Entrustlaravel 5.1 中间件中使用 Entrust 的管理员角色
【发布时间】:2016-05-12 15:35:20
【问题描述】:

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

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

内核.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);
            }
        }

【问题讨论】:

  • 你遇到了什么问题,有什么错误吗?
  • 重定向循环问题
  • 你能告诉我你的路线('dashboard')指向哪里吗?
  • Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Auth\AuthController@dashboard'));并返回 view('dashboard') ->with('title','Dashboard')
  • 我在成功尝试后使用了这个 return redirect()->intended('dashboard');

标签: php laravel-5.1 middleware entrust


【解决方案1】:

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

【讨论】:

  • 谢谢你,@rummykhan。我还将普通用户发送到限制某些功能的仪表板。但是,我找到了一个解决方案 Entrust 的角色:管理员功能,因为它不需要额外的中间件。再次感谢您
猜你喜欢
  • 2017-10-12
  • 2015-03-01
  • 2016-04-13
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
相关资源
最近更新 更多