Skip to content

Commit

Permalink
optimize calling permission models
Browse files Browse the repository at this point in the history
Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode committed Aug 11, 2024
1 parent de29efa commit 9f24a05
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 71 deletions.
15 changes: 7 additions & 8 deletions src/Actions/CreateRoleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
use Illuminate\Database\Eloquent\Model;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Data\RoleData;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\PermissionRegistrar;

final readonly class CreateRoleAction
readonly class CreateRoleAction
{
public function __construct(private PermissionRegistrar $permissionRegistrar) {}
public function __construct(private RoleContract $roleContract) {}

public function execute(RoleData $roleData): RoleContract & Model
{
/** @var RoleContract&Model $role */
$role = $this->permissionRegistrar->getRoleClass()::create([
'name' => $roleData->name,
'guard_name' => $roleData->guard_name,
]);
$role = $this->roleContract->findOrCreate(
name: $roleData->name,
guardName: $roleData->guard_name,
);

$role->syncPermissions($roleData->permissions);
$role->givePermissionTo($roleData->permissions);

return $role;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/EditRoleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Data\RoleData;
use Spatie\Permission\Contracts\Role as RoleContract;

final readonly class EditRoleAction
readonly class EditRoleAction
{
public function execute(RoleContract & Model $role, RoleData $roleData): RoleContract & Model
{
Expand Down
6 changes: 2 additions & 4 deletions src/Database/Seeders/BasePermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Models\Permission;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Contracts\Permission as PermissionContract;

abstract class BasePermissionSeeder extends Seeder
{
abstract protected function permissionsByGuard(): array;

public function run(): void
{
/** @var Permission $permissionClass */
$permissionClass = app(PermissionRegistrar::class)->getPermissionClass();
$permissionClass = app(PermissionContract::class);

collect($this->permissionsByGuard())
->map(fn (array $permissions) => collect($permissions))
Expand Down
33 changes: 19 additions & 14 deletions src/Database/Seeders/DefaultRoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Lloricode\FilamentSpatieLaravelPermissionPlugin\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Spatie\Permission\Commands\CreateRole;
use Spatie\Permission\Contracts\Permission as PermissionContract;
use Spatie\Permission\Contracts\Role as RoleContract;

class DefaultRoleSeeder extends Seeder
{
public function __construct(
protected readonly RoleContract $roleContract,
protected readonly PermissionContract $permissionContract,
) {}

Expand All @@ -22,23 +22,28 @@ public function __construct(
public function run(): void
{
$guard = Config::string('filament-permission.guard');

foreach (Config::array('filament-permission.roles') as $roleName) {
Artisan::call(CreateRole::class, [
'name' => $roleName,
'guard' => $guard,
'permissions' => config('filament-permission.roles.admin') === $roleName
? $this->permissionContract

$role = $this->roleContract->findOrCreate(
name: $roleName,
guardName: $guard,
);

if (config('filament-permission.roles.admin') === $roleName) {
$role->givePermissionTo(
$this->permissionContract
->where('guard_name', $guard)
->pluck('name')
->implode('|')
: null,
]);
);
}
}

foreach (Config::array('filament-permission.extra_roles') as $roleName) {
Artisan::call(CreateRole::class, [
'name' => $roleName,
'guard' => $guard,
]);
$this->roleContract->findOrCreate(
name: $roleName,
guardName: $guard,
);
}

}
Expand Down
30 changes: 0 additions & 30 deletions src/Models/Permission.php

This file was deleted.

3 changes: 2 additions & 1 deletion tests/Actions/EditRoleActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Actions\EditRoleAction;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Data\RoleData;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Contracts\Role as RoleContract;

use function PHPUnit\Framework\assertTrue;

Expand All @@ -19,7 +20,7 @@
));

/** @var array<int, Role&Model> $roles */
$roles = registrarRole()::whereIn('name', $roleNames)->get();
$roles = app(RoleContract::class)::whereIn('name', $roleNames)->get();

$action = app(EditRoleAction::class);

Expand Down
21 changes: 8 additions & 13 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

declare(strict_types=1);

use Illuminate\Support\Facades\Artisan;
use Illuminate\Database\Eloquent\Model;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Models\Role;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Tests\Fixture\UserFactory;
use Spatie\Permission\Commands\CreateRole;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\PermissionRegistrar;

use function Pest\Laravel\actingAs;

Expand All @@ -20,17 +18,14 @@ function loginAsSuperAdmin()
return $user;
}

function createRole(string $name, ?string $guard = null): RoleContract
function createRole(string $name, ?string $guard = null): RoleContract & Model
{
Artisan::call(CreateRole::class, [
'name' => $name,
'guard' => $guard ?? config('filament-permission.guard'),
]);

return registrarRole()::findByName($name);
}
/** @var RoleContract&Model $role */
$role = app(RoleContract::class)->findOrCreate(
name: $name,
guardName: $guard ?? config('filament-permission.guard'),
);

function registrarRole(): RoleContract
{
return app(app(PermissionRegistrar::class)->getRoleClass());
return $role;
}

0 comments on commit 9f24a05

Please sign in to comment.