gpt4 book ai didi

php - 在中间件之后执行服务提供者

转载 作者:搜寻专家 更新时间:2023-10-31 21:28:20 26 4
gpt4 key购买 nike

我正在使用 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;
});
}
}
}
}
}

最佳答案

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

也许这个提示有帮助

关于php - 在中间件之后执行服务提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com