Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GazeLockControl Trait to extend control functionality #28

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
22 changes: 5 additions & 17 deletions src/Forms/Components/GazeBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@
*/
class GazeBanner extends Component
{
use GazeLockControl;
/**
* The array of current viewers.
*/
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -173,7 +161,7 @@ public function refreshForm()
*
* @return void
*/
public function refreshViewers()
public function refreshViewers(): void
{
$this->registerListeners([
'FilamentGaze::takeControl' => [
Expand Down
61 changes: 61 additions & 0 deletions src/Traits/GazeLockControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace DiscoveryDesign\FilamentGaze\Traits;

use Illuminate\Support\Facades\Cache;


/**
* Trait GazeLockControl
*
* This Trait can be used by pages etc to control button access, and is used to contain the identifier logic.
*/
trait GazeLockControl {
/**
* The custom identifier for the GazeBanner component.
*/
public ?string $identifier = null;

public function getIdentifier(): ?string
{
// Try to get the custom identifier first, if one has been set.
if (! $this->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;
}

}