我正在使用 jwt-auth ,但我需要对服务提供商(政策内容)做一些事情,我需要让已登录的用户。
" boot"功能不适合我。知道如何在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;
});
}
}
}
}
}
答案 0 :(得分:1)
您可以扩展jwt-auth中间件并用服务提供商的东西覆盖句柄功能。或者您可以在jwt-auth中间件之后注册自己的中间件来检查您的策略。
也许这个提示有帮助