【问题标题】:Executing Service Provider after Middleware在中间件之后执行服务提供者
【发布时间】:2016-01-10 09:08:15
【问题描述】:

我正在使用 jwt-auth,但我需要与服务提供商(政策资料)做一些事情,并且我需要获取已登录的用户。

“启动”功能对我不起作用。知道如何在 jwt-auth 之后运行服务提供程序吗?

<?php

namespace App\Providers;

use CareerTown\Privileges\PrivilegesHolder;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate $gate
     *  parent::registerPolicies($gate);
     *
     * @return void
     */
    public function boot(GateContract $gate)
    {
        parent::boot($gate);

        if ( ! auth()->check()) {
            return false;
        }
        $userPrivileges = auth()->user()->privileges;

        $privilegesHolder = PrivilegesHolder::getInstance();

        foreach ($userPrivileges as $up) {
            if ($privilege = $privilegesHolder->getLevel($up->level)) {
                $privileges = array_dot($privilege->getPrivileges());
                foreach ($privileges as $key => $value) {
                    $gate->define($key.$up->company->key, function ($user, $object = null) use ($value, $up) {

                        if ($object->profile->user_id == $user->id) {
                            return true;
                        }
                        if ($object->id == $up->company_id) {
                            return boolval($value);
                        }

                        return false;
                    });
                }
            }
        }
    }
}

【问题讨论】:

    标签: php api laravel laravel-5.1 middleware


    【解决方案1】:

    您可以扩展 jwt-auth 中间件并用您的服务提供者的东西覆盖句柄函数。或者您可以注册自己的中间件以在 jwt-auth 中间件之后检查您的策略。

    也许这个提示有帮助

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 2020-01-31
      • 1970-01-01
      • 2018-10-10
      • 2020-12-21
      • 2016-04-11
      • 2023-03-28
      • 2015-06-18
      • 2016-08-05
      相关资源
      最近更新 更多