Skip to content

Commit

Permalink
Merge pull request #411 from bezhanSalleh/feature/handle-role-policy-…
Browse files Browse the repository at this point in the history
…auto-registration

handles role policy auto registration and resolves #407
  • Loading branch information
bezhanSalleh authored Sep 2, 2024
2 parents 032b1d5 + 22e1a44 commit 2124283
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 87 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest --no-coverage",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"finalize": "composer format && composer analyse && composer test"
},
"config": {
"sort-packages": true,
Expand Down
2 changes: 1 addition & 1 deletion config/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
],

'register_role_policy' => [
'enabled' => false,
'enabled' => true,
],

];
File renamed without changes.
79 changes: 0 additions & 79 deletions resources/lang/filament-shield_pt_PT.php

This file was deleted.

File renamed without changes.
7 changes: 2 additions & 5 deletions src/Commands/Concerns/CanGeneratePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ protected function generatePolicyPath(array $entity): string
{
$path = (new \ReflectionClass($entity['fqcn']::getModel()))->getFileName();

$policyPath = Str::of(config('filament-shield.generator.policy_directory', 'Policies'))
->replace('\\', DIRECTORY_SEPARATOR);

if (Str::of($path)->contains(['vendor', 'src'])) {
$basePolicyPath = app_path(
(string) Str::of($entity['model'])
->prepend($policyPath->append('\\'))
->prepend(str(Utils::getPolicyPath())->append('\\'))
->replace('\\', DIRECTORY_SEPARATOR),
);

Expand All @@ -37,7 +34,7 @@ protected function generatePolicyPath(array $entity): string

/** @phpstan-ignore-next-line */
$basePath = Str::of($path)
->replace('Models', $policyPath)
->replace('Models', Utils::getPolicyPath())
->replaceLast('.php', 'Policy.php')
->replace('\\', DIRECTORY_SEPARATOR);

Expand Down
16 changes: 15 additions & 1 deletion src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function getExcludedWidgets(): array

public static function isRolePolicyRegistered(): bool
{
return (bool) config('filament-shield.register_role_policy.enabled', true);
return static::isRolePolicyGenerated() && config('filament-shield.register_role_policy.enabled', false);
}

public static function doesResourceHaveCustomPermissions(string $resourceClass): bool
Expand Down Expand Up @@ -242,4 +242,18 @@ public static function discoverAllPages(): bool
{
return config('filament-shield.discovery.discover_all_pages', false);
}

public static function getPolicyPath(): string
{
return Str::of(config('filament-shield.generator.policy_directory', 'Policies'))
->replace('\\', DIRECTORY_SEPARATOR)
->toString();
}

protected static function isRolePolicyGenerated(): bool
{
$filesystem = new Filesystem;

return (bool) $filesystem->exists(app_path(static::getPolicyPath() . DIRECTORY_SEPARATOR . 'RolePolicy.php'));
}
}

0 comments on commit 2124283

Please sign in to comment.