I'm new in Laravel. I'm trying to use in Laravel 5 Zizaco/entrust (from laravel-5 branch). All working ok - attach rules, detach rules... but when I try check permissions I have problems.
我刚在Laravel。我正试着使用Laravel 5 Zizaco/委托(来自laravel-5分公司)。一切正常工作-附加规则,分离规则…但是当我尝试检查权限时,我有问题。
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.
首先,我尝试在路线上。php,但是在这个地方委托不知道我是谁,hasRole和routeNeedsRole不工作在routes.php中。
In middleware hasRole
is working but routeNeedsRole
not. Trying use as second parameter string, array, same effect - abort(403)
runs.
在中间件中,hasRole正在发挥作用,但却没有发挥作用。尝试使用第二个参数字符串,数组,同样的效果- abort(403)运行。
Because hasRole
is working this problem looks very strange for me.
因为hasRole在工作,这个问题对我来说很奇怪。
composer dump-autoload
- used, not solve problem
作曲人自编自用,不解决问题。
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
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
我在Readme文件中也有角色和权限模型,https://github.com/Zizaco/entrust/tree/laravel-5。
// sorry for my english.
//对不起我的英语。
2 个解决方案
#1
11
Update: Laravel 5.1.11 and newer now come with built in Authorization. It is much more Laravel friendly and will always be well maintained. Use this when possible
更新:Laravel 5.1.11和更新的现在是建立在授权。它更加友好,并且将永远保持良好。尽可能使用这个
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. The middleware shouldn't be setting routeNeedsRole
. Actually routeNeedsRole
doesn't really fit in L5 in my opinion. Here is how I would do it:
您使用的是中间件错误。有很多Laravel 4的东西还在文档中,所以你必须对你在那里使用的东西有选择性。中间件不应该设置routeNeedsRole。实际上,在我看来,罗山的角色并不适合L5。下面是我的做法:
Create a new middleware with
创建一个新的中间件。
php artisan make:middleware AuthAdmin
Now in the newly generated app/Http/Middleware/AuthAdmin.php
现在在新生成的app/Http/中间件/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
:
接下来,我们需要将中间件添加到routemiddleware中。在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'
. If we didn't copy the 'auth' code over we would be trying to get ->hasRole()
of null which would get an error.
注意:首先要让AuthAdmin运行“auth”中间件,而不是复制代码,但我不知道如何在中间件中实现这一点,而且我们也不希望必须做中间件=> ['auth', 'superadmin'],而不是'superadmin'。如果我们没有复制“auth”代码,那么我们就会尝试得到null的>hasRole(),这样就会出错。
#2
0
Try it in your controllers:
在控制器中试试:
Auth::user()->hasRole('superadmin');
身份验证:用户()- > hasRole(“超级管理员”几个);
#1
11
Update: Laravel 5.1.11 and newer now come with built in Authorization. It is much more Laravel friendly and will always be well maintained. Use this when possible
更新:Laravel 5.1.11和更新的现在是建立在授权。它更加友好,并且将永远保持良好。尽可能使用这个
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. The middleware shouldn't be setting routeNeedsRole
. Actually routeNeedsRole
doesn't really fit in L5 in my opinion. Here is how I would do it:
您使用的是中间件错误。有很多Laravel 4的东西还在文档中,所以你必须对你在那里使用的东西有选择性。中间件不应该设置routeNeedsRole。实际上,在我看来,罗山的角色并不适合L5。下面是我的做法:
Create a new middleware with
创建一个新的中间件。
php artisan make:middleware AuthAdmin
Now in the newly generated app/Http/Middleware/AuthAdmin.php
现在在新生成的app/Http/中间件/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
:
接下来,我们需要将中间件添加到routemiddleware中。在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'
. If we didn't copy the 'auth' code over we would be trying to get ->hasRole()
of null which would get an error.
注意:首先要让AuthAdmin运行“auth”中间件,而不是复制代码,但我不知道如何在中间件中实现这一点,而且我们也不希望必须做中间件=> ['auth', 'superadmin'],而不是'superadmin'。如果我们没有复制“auth”代码,那么我们就会尝试得到null的>hasRole(),这样就会出错。
#2
0
Try it in your controllers:
在控制器中试试:
Auth::user()->hasRole('superadmin');
身份验证:用户()- > hasRole(“超级管理员”几个);