Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use config models #139

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Commands/MakeShieldSeederCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace BezhanSalleh\FilamentShield\Commands;

use BezhanSalleh\FilamentShield\Support\Utils;
use Illuminate\Console\Command;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'shield:seeder')]
Expand Down Expand Up @@ -43,7 +42,7 @@ public function handle(): int
]);
}

if (Role::doesntExist() && Permission::doesntExist()) {
if (Utils::getRoleModel()::doesntExist() && Utils::getPermissionModel()::doesntExist()) {
$this->warn(' There are no roles or permissions to create the seeder. Please first run `shield:generate --all`');

return static::INVALID;
Expand All @@ -53,8 +52,8 @@ public function handle(): int
$permissionsViaRoles = collect();
$directPermissions = collect();

if (Role::exists()) {
$permissionsViaRoles = collect(Role::with('permissions')->get())
if (Utils::getRoleModel()::exists()) {
$permissionsViaRoles = collect(Utils::getRoleModel()::with('permissions')->get())
->map(function ($role) use ($directPermissionNames) {
$rolePermissions = $role->permissions
->pluck('name')
Expand All @@ -70,8 +69,8 @@ public function handle(): int
});
}

if (Permission::exists()) {
$directPermissions = collect(Permission::get())
if (Utils::getPermissionModel()::exists()) {
$directPermissions = collect(Utils::getPermissionModel()::get())
->filter(fn ($permission) => ! in_array($permission->name, $directPermissionNames->unique()->flatten()->all()))
->map(fn ($permission) => [
'name' => $permission->name,
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/MakeShieldSuperAdminCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Filament\Facades\Filament;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

class MakeShieldSuperAdminCommand extends Command
{
Expand All @@ -29,11 +28,11 @@ public function handle(): int
/** @var EloquentUserProvider $userProvider */
$userProvider = $auth->getProvider();

if (Role::whereName(Utils::getSuperAdminName())->doesntExist()) {
if (Utils::getRoleModel()::whereName(Utils::getSuperAdminName())->doesntExist()) {
FilamentShield::createRole();
}

if (Utils::isFilamentUserRoleEnabled() && Role::whereName(Utils::getFilamentUserRoleName())->doesntExist()) {
if (Utils::isFilamentUserRoleEnabled() && Utils::getRoleModel()::whereName(Utils::getFilamentUserRoleName())->doesntExist()) {
FilamentShield::createRole(isSuperAdmin: false);
}

Expand Down
16 changes: 7 additions & 9 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class FilamentShield
Expand All @@ -23,7 +21,7 @@ public static function generateForResource(array $entity): void
$permissions = collect();
collect($permissionPrefixes)
->each(function ($prefix) use ($resourceName, $permissions) {
$permissions->push(Permission::firstOrCreate(
$permissions->push(Utils::getPermissionModel()::firstOrCreate(
['name' => $prefix.'_'.$resourceName],
['guard_name' => Utils::getFilamentAuthGuard()]
));
Expand All @@ -36,7 +34,7 @@ public static function generateForResource(array $entity): void
public static function generateForPage(string $page): void
{
if (Utils::isPageEntityEnabled()) {
$permission = Permission::firstOrCreate(
$permission = Utils::getPermissionModel()::firstOrCreate(
['name' => $page],
['guard_name' => Utils::getFilamentAuthGuard()]
)->name;
Expand All @@ -48,7 +46,7 @@ public static function generateForPage(string $page): void
public static function generateForWidget(string $widget): void
{
if (Utils::isWidgetEntityEnabled()) {
$permission = Permission::firstOrCreate(
$permission = Utils::getPermissionModel()::firstOrCreate(
['name' => $widget],
['guard_name' => Utils::getFilamentAuthGuard()]
)->name;
Expand All @@ -68,9 +66,9 @@ protected static function giveSuperAdminPermission(string|array|Collection $perm
}
}

public static function createRole(bool $isSuperAdmin = true): Role
public static function createRole(bool $isSuperAdmin = true)
{
return Role::firstOrCreate(
return Utils::getRoleModel()::firstOrCreate(
['name' => $isSuperAdmin ? Utils::getSuperAdminName() : Utils::getFilamentUserRoleName()],
['guard_name' => $isSuperAdmin ? Utils::getFilamentAuthGuard() : Utils::getFilamentAuthGuard()]
);
Expand Down Expand Up @@ -243,8 +241,8 @@ protected static function transformClassString(string $string, bool $isPageClass
->first(fn ($item) => Str::endsWith(
$item,
Str::of($string)
->after('_')
->studly()
->after('_')
->studly()
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/FilamentShieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function packageBooted(): void
}

if (Utils::isRolePolicyRegistered()) {
Gate::policy('Spatie\Permission\Models\Role', 'App\Policies\RolePolicy');
Gate::policy(Utils::getRoleModel(), 'App\Policies\RolePolicy');
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RoleResource extends Resource implements HasShieldPermissions
{
protected static ?string $model = Role::class;

protected static ?string $recordTitleAttribute = 'name';

protected static $permissionsCollection;
Expand Down Expand Up @@ -184,6 +180,11 @@ public static function getPages(): array
];
}

public static function getModel(): string
{
return Utils::getRoleModel();
}

public static function getModelLabel(): string
{
return __('filament-shield::filament-shield.resource.label.role');
Expand Down Expand Up @@ -224,7 +225,7 @@ public static function getSlug(): string
protected static function getNavigationBadge(): ?string
{
return Utils::isResourceNavigationBadgeEnabled()
? static::$model::count()
? static::getModel()::count()
: null;
}

Expand All @@ -240,7 +241,7 @@ public static function canGloballySearch(): bool
public static function getResourceEntitiesSchema(): ?array
{
if (blank(static::$permissionsCollection)) {
static::$permissionsCollection = Permission::all();
static::$permissionsCollection = Utils::getPermissionModel()::all();
}

return collect(FilamentShield::getResources())->sortKeys()->reduce(function ($entities, $entity) {
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/RoleResource/Pages/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace BezhanSalleh\FilamentShield\Resources\RoleResource\Pages;

use BezhanSalleh\FilamentShield\Resources\RoleResource;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;

class CreateRole extends CreateRecord
{
Expand All @@ -28,7 +28,7 @@ protected function afterCreate(): void
{
$permissionModels = collect();
$this->permissions->each(function ($permission) use ($permissionModels) {
$permissionModels->push(Permission::firstOrCreate(
$permissionModels->push(Utils::getPermissionModel()::firstOrCreate(
/** @phpstan-ignore-next-line */
['name' => $permission],
['guard_name' => $this->data['guard_name']]
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace BezhanSalleh\FilamentShield\Resources\RoleResource\Pages;

use BezhanSalleh\FilamentShield\Resources\RoleResource;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;

class EditRole extends EditRecord
{
Expand Down Expand Up @@ -36,7 +36,7 @@ protected function afterSave(): void
{
$permissionModels = collect();
$this->permissions->each(function ($permission) use ($permissionModels) {
$permissionModels->push(Permission::firstOrCreate(
$permissionModels->push(Utils::getPermissionModel()::firstOrCreate(
['name' => $permission],
['guard_name' => $this->data['guard_name']]
));
Expand Down
10 changes: 10 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,14 @@ public static function getResourcePermissionPrefixes(string $resourceFQCN): arra
? $resourceFQCN::getPermissionPrefixes()
: static::getGeneralResourcePermissionPrefixes();
}

public static function getRoleModel(): string
{
return config('permission.models.role');
}

public static function getPermissionModel(): string
{
return config('permission.models.permission');
}
}