简体   繁体   English

Laravel 5,委托 - 检查角色不起作用

[英]Laravel 5, Entrust - check roles not working

I'm new in Laravel.我是 Laravel 的新手。 I'm trying to use in Laravel 5 Zizaco/entrust (from laravel-5 branch).我正在尝试在 Laravel 5 Zizaco/entrust(来自 laravel-5 分支)中使用。 All working ok - attach rules, detach rules... but when I try check permissions I have problems.一切正常 - 附加规则,分离规则......但是当我尝试检查权限时,我遇到了问题。

First I try in routes.php, but in this place Entrust don't know who am I, hasRole and routeNeedsRole not working in routes.php.首先我在routes.php 中尝试,但是在这个地方Entrust 不知道我是谁, hasRolerouteNeedsRolerouteNeedsRole不起作用。

In middleware hasRole is working but routeNeedsRole not.在中间件中hasRole正在工作,但routeNeedsRole没有。 Trying use as second parameter string, array, same effect - abort(403) runs.尝试用作第二个参数字符串、数组,效果相同 - abort(403)运行。

Because hasRole is working this problem looks very strange for me.因为hasRole正在工作,这个问题对我来说看起来很奇怪。

composer dump-autoload - used, not solve problem composer dump-autoload - 使用,不解决问题

in routes.php在routes.php

Entrust::hasRole('superadmin');// => false
\Entrust::routeNeedsRole('admin/dashboard', ['superadmin'], abort(403, 'Forbidden'), false); // display 403 page

in middleware在中间件中

\Entrust::hasRole('superadmin'); // => true
\Entrust::routeNeedsRole('admin/dashboard', ['superadmin'], abort(403, 'Forbidden'), false); // display 403 page

My model User.php我的模型User.php

use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword, EntrustUserTrait;

routes.php路由文件

Route::group([ 'prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['auth', 'admin']], function (){
    Route::get('dashboard', [ 'as' => 'dashboard', 'uses' => "DashBoardController@index" ]);
});

I have also Role and Permission models looks like in Readme file https://github.com/Zizaco/entrust/tree/laravel-5我在自述文件中也有角色和权限模型https://github.com/Zizaco/entrust/tree/laravel-5

// sorry for my english. // 对不起我的英语不好。

Update: Laravel 5.1.11 and newer now come with built in Authorization .更新: Laravel 5.1.11 和更新版本现在内置了Authorization It is much more Laravel friendly and will always be well maintained.它对 Laravel 更加友好,并且将始终得到很好的维护。 Use this when possible尽可能使用它


You are using the middleware wrong.您使用的中间件错误。 There is a lot of Laravel 4 stuff still in the docs for Entrust so you have to be selective as to what you use from there. Entrust 的文档中有很多 Laravel 4 的东西,所以你必须有选择地从那里使用。 The middleware shouldn't be setting routeNeedsRole .中间件不应设置routeNeedsRole Actually routeNeedsRole doesn't really fit in L5 in my opinion.实际上routeNeedsRole在我看来并不适合 L5。 Here is how I would do it:这是我将如何做到的:

Create a new middleware with创建一个新的中间件

php artisan make:middleware AuthAdmin

Now in the newly generated app/Http/Middleware/AuthAdmin.php现在在新生成的 app/Http/Middleware/AuthAdmin.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AuthAdmin {

  protected $auth;

  public function __construct(Guard $auth) {
    $this->auth = $auth;
  }

  public function handle($request, Closure $next) {
    if ($this->auth->guest()) {
      if ($request->ajax()) {
        return response('Unauthorized.', 401);
      } else {
        return redirect()->guest('auth/login');
      }
    } else if(! $request->user()->hasRole('superadmin')) {
      return abort(404); //Or redirect() or whatever you want
    }
    return $next($request);
  }

}

This will do the same thing as the auth middleware but if they are already logged in and don't have the 'superadmin' role they will get the 404.这将与 auth 中间件做同样的事情,但如果他们已经登录并且没有“超级管理员”角色,他们将获得 404。

Next we need to add the middleware to routemiddleware.接下来我们需要将中间件添加到路由中间件中。 Do this in app/Http/Kernal.php :app/Http/Kernal.php执行此app/Http/Kernal.php

protected $routeMiddleware = [
  ...,
  'superadmin' => 'App\Http\Middleware\AuthAdmin',
];

This makes it possible to add the middleware to the controller.这使得向控制器添加中间件成为可能。 Now let's do that.现在让我们这样做。 In your controller we do this in the constructor:在您的控制器中,我们在构造函数中执行此操作:

public function __construct() {
  $this->middleware('superadmin');
}

This will add the middleware to the whole controller.这会将中间件添加到整个控制器。 You can be specific as to the routes if needed but for your case I would assume we need the whole controller protected.如果需要,您可以具体说明路线,但对于您的情况,我认为我们需要保护整个控制器。

Let me know if you need nay more help.如果您需要更多帮助,请告诉我。

Note: It would be ideal to make AuthAdmin run the 'auth' middleware first instead of copying the code but I don't know how to do that from within the middleware and we don't want to have to do middleware => ['auth', 'superadmin'] instead of just 'superadmin' .注意:让 AuthAdmin 首先运行“auth”中间件而不是复制代码是理想的,但我不知道如何从中间件中执行此操作,而且我们不想必须执行middleware => ['auth', 'superadmin']而不仅仅是'superadmin' If we didn't copy the 'auth' code over we would be trying to get ->hasRole() of null which would get an error.如果我们没有复制 'auth' 代码,我们将尝试获取->hasRole()的 null,这将得到一个错误。

Try it in your controllers:在您的控制器中尝试:

Auth::user()->hasRole('superadmin'); Auth::user()->hasRole('superadmin');

在我的情况下,这是一个缓存问题,一旦我清除了我的应用程序缓存 - 它解决了我遇到的 403 权限被拒绝问题。

php artisan cache:clear

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

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