Skip to content

Commit

Permalink
wip
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 25, 2024
1 parent 72d0248 commit f0b8bed
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 96 deletions.
27 changes: 0 additions & 27 deletions src/Actions/CreateRoleAction.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/Actions/EditRoleAction.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Data/RoleData.php

This file was deleted.

14 changes: 10 additions & 4 deletions src/Resources/RoleResource/Pages/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Actions\CreateRoleAction;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Data\RoleData;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Resources\RoleResource;
use Spatie\Permission\Contracts\Role as RoleContract;

class CreateRole extends CreateRecord
{
Expand All @@ -17,7 +16,14 @@ class CreateRole extends CreateRecord
#[\Override]
protected function handleRecordCreation(array $data): Model
{
return app(CreateRoleAction::class)
->execute(new RoleData(...$data));
/** @var RoleContract&Model $role */
$role = app(RoleContract::class)->findOrCreate(
name: $data['name'],
guardName: $data['guard_name'],
);

$role->givePermissionTo($data['permissions']);

return $role;
}
}
32 changes: 27 additions & 5 deletions src/Resources/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Actions\EditRoleAction;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Data\RoleData;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Config\PermissionConfig;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Resources\RoleResource;
use Spatie\Permission\Contracts\Role as RoleContract;

Expand All @@ -28,11 +27,34 @@ protected function getHeaderActions(): array
];
}

/**
* @param RoleContract&Model $record
* @return RoleContract&Model
*/
#[\Override]
protected function handleRecordUpdate(Model $record, array $data): Model
{
/** @var RoleContract&Model $record */
return app(EditRoleAction::class)
->execute($record, new RoleData(...$data));
$roleNames = PermissionConfig::roleNamesByGuardName($data['guard_name'] ?? null);

$isExtraRole = in_array($record->name, $roleNames, true);

if ($record->name !== $data['name']) {

if ($isExtraRole) {
abort(400, trans('Cannot update role name of this role.'));
}

}

if (! $isExtraRole) {
$record->update([
'name' => $data['name'],
'guard_name' => $data['guard_name'],
]);
}

$record->syncPermissions($data['permissions']);

return $record;
}
}

0 comments on commit f0b8bed

Please sign in to comment.