Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADVAPP-311]: Improve the speed of permission / role loading (part 2) #533

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

use App\Models\Tenant;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Artisan;
use AdvisingApp\Authorization\Models\Role;
use Spatie\Permission\PermissionRegistrar;
Expand Down Expand Up @@ -65,7 +66,6 @@ public function handle(): int

$currentTenant = Tenant::current();

// Seed roles and permissions
Artisan::call(
command: SetupRoles::class,
parameters: [
Expand All @@ -92,6 +92,10 @@ public function handle(): int

// Artisan::call(SetupRoleGroups::class);

// Remove api/web model permissions from cache
Cache::forget('application.model.web.permissions');
Cache::forget('application.model.api.permissions');

return self::SUCCESS;
}

Expand All @@ -111,6 +115,7 @@ function (Role $role) {
$this->syncPermissionFor('web', $role);
}
);

$this->newLine();
}

Expand All @@ -124,6 +129,7 @@ function (Role $role) {
$this->syncPermissionFor('api', $role);
}
);

$this->newLine();
}

Expand All @@ -138,13 +144,13 @@ protected function syncPermissionFor(string $guard, Role $role): void
path: "roles/{$guard}/{$roleFileName}"
);

collect($permissions)->each(function ($specificPermissions, $permissionConvention) use ($role, $guard) {
collect($permissions)->map(function ($specificPermissions, $permissionConvention) use ($role, $guard) {
if (blank($specificPermissions)) {
return;
}

collect($specificPermissions)
->each(function ($specificPermission, $resource) use ($role, $guard) {
->map(function ($specificPermission, $resource) use ($role, $guard) {
if (! is_array($specificPermission)) {
$this->syncCustomPermissions($role, $specificPermission, $guard);
} else {
Expand All @@ -154,31 +160,34 @@ protected function syncPermissionFor(string $guard, Role $role): void
});
}

protected function syncCustomPermissions(Role $role, string $specificPermission, string $permissionType): void
protected function syncCustomPermissions(Role $role, string $specificPermission, string $guard): void
{
$foundPermissions = Permission::firstWhere([
'name' => $specificPermission,
'guard_name' => $permissionType,
'guard_name' => $guard,
])->name;

$role->syncPermissions([$role->permissions, $foundPermissions]);
}

protected function syncModelPermissions(Role $role, string $resource, array $specificPermission, string $permissionType): void
protected function syncModelPermissions(Role $role, string $resource, array $specificPermission, string $guard): void
{
if (count($specificPermission) === 1 && $specificPermission[0] === '*') {
$foundPermissions = Permission::where('name', 'like', "{$resource}.%")
->where('guard_name', $permissionType)
->where('guard_name', $guard)
->pluck('name');
} else {
$foundPermissions = collect($specificPermission)->map(function ($permission) use ($resource, $permissionType) {
return Permission::firstWhere([
'name' => "{$resource}.{$permission}",
'guard_name' => $permissionType,
])->name;
$permissionNames = collect($specificPermission)->map(function ($permission) use ($resource) {
return "{$resource}.{$permission}";
});

$foundPermissions = collect($specificPermission)->map(function () use ($permissionNames, $guard) {
return Permission::where('guard_name', $guard)
->whereIn('name', $permissionNames)
->pluck('name');
});
}

$role->syncPermissions([$role->permissions, $foundPermissions]);
$role->givePermissionTo($foundPermissions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

use ReflectionClass;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use App\Models\Attributes\NoPermissions;
use App\Actions\Finders\ApplicationModules;

Expand All @@ -46,6 +47,8 @@
// Or override the functionality provided here.
trait DefinesPermissions
{
public const SECONDS_TO_CACHE = 600; // 10 minutes

public function getWebPermissions(): Collection
{
return collect($this->webPermissions());
Expand All @@ -62,11 +65,13 @@ protected function webPermissions(): array
return [];
}

return resolve(ApplicationModules::class)
->moduleConfig(
module: 'authorization',
path: 'permissions/web/model'
);
return Cache::remember('application.model.web.permissions', self::SECONDS_TO_CACHE, function () {
return resolve(ApplicationModules::class)
->moduleConfig(
module: 'authorization',
path: 'permissions/web/model'
);
});
Orrison marked this conversation as resolved.
Show resolved Hide resolved
}

protected function apiPermissions(): array
Expand All @@ -75,10 +80,12 @@ protected function apiPermissions(): array
return [];
}

return resolve(ApplicationModules::class)
->moduleConfig(
module: 'authorization',
path: 'permissions/api/model'
);
return Cache::remember('application.model.api.permissions', self::SECONDS_TO_CACHE, function () {
return resolve(ApplicationModules::class)
->moduleConfig(
module: 'authorization',
path: 'permissions/api/model'
);
});
}
}