From fac77de837b8f7999f1163f180a01c50bd224327 Mon Sep 17 00:00:00 2001 From: Majid Ghaemifar Date: Thu, 21 Dec 2023 15:25:37 +0330 Subject: [PATCH] fix permissions cache bug --- .../Role/Controllers/Admin/RoleController.stub | 7 ++++--- .../modules/Role/Models/PermissionRoute.stub | 5 ++--- .../Role/Traits/RefreshPermissionRouteCache.stub | 16 ---------------- .../User/Controllers/Admin/AdminController.stub | 3 ++- src/stubs/modules/User/Models/Admin.stub | 11 +++++++---- 5 files changed, 15 insertions(+), 27 deletions(-) delete mode 100644 src/stubs/modules/Role/Traits/RefreshPermissionRouteCache.stub diff --git a/src/stubs/modules/Role/Controllers/Admin/RoleController.stub b/src/stubs/modules/Role/Controllers/Admin/RoleController.stub index b0bc8a8..a05e3a1 100644 --- a/src/stubs/modules/Role/Controllers/Admin/RoleController.stub +++ b/src/stubs/modules/Role/Controllers/Admin/RoleController.stub @@ -4,6 +4,7 @@ namespace App\Modules\Role\Controllers\Admin; use App\Http\Controllers\Controller; use App\Modules\Role\Models\Permission; +use App\Modules\Role\Models\PermissionRoute; use App\Modules\Role\Requests\Admin\RoleRequest; use Spatie\Permission\Models\Role; @@ -28,7 +29,7 @@ class RoleController extends Controller { $role = Role::create(['name' => $request->get('name'), 'guard_name' => 'admin']); $role->syncPermissions($request->get('permissions')); - cache()->forget(config('permission.cache.key').'_routes'); + cache()->tags(PermissionRoute::$cacheTags)->flush(); return redirect()->route('admin.role.index')->with('success', trans('admin.saved')); } @@ -46,7 +47,7 @@ class RoleController extends Controller $role->name = $request->get('name'); $role->save(); $role->syncPermissions($request->get('permissions')); - cache()->forget(config('permission.cache.key').'_routes'); + cache()->tags(PermissionRoute::$cacheTags)->flush(); return redirect()->route('admin.role.index')->with('success', trans('admin.saved')); } @@ -54,7 +55,7 @@ class RoleController extends Controller public function destroy(RoleRequest $request) { Role::whereIn('id', $request->get('id'))->get()->each->delete(); - cache()->forget(config('permission.cache.key').'_routes'); + cache()->tags(PermissionRoute::$cacheTags)->flush(); return back()->with('success', trans('admin.deleted')); } diff --git a/src/stubs/modules/Role/Models/PermissionRoute.stub b/src/stubs/modules/Role/Models/PermissionRoute.stub index e15e9cc..6d32d71 100644 --- a/src/stubs/modules/Role/Models/PermissionRoute.stub +++ b/src/stubs/modules/Role/Models/PermissionRoute.stub @@ -2,13 +2,10 @@ namespace App\Modules\Role\Models; -use App\Modules\Role\Traits\RefreshPermissionRouteCache; use Illuminate\Database\Eloquent\Model; class PermissionRoute extends Model { - use RefreshPermissionRouteCache; - public $table = 'permission_routes'; protected $fillable = [ @@ -16,6 +13,8 @@ class PermissionRoute extends Model 'name', ]; + public static $cacheTags = ['permission-routes']; + public function permission() { return $this->belongsTo(Permission::class); diff --git a/src/stubs/modules/Role/Traits/RefreshPermissionRouteCache.stub b/src/stubs/modules/Role/Traits/RefreshPermissionRouteCache.stub deleted file mode 100644 index 2c033dd..0000000 --- a/src/stubs/modules/Role/Traits/RefreshPermissionRouteCache.stub +++ /dev/null @@ -1,16 +0,0 @@ -forget('permission_routes'); - }); - - static::deleted(function () { - cache()->forget('permission_routes'); - }); - } -} diff --git a/src/stubs/modules/User/Controllers/Admin/AdminController.stub b/src/stubs/modules/User/Controllers/Admin/AdminController.stub index 2c6a4c0..71936c2 100644 --- a/src/stubs/modules/User/Controllers/Admin/AdminController.stub +++ b/src/stubs/modules/User/Controllers/Admin/AdminController.stub @@ -3,6 +3,7 @@ namespace App\Modules\User\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Modules\Role\Models\PermissionRoute; use App\Modules\User\Models\Admin; use App\Modules\User\Requests\Admin\AdminRequest; use Spatie\Permission\Models\Role; @@ -94,6 +95,6 @@ class AdminController extends Controller */ public function clearPermissionCache($item): void { - cache()->forget(config('permission.cache.key') . '_routes_' . $item->id); + cache()->tags(PermissionRoute::$cacheTags)->forget(config('permission.cache.key') . '_routes_' . $item->id); } } diff --git a/src/stubs/modules/User/Models/Admin.stub b/src/stubs/modules/User/Models/Admin.stub index beca7b0..f9975f1 100644 --- a/src/stubs/modules/User/Models/Admin.stub +++ b/src/stubs/modules/User/Models/Admin.stub @@ -2,6 +2,7 @@ namespace App\Modules\User\Models; +use App\Modules\Role\Models\PermissionRoute; use App\Modules\Role\Traits\RefreshPermissionRouteCache; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -48,14 +49,16 @@ class Admin extends Authenticatable private function loadAllPermissionRoutes() { - return cache()->remember(config('permission.cache.key').'_routes_'.$this->id, config('permission.cache.expiration_time'), function () { + return cache() + ->tags(PermissionRoute::$cacheTags) + ->remember(config('permission.cache.key').'_routes_'.$this->id, config('permission.cache.expiration_time'), function () { return $this->loadMissing('roles.permissions.routes') ->roles->flatMap(function ($role) { - return $role->permissions->flatMap(function ($role) { - return $role->routes->pluck('name'); + return $role->permissions->flatMap(function ($permission) { + return $permission->routes->pluck('name'); }); })->unique(); - }); + }); } /**