Skip to content

Commit

Permalink
Merge pull request #15 from mgh145/bugfix/permissions
Browse files Browse the repository at this point in the history
fix permissions cache bug
  • Loading branch information
sedehi authored Dec 21, 2023
2 parents 77b60a9 + fac77de commit f12636c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/stubs/modules/Role/Controllers/Admin/RoleController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
}

Expand All @@ -46,15 +47,15 @@ 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'));
}

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'));
}
Expand Down
5 changes: 2 additions & 3 deletions src/stubs/modules/Role/Models/PermissionRoute.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

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 = [
'permission_id',
'name',
];

public static $cacheTags = ['permission-routes'];

public function permission()
{
return $this->belongsTo(Permission::class);
Expand Down
16 changes: 0 additions & 16 deletions src/stubs/modules/Role/Traits/RefreshPermissionRouteCache.stub

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
11 changes: 7 additions & 4 deletions src/stubs/modules/User/Models/Admin.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
});
}

/**
Expand Down

0 comments on commit f12636c

Please sign in to comment.