Skip to content

Commit

Permalink
filament v3 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharinda Rodrigo committed Aug 2, 2023
1 parent 2f73634 commit 276d6fb
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 138 deletions.
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
}
],
"require": {
"php": "^7.4|^8.0|^8.1|^8.2",
"filament/filament": "^2.0|^2.1",
"illuminate/support": "^8|^9|^10",
"php": "^8.1|^8.2",
"filament/filament": "^3.0",
"illuminate/support": "^9|^10",
"spatie/laravel-permission": "^5.4"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"laravel/pint": "^1.10",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
Expand Down Expand Up @@ -52,5 +53,7 @@
"FilamentSpatieRolesPermissions": "Althinect\\FilamentSpatieRolesPermissions\\FilamentSpatieRolesPermissionsFacade"
}
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
16 changes: 7 additions & 9 deletions config/filament-spatie-roles-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'preload_roles' => true,

'preload_permissions' => false,
'preload_permissions' => true,

'navigation_section_group' => 'filament-spatie-roles-permissions::filament-spatie.section.roles_and_permissions', // Default uses language constant

Expand All @@ -15,24 +15,23 @@
*/
'should_register_on_navigation' => [
'permissions' => true,
'roles' => true
'roles' => true,
],

'guard_names' => [
'web' => 'web',
'api' => 'api'
'api' => 'api',
],

'default_guard_name' => null,


'model_filter_key' => 'return \'%\'.$key;', // Eg: 'return \'%\'.$key.'\%\';'

'generator' => [

'guard_names' => [
'web',
'api'
'api',
],

'permission_affixes' => [
Expand Down Expand Up @@ -77,7 +76,7 @@
* Include directories which consists of models.
*/
'model_directories' => [
app_path('Models')
app_path('Models'),
//app_path('Domains/Forum')
],

Expand All @@ -96,7 +95,7 @@
],

'excluded_policy_models' => [
\App\Models\User::class
\App\Models\User::class,
],

/*
Expand All @@ -106,9 +105,8 @@
//'view-log'
],


'user_model' => \App\Models\User::class,

'policies_namespace' => 'App\Policies'
'policies_namespace' => 'App\Policies',
],
];
3 changes: 2 additions & 1 deletion resources/lang/en/filament-spatie.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'field.name' => 'Name',
'field.permissions' => 'Permissions',
'field.roles' => 'Roles',
'field.role' => 'Role',
'field.team' => 'Team',

/*
Expand All @@ -26,5 +27,5 @@
'section.roles_and_permissions' => 'Roles and Permissions',
'select-team' => 'Select a Team',
'select-team-hint' => 'Leave blank for a global role',
'section.users' => 'Users'
'section.users' => 'Users',
];
24 changes: 12 additions & 12 deletions resources/lang/uk/filament-spatie.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

return [
/*
/*
|------------------------------------------------- -------------------------
| Fields
|------------------------------------------------- -------------------------
*/

'field.guard_name' => 'Аутентифікація (guard)',
'field.name' => "Ім'я",
'field.permissions' => 'Дозволи',
'field.roles' => 'Ролі',
/*
'field.guard_name' => 'Аутентифікація (guard)',
'field.name' => "Ім'я",
'field.permissions' => 'Дозволи',
'field.roles' => 'Ролі',
/*
|------------------------------------------------- -------------------------
| Labels
|------------------------------------------------- -------------------------
*/

'section.permission' => 'Дозвол',
'section.permissions' => 'Дозволи',
'section.role' => 'Роль',
'section.roles' => 'Ролі',
'section.roles_and_permissions' => 'Ролі та дозволи',
];
'section.permission' => 'Дозвол',
'section.permissions' => 'Дозволи',
'section.role' => 'Роль',
'section.roles' => 'Ролі',
'section.roles_and_permissions' => 'Ролі та дозволи',
];
50 changes: 23 additions & 27 deletions src/Commands/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Althinect\FilamentSpatieRolesPermissions\Commands;

use Althinect\FilamentSpatieRolesPermissions\Commands\Concerns\ManipulateFiles;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -52,7 +51,7 @@ public function handle(): void
$this->prepareCustomPermissions();

foreach ($this->permissions as $permission) {
$this->comment("Syncing Permission for: " . $permission['name']);
$this->comment('Syncing Permission for: '.$permission['name']);
PermissionModel::firstOrCreate($permission);
}
}
Expand Down Expand Up @@ -84,53 +83,53 @@ public function prepareClassPermissionsAndPolicies($classes): void
$modelName = $model->getShortName();

$stub = '/stubs/genericPolicy.stub';
$contents = $filesystem->get(__DIR__ . $stub);
$contents = $filesystem->get(__DIR__.$stub);

foreach ($this->permissionAffixes() as $key => $permissionAffix) {
foreach ($this->guardNames() as $guardName) {

$permission = eval($this->config['permission_name']);
$this->permissions[] = [
'name' => $permission,
'guard_name' => $guardName
'guard_name' => $guardName,
];

if ($this->option('policies')) {
$contents = Str::replace("{{ " . $key . " }}", $permission, $contents);
$contents = Str::replace('{{ '.$key.' }}', $permission, $contents);
}
}
}

if ($this->option('policies') || $this->option('yes-to-all')) {

$policyVariables = [
'class' => $modelName . 'Policy',
'class' => $modelName.'Policy',
'namespacedModel' => $model->getName(),
'namespacedUserModel' => (new ReflectionClass($this->config['user_model']))->getName(),
'namespace' => $this->config['policies_namespace'],
'user' => 'User',
'model' => $modelName,
'modelVariable' => $modelName == 'User' ? 'model' : Str::lower($modelName)
'modelVariable' => $modelName == 'User' ? 'model' : Str::lower($modelName),
];

foreach ($policyVariables as $search => $replace) {
if ($modelName == 'User' && $search == 'namespacedModel') {
$contents = Str::replace("use {{ namespacedModel }};", '', $contents);
$contents = Str::replace('use {{ namespacedModel }};', '', $contents);
} else {
$contents = Str::replace("{{ " . $search . " }}", $replace, $contents);
$contents = Str::replace('{{ '.$search.' }}', $replace, $contents);
}
}

if ($filesystem->exists(app_path('Policies/' . $modelName . 'Policy.php'))) {
if ($filesystem->exists(app_path('Policies/'.$modelName.'Policy.php'))) {
if ($this->option('oep')) {
$filesystem->put(app_path('Policies/' . $modelName . 'Policy.php'), $contents);
$this->comment('Overriding Existing Policy: ' . $modelName);
$filesystem->put(app_path('Policies/'.$modelName.'Policy.php'), $contents);
$this->comment('Overriding Existing Policy: '.$modelName);
} else {
$this->warn('Policy already exists for: ' . $modelName);
$this->warn('Policy already exists for: '.$modelName);
}
} else {
$filesystem->put(app_path('Policies/' . $modelName . 'Policy.php'), $contents);
$this->comment('Creating Policy: ' . $modelName);
$filesystem->put(app_path('Policies/'.$modelName.'Policy.php'), $contents);
$this->comment('Creating Policy: '.$modelName);
}
}
}
Expand All @@ -142,7 +141,7 @@ public function prepareCustomPermissions(): void
foreach ($this->guardNames() as $guardName) {
$this->permissions[] = [
'name' => $customPermission,
'guard_name' => $guardName
'guard_name' => $guardName,
];
}
}
Expand All @@ -160,12 +159,12 @@ public function getModels(): array

foreach ($resources as $resource) {
$resourceNameSpace = $this->extractNamespace($resource);
$reflection = new ReflectionClass($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension());
$reflection = new ReflectionClass($resourceNameSpace.'\\'.$resource->getFilenameWithoutExtension());
if (
!$reflection->isAbstract() &&
! $reflection->isAbstract() &&
$reflection->getParentClass()->getName() == 'Filament\Resources\Resource'
) {
$models[] = new ReflectionClass(app($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension())->getModel());
$models[] = new ReflectionClass(app($resourceNameSpace.'\\'.$resource->getFilenameWithoutExtension())->getModel());
}
}
}
Expand All @@ -177,7 +176,6 @@ public function getModels(): array
return $models;
}


/**
* @throws ReflectionException
*/
Expand All @@ -188,9 +186,9 @@ private function getClassesInDirectory($path): array

foreach ($files as $file) {
$namespace = $this->extractNamespace($file);
$class = $namespace . '\\' . $file->getFilenameWithoutExtension();
$class = $namespace.'\\'.$file->getFilenameWithoutExtension();
$model = new ReflectionClass($class);
if (!$model->isAbstract()) {
if (! $model->isAbstract()) {
$models[] = $model;
}

Expand Down Expand Up @@ -231,12 +229,11 @@ private function getModelReflections($array): array
}, $array);
}


private function extractNamespace($file)
{

$ns = NULL;
$handle = fopen($file, "r");
$ns = null;
$handle = fopen($file, 'r');
if ($handle) {
while (($line = fgets($handle)) !== false) {
if (str_starts_with($line, 'namespace')) {
Expand All @@ -247,6 +244,7 @@ private function extractNamespace($file)
}
fclose($handle);
}

return $ns;
}

Expand All @@ -257,6 +255,4 @@ public function getAllModels(): array

return array_merge($models, $customModels);
}


}
2 changes: 0 additions & 2 deletions src/Concerns/HasSuperAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Althinect\FilamentSpatieRolesPermissions\Concerns;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Spatie\Permission\Traits\HasRoles;

trait HasSuperAdmin
Expand Down
36 changes: 36 additions & 0 deletions src/FilamentSpatieRolesPermissionsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Althinect\FilamentSpatieRolesPermissions;

use Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource;
use Althinect\FilamentSpatieRolesPermissions\Resources\RoleResource;
use Filament\Contracts\Plugin;
use Filament\Panel;

class FilamentSpatieRolesPermissionsPlugin implements Plugin
{
public function getId(): string
{
return 'filament-spatie-roles-permissions';
}

public function register(Panel $panel): void
{
$panel
->resources([
RoleResource::class,
PermissionResource::class,
]);

}

public static function make(): static
{
return app(static::class);
}

public function boot(Panel $panel): void
{
//
}
}
15 changes: 2 additions & 13 deletions src/FilamentSpatieRolesPermissionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace Althinect\FilamentSpatieRolesPermissions;

use Althinect\FilamentSpatieRolesPermissions\Commands\Permission;
use Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource;
use Althinect\FilamentSpatieRolesPermissions\Resources\RoleResource;
use Filament\PluginServiceProvider;
use Illuminate\Support\ServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class FilamentSpatieRolesPermissionsServiceProvider extends PluginServiceProvider
class FilamentSpatieRolesPermissionsServiceProvider extends PackageServiceProvider
{
public static string $name = 'filament-spatie-roles-permissions';

Expand All @@ -21,12 +18,4 @@ public function configurePackage(Package $package): void
->hasTranslations()
->hasCommand(Permission::class);
}

protected function getResources(): array
{
return [
RoleResource::class,
PermissionResource::class
];
}
}
Loading

0 comments on commit 276d6fb

Please sign in to comment.