use cached scopes

This commit is contained in:
Asnxthaony 2021-08-07 13:20:47 +08:00
parent a79cb1c639
commit 61ce97ad79
No known key found for this signature in database
GPG Key ID: 1D36B94AA9417589
3 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Observers;
use App\Models\Scope;
use Illuminate\Support\Facades\Cache;
class ScopeObserver
{
/**
* Handle the Scope "saved" event.
*
* @return void
*/
public function saved()
{
$this->refreshCachedScopes();
}
/**
* Handle the Scope "deleted" event.
*
* @return void
*/
public function deleted()
{
$this->refreshCachedScopes();
}
protected function refreshCachedScopes()
{
Cache::forget('scopes');
Cache::rememberForever('scopes', function () {
return Scope::pluck('description', 'name')->toArray();
});
}
}

View File

@ -2,8 +2,8 @@
namespace App\Providers;
use App\Models\Scope;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Cache;
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
@ -45,7 +45,7 @@ class AuthServiceProvider extends ServiceProvider
'ReportsManagement.ReadWrite' => 'auth.oauth.scope.reports-management.readwrite',
];
$scopes = Scope::all()->pluck('description', 'name')->all();
$scopes = Cache::get('scopes', []);
Passport::tokensCan(array_merge($defaultScopes, $scopes));

View File

@ -3,6 +3,8 @@
namespace App\Providers;
use App\Listeners;
use App\Models\Scope;
use App\Observers\ScopeObserver;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
@ -39,4 +41,14 @@ class EventServiceProvider extends ServiceProvider
Listeners\SetAppLocale::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
Scope::observe(ScopeObserver::class);
}
}