How can i login with single role if user have multiple role assigned in laravel?

☆樱花仙子☆ 提交于 2019-12-25 18:49:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!