问题
I have a use case that a user having multiple role but want to login user with a single role at a time and give the rights of respective role. For example a user have employee,manager & finance manager role and each role has its own permissions/rights. So, now i want the system ask for role selection when user trying to login in the system and on the basis of selection it would authenticate .
Laravel 5.4
Zizaco/entrust 1.3.x
TIA.
回答1:
You need multiple guard function for different user roles. you can edit that in config/auth.php
During sign in you can use a drop down menu. Based on that input you can define different guard in your controller. Your code can be something like the following;
if (request('role')=='admin')
if(Auth::guard('admin')->attempt($credentials)){
// your code to login
}
else if (request('role')=='user')
if(Auth::guard('user')->attempt($credentials)){
// your code to login
}
For more information on authentication you can view the documentation
来源:https://stackoverflow.com/questions/53528882/how-can-i-login-with-single-role-if-user-have-multiple-role-assigned-in-laravel