PHP code example of bezhansalleh / filament-shield
1. Go to this page and download the library: Download bezhansalleh/filament-shield library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Filament\Pages;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
class MyPage extends Page
{
use HasPageShield;
...
}
namespace App\Filament\Widgets;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
class IncomeWidget extends LineChartWidget
{
use HasWidgetShield;
...
}
use BezhanSalleh\FilamentShield\Facades\FilamentShield;
use BezhanSalleh\FilamentShield\Commands;
public function boot(): void
{
// individually prohibit commands
Commands\GenerateCommand::prohibit($this->app->isProduction());
Commands\InstallCommand::prohibit($this->app->isProduction());
Commands\PublishCommand::prohibit($this->app->isProduction());
Commands\SetupCommand::prohibit($this->app->isProduction());
Commands\SeederCommand::prohibit($this->app->isProduction());
Commands\SuperAdminCommand::prohibit($this->app->isProduction());
// or prohibit the above commands all at once
FilamentShield::prohibitDestructiveCommands($this->app->isProduction());
}
'localization' => [
'enabled' => false,
'key' => 'shield-permissions', // could be any name you want
],
use BezhanSalleh\FilamentShield\Facades\FilamentShield;
use Filament\Pages\BasePage as Page;
use Filament\Resources\Resource;
use Filament\Widgets\Widget;
use Illuminate\Support\Str;
//...
public function boot(): void
{
FilamentShield::buildPermissionKeyUsing(
function (string $entity, string $affix, string $subject, string $case, string $separator) {
return match(true) {
# if `configurePermissionIdentifierUsing()` was used previously, then this needs to be adjusted accordingly
is_subclass_of($entity, Resource::class) => Str::of($affix)
->snake()
->append('_')
->append(
Str::of($entity)
->afterLast('\\')
->beforeLast('Resource')
->replace('\\', '')
->snake()
->replace('_', '::')
)
->toString(),
is_subclass_of($entity, Page::class) => Str::of('page_')
->append(class_basename($entity))
->toString(),
is_subclass_of($entity, Widget::class) => Str::of('widget_')
->append(class_basename($entity))
->toString()
};
});
}
bash
php artisan shield:translation en --panel=admin
bash
php artisan vendor:publish --tag="filament-shield-config"
php artisan shield:publish --panel=admin # you can ignore this if you didn't published the resource previously