我希望创建一个分流到一个管理区,在用户表中的类型列有一个A的值,否则他们被分流到一个用户区域。
然而,我似乎已经找到了许多不同的方法,从守卫到重定向和中间件组。你如何推荐和实现这样的登录?
发布于 2016-05-13 02:33:07
这取决于表的设置方式。最简单、最常见的方法是在users表上有一个列,表示该用户是否为管理员(如is_admin)。
如果您想让它们在登录后去不同的地方,最好的解决方案是覆盖原始AuthController的登录函数(只需创建一个新的postLogin),并根据字段is_admin进行重定向。
就像这样
public function postLogin(Request $request)
{
// setup rules for validation of user input
$rules = [
'username' => 'required',
'password' => 'required'
];
// run validation, if this does not pass Laravel will redirect back with input and errors
$this->validate($request, $rules);
// attempt to authenticate the user
if(Auth::attempt(['username' => $request->input('username'), 'password' => $request->input('password')]))
{
here you should check the user which just happens to be stored in Auth::user()
}
else
{
// the attempt was not correct, lets redirect back with input and an error
return Redirect::back()->withInput()->withErrors(['The user information is invalid']);
}
}
您需要创建一些中间件来确保访问路由的用户也是管理员。
发布于 2016-05-13 02:07:03
在members或user表中创建一个名为position的列,并存储一个值admin,用户名和密码用于以admin身份登录。
当表单提交时,然后检查位置是否等于管理员,然后在管理员页面上重定向,否则在用户页面上重定向,
发布于 2016-05-13 11:54:41
最简单的方法是使用sentinel设置授权。它为你的用户创建提供了角色,你也可以添加角色。您所要做的就是将包添加到您的composer文件中,composer更新并向artisan发布供应商。下面是如何实现这一点,sentinel setup.它为您提供了权限,但在您的情况下,您需要对您的中间件进行分组。你所要做的就是
Route::group(['middleware' => 'App\Http\Middleware\SentinelAdmin'], function()
{
//Your admin routes
});
定义您只希望管理员在该函数中访问的路由。类似地,您可以在用户中间件中定义用户路由。
Route::group(['middleware' => 'App\Http\Middleware\SentinelUser'], function()
{
// Your user routes
}
这些都是从包中为您提供的。您可以分别创建更多的角色和您自己的中间件。一旦在服务提供者中定义了中间件,您就有了它的简写表示,但为了更好地理解,我使用了完整的路径。
https://stackoverflow.com/questions/37194297
复制相似问题