diff --git a/README.md b/README.md index 73631cc..176a62b 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,31 @@ There is also a helper function GazeBanner::make() ->hideOnCreate(), ``` +### Hide actions based on the controlling user (if you've implemented the take control logic) +You will need to use the `DiscoveryDesign\FilamentGaze\Traits\GazeLockControl` trait, in order to access the method. +```php +namespace App\Filament\Resources\CustomerResource\Pages; + +use App\Filament\Resources\CustomerResource; +use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl; +use Filament\Actions; +use Filament\Resources\Pages\EditRecord; + +class EditCustomer extends EditRecord +{ + use GazeLockControl; + + protected static string $resource = CustomerResource::class; + + protected function getHeaderActions(): array + { + return [ + Actions\DeleteAction::make()->hidden(!$this->hasGazeControl()), + ]; + } + +``` ## Docs @@ -138,6 +162,12 @@ GazeBanner::make() #### Description `hideOnCreate` is a helper function that can be used to hide the banner on create forms. +### `->hasGazeControl()` + +#### Description +`hasGazeControl` is a helper function that can be used to hide actions, and is usable from the GazeLockControl trait. + + ## Customization ### Change icons diff --git a/src/Forms/Components/GazeBanner.php b/src/Forms/Components/GazeBanner.php index 5296533..cd903b9 100644 --- a/src/Forms/Components/GazeBanner.php +++ b/src/Forms/Components/GazeBanner.php @@ -7,6 +7,7 @@ use Filament\Facades\Filament; use Filament\Forms\Components\Component; use Illuminate\Support\Facades\Cache; +use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl; /** * Class GazeBanner @@ -18,6 +19,7 @@ */ class GazeBanner extends Component { + use GazeLockControl; /** * The array of current viewers. */ @@ -63,7 +65,7 @@ public static function make(array | Closure $schema = []): static public function identifier(string | Closure $fnc = ''): static { $this->identifier = (string) $this->evaluate($fnc); - + Cache::put('filament-gaze-' . $this->getIdentifier() . '-custom-identfier',$this->identifier); return $this; } @@ -141,21 +143,7 @@ public function takeControl() } - public function getIdentifier() - { - if (! $this->identifier) { - $record = $this->getRecord(); - if (! $record) { - $this->identifier = (string) $this->getModel(); - } else { - $this->identifier = get_class($record) . '-' . $record->id; - } - } - - return $this->identifier; - } - - public function refreshForm() + public function refreshForm(): void { // Very hacky, maybe a better solution for this? $record = $this->getRecord(); @@ -173,7 +161,7 @@ public function refreshForm() * * @return void */ - public function refreshViewers() + public function refreshViewers(): void { $this->registerListeners([ 'FilamentGaze::takeControl' => [ diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php new file mode 100644 index 0000000..3682d1d --- /dev/null +++ b/src/Traits/GazeLockControl.php @@ -0,0 +1,61 @@ +identifier) { + $record = $this->getRecord(); + if (! $record) { + $this->identifier = (string) $this->getModel(); + } else { + $this->identifier = get_class($record) . '-' . $record->id; + } + } + // Ensure there's not a custom identifier set. + $customIdentifier = Cache::get('filament-gaze-' . $this->identifier . '-custom-identfier'); + if($customIdentifier) { + $this->identifier = $customIdentifier; + } + return $this->identifier; + } + + /** + * See if current user has gaze control access. + * @return bool + */ + public function hasGazeControl(): bool + { + if(!isset(request()->user()?->id)){ + return false; + } + $currentViewers = Cache::get('filament-gaze-' . $this->getIdentifier()); + + if( is_null($currentViewers) ) { + return true; + } + foreach ($currentViewers as $viewer) { + if($viewer['id'] === request()->user()->id && $viewer['has_control']) { + return true; + } + } + + return false; + } + +} \ No newline at end of file