use cached scopes
This commit is contained in:
parent
a79cb1c639
commit
61ce97ad79
37
app/Observers/ScopeObserver.php
Normal file
37
app/Observers/ScopeObserver.php
Normal 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();
|
||||
});
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user