【问题标题】:How to redirect intended user to a different route based on their role?如何根据角色将目标用户重定向到不同的路线?
【发布时间】:2015-09-09 20:30:31
【问题描述】:

我想根据用户的角色将我的用户重定向到不同的路线。我的应用程序中有两个安全区域,“管理员”和“仪表板”。我想检查用户是否经过身份验证,然后重定向到预期,但是如果用户具有角色编辑器,则应将其重定向到仪表板,否则如果他具有角色 admin 应重定向到管理区域。

我在登录时使用了 AuthenticatesAndRegistersUsers 类。我的自定义控制器上有这个:

 /**
 * The default redirecTo path.
 *
 */
 protected $redirectTo = '/dashboard';

因此,当用户通过身份验证时,它将被重定向到仪表板,但我想检查预期的 url 是否在管理组路由上,如果用户具有管理员角色,则应将其重定向到管理区域。

我正在使用这个中间件重定向到登录:

public function handle($request, Closure $next)
{
    if ($this->auth->guest())
    {
        if ($request->ajax())
        {
            return response('Unauthorized.', 401);
        }
        else
        {
            return redirect()->guest('auth/login');
        }
    }

    return $next($request);
}

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    我用这个。您还需要修改中间件 RedirectIfAuthenticated 使其不会路由回家。只是到不同用户的仪表板。

    public function authenticated()
    {
        if($request->user()->hasRole('admin'))
                 {
                     // return redirect()->intended(route('admin.index'));
                     return redirect()->route('admin.index');
                 }
             if($request->user()->hasRole('super'))
                 {
                     return redirect()->route('super.index');
                 }
             if($request->user()->hasRole('officer'))
                 {
                     return redirect()->route('officer.index');
                 }   
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是覆盖authenticated 方法

      public function authenticated()
          {
              if(Auth::check()) {
                  if(\Auth::user()->hasRole('Super Admin')) {
                      return redirect('/admin-dashboard');
                  } else {
                      return redirect('/user-dashbaord');
                  }
              }    
          }
      

      【讨论】:

        【解决方案3】:

        您可以覆盖 AuthController 中的 trait 使用的 redirectPath 方法以注入您需要的逻辑。像这样的:

        /**
         * Get the post register / login redirect path.
         *
         * @return string
         */
        public function redirectPath()
        {
            // Logic that determines where to send the user
            if (\Auth::user()->type == 'admin') {
                return '/admin';
            }
        
            return '/dashboard';
        }
        

        编辑:

        Laravel 在AuthenticatesAndRegistersUsers trait 中使用以下声明在成功登录后重定向用户:

        return redirect()->intended($this->redirectPath());
        

        这将尝试将用户重定向到之前尝试的 URL。

        如果您需要在用户已经登录时将其重定向到正确的位置,最好通过向您的身份验证中间件添加更多逻辑来完成。

        【讨论】:

        • 这应该可以,但是如果我想重定向到完整路径怎么办?例如:用户尝试转到“admin/settings/general”,被重定向到登录,他登录然后被重定向到/admin,而不是完整路径。
        • 默认情况下,Laravel 会首先尝试重定向到预期的 URL。如果没有,它将回退到这种方法。
        • 我应该在中间件上设置重定向 url 吗?
        • 您需要将redirectPath 方法放入您的AuthController 并根据您的需要修改逻辑。
        • 这里的问题似乎是由于 Entrust::routeNeedsRole('admin*', array('admin'), Redirect::guest('auth/login') );没有正确设置预期的网址。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 2015-02-15
        • 2020-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多