【发布时间】:2017-07-22 17:15:35
【问题描述】:
我的路线分为两组:web(默认)组和admin(自定义)组。 admin 组使用 auth 中间件。其他一切都使用web 中间件。
我遇到了一个问题,我的 AuthServiceProvider 在两个组中都查询我的权限模型...但我只想在用户请求访问我的admin 组中的路由时查询我的权限模型。这是我的 AuthServiceProvider:
<?php
namespace App\Providers;
use App\Models\Permission;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy'
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::before( function($user){
if($user->isAdmin()){
return true;
}
});
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function ($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}
protected function getPermissions()
{
return Permission::with('roles')->get();
}
因此,在我的web 中间件中的每个请求中,我的应用都会在不需要时查询用户的权限。在我的网络中间件中查询任何权限都没有需要。那么如何告诉我的 AuthServiceProvider 仅检查我的 Auth 组(或中间件)中的路由的权限?
【问题讨论】:
标签: php authentication authorization roles laravel-5.4