Skip to content

Commit

Permalink
configurable role policy, allow edit admin role
Browse files Browse the repository at this point in the history
Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode committed Sep 3, 2024
1 parent fe4f47b commit 6daa2fe
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
8 changes: 6 additions & 2 deletions config/filament-permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*/
'seeders' => [

'roles' => \Lloricode\FilamentSpatieLaravelPermissionPlugin\Database\Seeders\DefaultRoleSeeder::class,
'roles' => Lloricode\FilamentSpatieLaravelPermissionPlugin\Database\Seeders\DefaultRoleSeeder::class,

/**
* All permissions are generated base on your your setup.
Expand All @@ -62,7 +62,11 @@
* - filament widgets that's implements `\Lloricode\FilamentSpatieLaravelPermissionPlugin\Contracts\HasPermissionWidgets`.
* - from this config key `custom_permission_names`
*/
'permissions' => \Lloricode\FilamentSpatieLaravelPermissionPlugin\Database\Seeders\DefaultPermissionSeeder::class,
'permissions' => Lloricode\FilamentSpatieLaravelPermissionPlugin\Database\Seeders\DefaultPermissionSeeder::class,
],

'model_policies' => [
'role' => Lloricode\FilamentSpatieLaravelPermissionPlugin\Policies\RolePolicy::class,
],

'translated' => false,
Expand Down
8 changes: 8 additions & 0 deletions src/Config/PermissionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public static function translated(): bool
{
return Config::boolean('filament-permission.translated', false);
}

/**
* @return class-string|null
*/
public static function rolePolicy(): ?string
{
return config('filament-permission.model_policies.role');
}
}
2 changes: 1 addition & 1 deletion src/FilamentPermissionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function make(): static
public static function get(): static
{
/** @var static $plugin */
$plugin = filament(app(static::class)->getId());
$plugin = filament(self::make()->getId());

return $plugin;
}
Expand Down
10 changes: 7 additions & 3 deletions src/FilamentSpatieLaravelPermissionPluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Gate;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Commands\PermissionSyncCommand;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Config\PermissionConfig;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Contracts\HasPermissionUser;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Policies\RolePolicy;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -26,13 +26,16 @@ public function configurePackage(Package $package): void
public function packageRegistered(): void
{
$this->booting(function () {
Gate::policy(config('permission.models.role'), RolePolicy::class);
$rolePolicy = PermissionConfig::rolePolicy();

if ($rolePolicy !== null) {
Gate::policy(config('permission.models.role'), $rolePolicy);
}
});
}

public function packageBooted(): void
{

Gate::after(function (Authenticatable $user) {

if ($user instanceof HasPermissionUser && $user->isSuperAdmin()) {
Expand All @@ -42,5 +45,6 @@ public function packageBooted(): void

return null;
});

}
}
2 changes: 1 addition & 1 deletion src/Policies/RolePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function create(User $user): bool

public function update(User&HasPermissionUser $user, RoleContract $role): bool
{
if (! $user->isSuperAdmin() && $role->name === PermissionConfig::admin($role->guard_name)) {
if (! $user->isSuperAdmin() && $role->name === PermissionConfig::superAdmin($role->guard_name)) {
return false;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ public static function table(Table $table): Table

Tables\Actions\ActionGroup::make([
Tables\Actions\DeleteAction::make()
->disabled(fn (RoleContract $record): bool => $record->users->isNotEmpty()),
// ->disabled(fn (RoleContract $record): bool => $record->users->isNotEmpty())
->tooltip(
fn (RoleContract $record): ?string => $record->users->isNotEmpty()

Check failure on line 138 in src/Resources/RoleResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Anonymous function should return string|null but returns array|string|null.
? trans('This role has users.')
: null
),
]),
])
->defaultSort('updated_at', 'desc');
Expand Down

0 comments on commit 6daa2fe

Please sign in to comment.