diff --git a/README.md b/README.md index 89d1b0c..420a16d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,48 @@ $panel->plugin(CommandRunnerPlugin::make()); ## Customizations -1. Command Validation +### 1. Authorization + +You can restrict access to this plugin using the `authorize` method. This accepts a boolean or a closure that returns a boolean. + +```php +CommandRunnerPlugin::make()->authorize(fn () => auth()->user()->can('view-command-runner')) +``` + +### 2. Navigation Group + +You can change the navigation group using the `navigationGroup` method. This accepts a string, `UnitEnum`. + +```php +CommandRunnerPlugin::make()->navigationGroup('System') +``` + +### 3. Navigation Label + +You can customize the navigation label using the `navigationLabel` method. + +```php +CommandRunnerPlugin::make()->navigationLabel('Command Runner') +``` + +### 4. Navigation Icon + +You can change the navigation icon using the `navigationIcon` method. This accepts a Heroicon string or a `Heroicon` enum value. + +```php +CommandRunnerPlugin::make()->navigationIcon('heroicon-o-exclamation-triangle') +``` + +### 5. Navigation Sort Order + +You can change the navigation sort order using the `navigationSort` method. + +```php +CommandRunnerPlugin::make()->navigationSort(10) +``` + + +### 6. Command Validation You can define custom validation logic using the validateCommand() method. This is useful for restricting which commands can be run: @@ -81,7 +122,7 @@ $panel->plugin( ); ``` -2. Delete Command History +### 7. Delete Command History Control who can delete command history entries using a boolean or closure with `canDeleteCommandHistory()`: @@ -91,7 +132,7 @@ $panel->plugin( ); ``` -3. Purge Old Command History +### 8. Purge Old Command History Schedule the following artisan command to purge command history entries daily: diff --git a/src/CommandRunnerPlugin.php b/src/CommandRunnerPlugin.php index 7fe6662..a5256bf 100644 --- a/src/CommandRunnerPlugin.php +++ b/src/CommandRunnerPlugin.php @@ -2,21 +2,50 @@ namespace BinaryBuilds\CommandRunner; +use BackedEnum; use BinaryBuilds\CommandRunner\Resources\CommandRuns\CommandRunResource; +use Closure; use Filament\Contracts\Plugin; use Filament\Panel; +use Filament\Support\Concerns\EvaluatesClosures; +use Filament\Support\Icons\Heroicon; +use UnitEnum; class CommandRunnerPlugin implements Plugin { + use EvaluatesClosures; + private $validationRule = null; private $deleteHistory = true; + private bool | Closure $authorized = true; + + private string | BackedEnum | null $navigationIcon = HeroIcon::OutlinedCommandLine; + + private string | UnitEnum | null $navigationGroup = 'Settings'; + + private string $navigationLabel = 'Command Runner'; + + private int | Closure $navigationSort = 9999; + public function getId(): string { return 'command-runner'; } + public function authorize(bool | Closure $callback): static + { + $this->authorized = $callback; + + return $this; + } + + public function isAuthorized(): bool + { + return (bool) $this->evaluate($this->authorized); + } + public function validateCommand(callable $commandRule): CommandRunnerPlugin { $this->validationRule = $commandRule; @@ -24,6 +53,55 @@ public function validateCommand(callable $commandRule): CommandRunnerPlugin return $this; } + public function navigationLabel(string $navigationLabel): static + { + $this->navigationLabel = $navigationLabel; + + return $this; + } + + public function getNavigationLabel(): string + { + return $this->navigationLabel; + } + + public function navigationGroup(string | UnitEnum | null $navigationGroup): static + { + $this->navigationGroup = $navigationGroup; + + return $this; + } + + public function getNavigationGroup(): string + { + return $this->navigationGroup; + } + + public function navigationSort(int | Closure $sort): static + { + $this->navigationSort = $sort; + + return $this; + } + + public function getNavigationSort(): int + { + /** @var int */ + return $this->evaluate($this->navigationSort); + } + + public function navigationIcon(string | BackedEnum | null $navigationIcon): static + { + $this->navigationIcon = $navigationIcon; + + return $this; + } + + public function getNavigationIcon(): string | Heroicon | null + { + return $this->navigationIcon; + } + public function canDeleteCommandHistory(callable | bool $deleteHistory): CommandRunnerPlugin { $this->deleteHistory = $deleteHistory; diff --git a/src/Resources/CommandRuns/CommandRunResource.php b/src/Resources/CommandRuns/CommandRunResource.php index 651e2a9..26e3130 100644 --- a/src/Resources/CommandRuns/CommandRunResource.php +++ b/src/Resources/CommandRuns/CommandRunResource.php @@ -2,6 +2,8 @@ namespace BinaryBuilds\CommandRunner\Resources\CommandRuns; +use BackedEnum; +use BinaryBuilds\CommandRunner\CommandRunnerPlugin; use BinaryBuilds\CommandRunner\Models\CommandRun; use BinaryBuilds\CommandRunner\Resources\CommandRuns\Pages\ListCommandRuns; use BinaryBuilds\CommandRunner\Resources\CommandRuns\Pages\RunCommand; @@ -13,14 +15,40 @@ use Filament\Schemas\Schema; use Filament\Support\Icons\Heroicon; use Filament\Tables\Table; +use UnitEnum; class CommandRunResource extends Resource { protected static ?string $slug = 'command-runner'; + protected static function getPlugin(): CommandRunnerPlugin + { + return CommandRunnerPlugin::get(); + } + public static function getNavigationLabel(): string { - return __('Command Runner'); + return static::getPlugin()->getNavigationLabel(); + } + + public static function canAccess(): bool + { + return static::getPlugin()->isAuthorized(); + } + + public static function getNavigationGroup(): string | UnitEnum | null + { + return static::getPlugin()->getNavigationGroup(); + } + + public static function getNavigationSort(): ?int + { + return static::getPlugin()->getNavigationSort(); + } + + public static function getNavigationIcon(): string | BackedEnum | Heroicon | null + { + return static::getPlugin()->getNavigationIcon(); } public static function getBreadcrumb(): string @@ -30,8 +58,6 @@ public static function getBreadcrumb(): string protected static ?string $model = CommandRun::class; - protected static string | null | \BackedEnum $navigationIcon = Heroicon::OutlinedCommandLine; - public static function infolist(Schema $schema): Schema { return CommandRunInfolist::configure($schema);