简体   繁体   English

Laravel 5 - 根据用户在身份验证中的角色将用户重定向到特定页面

[英]Laravel 5 - Redirecting a user to a specific page based on their role on authentication

I am using the Bican Roles package with Laravel 5 and i'm trying to figure out how to modify the AuthController class to redirect a user to their designated page based on their role.我在 Laravel 5 中使用Bican Roles包,我试图弄清楚如何修改 AuthController 类以根据用户的角色将用户重定向到他们指定的页面。 I read on an article somewhere that i would need to create the method authenticate to override the default function but i'm unsure how i would include all the methods of the bican class.我在某处读到一篇文章,我需要创建方法验证来覆盖默认函数,但我不确定如何包含 bican 类的所有方法。 For example on the github page of bicon it says to use例如在 bicon 的 github 页面上,它说要使用

$user->level to get the users level but i get an error, $user->level 获取用户级别,但出现错误,

protected function authenticated($user)
    {
        dd($user->level);
    }

If I understand the problem:如果我理解问题:

  1. You can create the middleware to checking user's role on specific routes.您可以创建中间件来检查用户在特定路由上的角色。

  2. On your method authenticated() , variable $users is not defined.在您的方法authenticated() ,未定义变量$users Documentation of bican/roles package uses shorthand of Auth::user() to get data of current user. bican/roles包的文档使用Auth::user()简写来获取当前用户的数据。 So you should use:所以你应该使用:

     Auth::user()->is('YOUR_ROLE_NAME'); # To check user role Auth::user()->can('YOUR.ACTION'); # To check user permission Auth::user()->level(); # To check user level Auth::user()->allowed('ACTION', $someModel); # To check user permission to object

I read on an article somewhere that i would need to create the method authenticate to override the default function我在某处读到一篇文章,我需要创建方法验证以覆盖默认函数

Until that default function is in app directory, it pass.直到该默认函数在app目录中,它才会通过。 But Laravel controllers are so flexible that you can do it in another ways ( look first point ).但是 Laravel 控制器非常灵活,您可以通过其他方式来实现(请看第一点)。

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

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