【问题标题】:Laravel 5.4 - Bind AuthServiceProvider to route group?Laravel 5.4 - 将 AuthServiceProvider 绑定到路由组?
【发布时间】: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


    【解决方案1】:

    answer is here

    我刚刚从使用服务提供商切换到使用我自己的自定义中间件

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2018-01-18
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多