Skip to content

Commit

Permalink
add filament-shield integration
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Oct 2, 2024
1 parent 3336590 commit 8352ef5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,54 @@ use TomatoPHP\FilamentCms\Services\FilamentCMSFormBuilder;
FilamentCMSFormBuilder::make('xvssd')->send($data)
```

## Use Filament Shield

you can use the shield to protect your resource and allow user roles by install it first

```bash
composer require bezhansalleh/filament-shield
```

Add the Spatie\Permission\Traits\HasRoles trait to your User model(s):

```php
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use HasRoles;

// ...
}
```
Publish the config file then setup your configuration:

```php
->plugin(\BezhanSalleh\FilamentShield\FilamentShieldPlugin::make())
```

Now run the following command to install shield:

```bash
php artisan shield:install
```

Now we can [publish the package assets]([https://github.com/bezhanSalleh/filament-shield](https://github.com/tomatophp/filament-users?tab=readme-ov-file#publish-assets)).

```bash
php artisan vendor:publish --tag="filament-users-config"
```

now you need to allow it on the plugin options

```php
->plugin(\TomatoPHP\FilamentCms\FilamentCMSPlugin::make()->allowShield())
```

for more information check the [Filament Shield](https://github.com/bezhanSalleh/filament-shield)


## Publish Assets

you can publish config file by use this command
Expand Down
2 changes: 2 additions & 0 deletions src/Filament/Pages/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
use Illuminate\Support\Facades\File;
use Filament\Pages\Actions\ButtonAction;
use Nwidart\Modules\Facades\Module;
use TomatoPHP\FilamentCms\Filament\Pages\Traits\HasShield;
use TomatoPHP\FilamentCms\Models\Theme;
use TomatoPHP\FilamentCms\Settings\ThemesSettings;

class Themes extends Page implements HasTable
{
use InteractsWithTable;
use HasShield;

protected static ?string $navigationIcon = 'heroicon-o-swatch';

Expand Down
75 changes: 75 additions & 0 deletions src/Filament/Pages/Traits/HasShield.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace TomatoPHP\FilamentCms\Filament\Pages\Traits;

use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Facades\Filament;
use Illuminate\Support\Str;

trait HasShield
{
public function booted(): void
{
if(filament('filament-cms')->isShieldAllowed()){
$this->beforeBooted();

if (! static::canAccess()) {

Notification::make()
->title(__('filament-shield::filament-shield.forbidden'))
->warning()
->send();

$this->beforeShieldRedirects();

redirect($this->getShieldRedirectPath());

return;
}

if (method_exists(parent::class, 'booted')) {
parent::booted();
}

$this->afterBooted();
}
}

protected function beforeBooted(): void
{
}

protected function afterBooted(): void
{
}

protected function beforeShieldRedirects(): void
{
}

protected function getShieldRedirectPath(): string
{
return Filament::getUrl();
}

protected static function getPermissionName(): string
{
return Str::of(class_basename(static::class))
->prepend(
Str::of(Utils::getPagePermissionPrefix())
->append('_')
->toString()
)
->toString();
}

public static function canAccess(): bool
{
if(filament('filament-cms')->isShieldAllowed()){
return Filament::auth()->user()->can(static::getPermissionName());
}
else {
return true;
}
}
}
12 changes: 12 additions & 0 deletions src/FilamentCMSPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FilamentCMSPlugin implements Plugin
public static bool $useThemeManager = false;
public static bool $usePageBuilder = false;
public static bool $useFormBuilder = false;
public static bool $allowShield = false;
// public static bool $useTicketingSystem = false;
public static array $defaultLocales = ['ar', 'en'];

Expand Down Expand Up @@ -88,6 +89,17 @@ public function register(Panel $panel): void
}
}

public function allowShield(bool $allowShield = true): static
{
self::$allowShield = $allowShield;
return $this;
}

public function isShieldAllowed(): bool
{
return self::$allowShield;
}

public function useFormBuilder(bool $useFormBuilder = true): static
{
self::$useFormBuilder = $useFormBuilder;
Expand Down

0 comments on commit 8352ef5

Please sign in to comment.