I'm using Laravel 5 on a Windows dev machine. I want to customize and use the Auth middleware throughout my application, to maintain authentication. My use case is a standard one. There are two (or three) classes of users - Admin and Regular (regular would be all users that are not admin).

我正在Windows开发机器上使用Laravel 5。我想在整个应用程序中定制和使用Auth中间件,以维护身份验证。我的用例是一个标准用例。有两个(或三个)用户类——Admin和Regular(常规是所有非Admin的用户)。

The Admin has the obvious role of backend management, and hence has a separate routing group /admin/, which should redirect an unlogged user to /admin/login. I have set it up like so..

Admin具有后端管理的明显角色,因此有一个单独的路由组/ Admin /,它应该将未登录的用户重定向到/ Admin /login。我是这样安排的。

Route::group(['middleware'=>'auth', 'prefix' => 'admin'], function() {
  Route::get('login','App\AuthController@getLogin');
  Route::post('login','App\AuthController@postLogin');
});

When the login form is posted, how do I ask Auth to add a filter

当登录表单被发布时,我如何要求Auth添加一个过滤器

  • either that only validate from among those users where 'is_admin' is true?
  • 或者只在“is_admin”为真的用户之间进行验证?
  • or ask it to first join a User and a UserRoles table to identify only users with an Admin role?
  • 或者让它首先加入一个用户和一个UserRoles表,只标识一个Admin角色的用户?

2 个解决方案

#1


5

I recommend you to define another middleware that detects if user is admin instead of modifying the auth. Now add this another middleware to your routes that only admins can access.

我建议您定义另一个中间件来检测用户是否为admin,而不是修改auth。现在将这个中间件添加到只有管理员才能访问的路由中。

Add several middleware to route like this

向这样的路由添加几个中间件

Route::group(['middleware' => ['auth','admin']], function() {

Middleware will look something like

中间件看起来是这样的

public function handle($request, Closure $next) {
  if (Auth::user()->role == "admin") {
    return $next($request);
  } else {
    return redirect("/")->withMyerror("You are not authorized for this action");
  }
}

更多相关文章

  1. 用户GROUP BY ERROR之间的SQL查询privatemsgs
  2. 使用PHP显示用户输入
  3. 根据网址上的国家/地区将用户重定向到正确的数据库,我使用i18n fo
  4. PHP结合Redis来限制用户或者IP某个时间段内访问的次数
  5. 使用JOIN获取有关两个用户的信息
  6. PHP:如果用户没有按下提交按钮,则Mysql回滚多个查询(通过ajax完成)
  7. Zend数据库适配器-未捕获异常-堆栈跟踪显示用户名和密码
  8. 在MySQL数据库中存储无法访问的用户
  9. init-connectMysql对用户操作加审计功能

随机推荐

  1. 函数的学习
  2. rollup是面向library的?!
  3. lerna入门指南
  4. react-redux源码解读
  5. Web Components
  6. 微信公众号自动回复图文消息
  7. JAVA虚拟机体系结构
  8. 最近厚着脸皮联系了不少同学
  9. 学C语言和学C++它有毛关系吗?
  10. GitHub还真把所有代码埋到北极地下了,我特