Skip to content

Commit

Permalink
make destructive commands prohibitable
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Nov 9, 2024
1 parent 92c527b commit 5e2fdec
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 16 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,22 @@ public function panel(Panel $panel): Panel
<img width="1161" alt="Screenshot 2023-09-24 at 10 34 31 PM" src="https://github.com/bezhanSalleh/filament-shield/assets/10007504/be42bab2-72d1-4db0-8de4-8b8fba2d4e68">

## Available Commands

### Prohibited Commands
Since almost all shield commands are destructive and can cause data loss, they can be prohibited by calling the prohibit method of the command as following in a service provider's `boot()` method:
```php
use BezhanSalleh\FilamentShield\FilamentShield;
use BezhanSalleh\FilamentShield\Commands;
public function boot(): void
{
// individually prohibit commands
Commands\SetupCommand::prohibit($this->app->isProduction());
Commands\InstallCommand::prohibit($this->app->isProduction());
Commands\GenerateCommand::prohibit($this->app->isProduction());
Commands\PublishCommand::prohibit($this->app->isProduction());
// or prohibit the above commands all at once
FilamentShield::prohibitDestructiveCommands($this->app->isProduction())
}
```
### Core Commands
```bash
# Setup Shield
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class GenerateCommand extends Command

public function handle(): int
{
if ($this->isProhibited()) {
return Command::FAILURE;
}

$panel = $this->option('panel')
? $this->option('panel')
: Select(
Expand All @@ -83,7 +87,7 @@ public function handle(): int
$this->components->error('No entites provided for the generators ...');
$this->components->alert('Generation skipped');

return self::INVALID;
return Command::INVALID;
}

if (filled($this->option('resource')) || $this->option('all')) {
Expand All @@ -106,7 +110,7 @@ public function handle(): int
Cache::forget('shield_general_exclude');
}

return self::SUCCESS;
return Command::SUCCESS;
}

protected function determinGeneratorOptionAndEntities(): void
Expand Down
12 changes: 8 additions & 4 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class InstallCommand extends Command implements PromptsForMissingInput

public function handle(): int
{
if ($this->isProhibited()) {
return Command::FAILURE;
}

$shouldSetPanelAsCentralApp = false;

$panel = Filament::getPanel($this->argument('panel') ?? null);
Expand All @@ -49,17 +53,17 @@ public function handle(): int
if (! $this->fileExists($panelPath)) {
$this->error("Panel not found: {$panelPath}");

return static::FAILURE;
return Command::FAILURE;
}

// if ($panel->hasTenancy() && $shouldSetPanelAsCentralApp) {
// $this->components->warn('Cannot install Shield as `Central App` on a tenant panel!');
// return static::FAILURE;
// return Command::FAILURE;
// }

// if (! $panel->hasTenancy() && $shouldSetPanelAsCentralApp && blank($tenant)) {
// $this->components->warn('Make sure you have at least a panel with tenancy setup first!');
// return static::INVALID;
// return Command::INVALID;
// }

$this->registerPlugin(
Expand Down Expand Up @@ -89,6 +93,6 @@ public function handle(): int

$this->components->info('Shield has been successfully configured & installed!');

return static::SUCCESS;
return Command::SUCCESS;
}
}
9 changes: 7 additions & 2 deletions src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
#[AsCommand(name: 'shield:publish', description: "Publish Shield's Resource.")]
class PublishCommand extends Command
{
use Concerns\CanBeProhibitable;
use Concerns\CanManipulateFiles;

protected $signature = 'shield:publish {panel}';

public function handle(Filesystem $filesystem): int
{
if ($this->isProhibited()) {
return Command::FAILURE;
}

Filament::setCurrentPanel(Filament::getPanel($this->argument('panel')));

$panel = Filament::getCurrentPanel();
Expand All @@ -46,7 +51,7 @@ public function handle(Filesystem $filesystem): int
if ($this->checkForCollision([$roleResourcePath])) {
$confirmed = confirm('Shield Resource already exists. Overwrite?');
if (! $confirmed) {
return self::INVALID;
return Command::INVALID;
}
}

Expand All @@ -63,6 +68,6 @@ public function handle(Filesystem $filesystem): int

$this->components->info("Shield's Resource have been published successfully!");

return self::SUCCESS;
return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'shield:seeder')]
class MakeShieldSeederCommand extends Command
class SeederCommand extends Command
{
use Concerns\CanManipulateFiles;

Expand Down
10 changes: 7 additions & 3 deletions src/Commands/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ class SetupCommand extends Command

public function handle(): int
{
if ($this->isProhibited()) {
return Command::FAILURE;
}

if (! Utils::isAuthProviderConfigured()) {
$this->components->error('Please make sure your Auth Provider model (\App\\Models\\User) uses either `HasRoles` or `HasFilamentShield` trait');

return self::INVALID;
return Command::INVALID;
}

if ($this->option('minimal')) {
Expand All @@ -57,7 +61,7 @@ public function handle(): int
$this->install(true);
}

return self::INVALID;
return Command::INVALID;
}

if ($confirmed) {
Expand All @@ -82,7 +86,7 @@ public function handle(): int
}
}

return self::SUCCESS;
return Command::SUCCESS;
}

protected function CheckIfAlreadyInstalled(): bool
Expand Down
5 changes: 3 additions & 2 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ protected function getEntitiesPermissions(): ?array
*/
public static function prohibitDestructiveCommands(bool $prohibit = true)
{
Commands\SetupCommand::prohibit($prohibit);
Commands\InstallCommand::prohibit($prohibit);
Commands\GenerateCommand::prohibit($prohibit);
Commands\InstallCommand::prohibit($prohibit);
Commands\PublishCommand::prohibit($prohibit);
Commands\SetupCommand::prohibit($prohibit);
}
}
2 changes: 1 addition & 1 deletion src/FilamentShieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ protected function getCommands(): array
Commands\GenerateCommand::class,
Commands\InstallCommand::class,
Commands\PublishCommand::class,
Commands\SeederCommand::class,
Commands\SetupCommand::class,
Commands\SuperAdminCommand::class,
Commands\MakeShieldSeederCommand::class,
];
}
}

0 comments on commit 5e2fdec

Please sign in to comment.