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 Custom Impersonate Record Handling #99

Open
wants to merge 1 commit into
base: master
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ Impersonate::make('impersonate')
->guard('another-guard')
->redirectTo(route('some.other.route'));
```

#### Setting a Custom Impersonation Record

You can customize the impersonation target by setting a specific impersonation record. This is particularly useful when the target user is related to another entity, and you need to specify which user should be impersonated.

```php
Impersonate::make('impersonate')
->setImpersonateRecord(fn($record) => $record->user);
```

### 2. Add the page action

Expand Down
14 changes: 14 additions & 0 deletions src/Concerns/Impersonates.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trait Impersonates

protected Closure|string|null $backTo = null;

protected Closure|null $impersonateRecord = null;

public static function getDefaultName(): ?string
{
return 'impersonate';
Expand Down Expand Up @@ -86,4 +88,16 @@ public function impersonate($record): bool|Redirector|RedirectResponse

return redirect($this->getRedirectTo());
}

public function setImpersonateRecord(Closure $record): static
{
$this->impersonateRecord = $record;

return $this;
}

public function getImpersonateRecord()
{
return $this->evaluate($this->impersonateRecord);
}
}
4 changes: 2 additions & 2 deletions src/Pages/Actions/Impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void
$this
->label(__('filament-impersonate::action.label'))
->icon('impersonate-icon')
->action(fn ($record) => $this->impersonate($record))
->hidden(fn ($record) => !$this->canBeImpersonated($record));
->action(fn ($record) => $this->impersonate($this->getImpersonateRecord() ?? $record))
->hidden(fn ($record) => ! $this->canBeImpersonated($this->getImpersonateRecord() ?? $record));
}
}
4 changes: 2 additions & 2 deletions src/Tables/Actions/Impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp(): void
->label(__('filament-impersonate::action.label'))
->iconButton()
->icon('impersonate-icon')
->action(fn ($record) => $this->impersonate($record))
->hidden(fn ($record) => !$this->canBeImpersonated($record));
->action(fn ($record) => $this->impersonate($this->getImpersonateRecord() ?? $record))
->hidden(fn ($record) => ! $this->canBeImpersonated($this->getImpersonateRecord() ?? $record));
}
}