Skip to content

Commit

Permalink
Merge pull request #336 from bezhanSalleh/feature/simple-view
Browse files Browse the repository at this point in the history
Feature/simple view
  • Loading branch information
bezhanSalleh authored Mar 28, 2024
2 parents 9d997dd + 7c9dfcf commit 71dc9cd
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 64 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [10.*]
php: [8.3, 8.2, 8.1]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 11.*
testbench: 9.*
- laravel: 10.*
testbench: 8.*
exclude:
- laravel: 11.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"pest_2.33.1","defects":[],"times":{"P\\Tests\\DatabaseTest::__pest_evaluable_it_can_check_if_package_testing_is_configured":0,"P\\Tests\\DatabaseTest::__pest_evaluable_it_can_check_if_the_permission_name_can_be_configured_using_the_closure":0.005}}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0",
"nunomaduro/collision": "^7.0|^8.0",
"larastan/larastan": "^2.0",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.10",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-laravel": "^2.3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
Expand Down
5 changes: 2 additions & 3 deletions config/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'is_globally_searchable' => false,
'show_model_path' => true,
'is_scoped_to_tenant' => true,
'cluster' => env('FILAMENT_SHIELD_CLUSTER', null),
'cluster' => null,
],

'auth_provider_model' => [
Expand Down Expand Up @@ -59,8 +59,7 @@
'generator' => [
'option' => 'policies_and_permissions',
'policy_directory' => 'Policies',
// The namespace of the generated policy file will be App\Policies
'namespace' => 'Policies',
'policy_namespace' => 'Policies',
],

'exclude' => [
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Concerns/CanGeneratePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ protected function generatePolicyStubVariables(array $entity): array
$path = $reflectionClass->getFileName();

$stubVariables['namespace'] = Str::of($path)->contains(['vendor', 'src'])
? 'App\\' . Utils::getGeneratorNamespace()
: Str::of($namespace)->replace('Models', Utils::getGeneratorNamespace()); /** @phpstan-ignore-line */
? 'App\\' . Utils::getPolicyNamespace()
: Str::of($namespace)->replace('Models', Utils::getPolicyNamespace()); /** @phpstan-ignore-line */
$stubVariables['model_name'] = $entity['model'];
$stubVariables['model_fqcn'] = $namespace . '\\' . $entity['model'];
$stubVariables['model_variable'] = Str::of($entity['model'])->camel();
Expand Down
22 changes: 22 additions & 0 deletions src/Concerns/CanLocalizePermissionLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace BezhanSalleh\FilamentShield\Concerns;

trait CanLocalizePermissionLabels
{
protected bool $arePermissionLabelsLocalized = true;

public function localizePermissionLabels(bool $condition = true): static
{
$this->arePermissionLabelsLocalized = $condition;

return $this;
}

public function hasLocalizedPermissionLabels(): bool
{
return $this->arePermissionLabelsLocalized;
}
}
22 changes: 22 additions & 0 deletions src/Concerns/HasSimpleResourcePermissionView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace BezhanSalleh\FilamentShield\Concerns;

trait HasSimpleResourcePermissionView
{
protected bool $isSimple = false;

public function simpleResourcePermissionView(bool $condition = true): static
{
$this->isSimple = $condition;

return $this;
}

public function hasSimpleResourcePermissionView(): bool
{
return $this->isSimple;
}
}
12 changes: 8 additions & 4 deletions src/Facades/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static static configurePermissionIdentifierUsing(\Closure $callback)
* @method static \BezhanSalleh\FilamentShield\FilamentShield configurePermissionIdentifierUsing(\Closure $callback)
* @method static string getPermissionIdentifier(string $resource)
* @method static void generateForResource(array $entity)
* @method static void generateForPage(string $page)
* @method static void generateForWidget(string $widget)
* @method static null|array getResources()
* @method static null|array getPages()
* @method static null|array getWidgets()
* @method static void createRole(string|null $name = null)
* @method static array|null getResources()
* @method static string getLocalizedResourceLabel(string $entity)
* @method static string getLocalizedResourcePermissionLabel(string $permission)
* @method static array|null getPages()
* @method static string getLocalizedPageLabel(string $page)
* @method static array|null getWidgets()
* @method static string getLocalizedWidgetLabel(string $widget)
* @method static array getAllResourcePermissions()
* @method static \Illuminate\Support\Collection|null getCustomPermissions()
* @method static mixed evaluate(mixed $value, array $namedInjections = [], array $typedInjections = [])
*
* @see \BezhanSalleh\FilamentShield\FilamentShield
*/
Expand Down
67 changes: 65 additions & 2 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Widgets\Widget;
use Filament\Widgets\WidgetConfiguration;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Spatie\Permission\PermissionRegistrar;
Expand All @@ -20,6 +21,8 @@ class FilamentShield

protected ?Closure $configurePermissionIdentifierUsing = null;

public ?Collection $customPermissions = null;

public function configurePermissionIdentifierUsing(Closure $callback): static
{
$this->configurePermissionIdentifierUsing = $callback;
Expand Down Expand Up @@ -139,7 +142,7 @@ public function getResources(): ?array
return [
$name => [
'resource' => "{$name}",
'model' => Str::of($resource::getModel())->afterLast('\\'),
'model' => str($resource::getModel())->afterLast('\\')->toString(),
'fqcn' => $resource,
],
];
Expand All @@ -165,7 +168,7 @@ public static function getLocalizedResourceLabel(string $entity): string
return $resource === $entity;
})->first()::getModelLabel();

return Str::of($label)->headline();
return str($label)->headline()->toString();
}

/**
Expand Down Expand Up @@ -309,4 +312,64 @@ protected static function getWidgetInstanceFromWidgetConfiguration(string | Widg
? $widget->widget
: $widget;
}

public function getAllResourcePermissions(): array
{
return collect($this->getResources())
->map(function ($resourceEntity) {
return collect(
Utils::getResourcePermissionPrefixes($resourceEntity['fqcn'])
)
->flatMap(function ($permission) use ($resourceEntity) {
$name = $permission . '_' . $resourceEntity['resource'];
$permissionLabel = FilamentShieldPlugin::get()->hasLocalizedPermissionLabels()
? str(static::getLocalizedResourcePermissionLabel($permission))
->prepend(
str($resourceEntity['fqcn']::getPluralModelLabel())
->plural()
->title()
->append(' - ')
->toString()
)
->toString()
: $name;
$resourceLabel = FilamentShieldPlugin::get()->hasLocalizedPermissionLabels()
? static::getLocalizedResourceLabel($resourceEntity['fqcn'])
: $resourceEntity['model'];

return [
$name => $permissionLabel,
];
})
->toArray();
})
->sortKeys()
->collapse()
->toArray();
}

public function getCustomPermissions(): ?Collection
{

if (blank($this->customPermissions)) {
$query = Utils::getPermissionModel()::query();
$this->customPermissions = $query
->select('name')
->whereNotIn(DB::raw('lower(name)'), $this->getEntitiesPermissions())
->pluck('name');
}

return $this->customPermissions;
}

protected function getEntitiesPermissions(): ?array
{
return collect($this->getAllResourcePermissions())->keys()
->merge(collect($this->getPages())->map->permission->keys())
->merge(collect($this->getWidgets())->map->permission->keys())
->map(fn ($permission) => str($permission)->lower()->toString())
->values()
->unique()
->toArray();
}
}
2 changes: 2 additions & 0 deletions src/FilamentShieldPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class FilamentShieldPlugin implements Plugin
{
use Concerns\CanCustomizeColumns;
use Concerns\CanLocalizePermissionLabels;
use Concerns\HasSimpleResourcePermissionView;

public static function make(): static
{
Expand Down
Loading

0 comments on commit 71dc9cd

Please sign in to comment.