xlite-dev / filament-impersonate
This is my package filament-impersonate
Fund package maintenance!
icaliman
Requires
- php: ^8.1
- filament/filament: ^3.0
- illuminate/contracts: ^11.0|^10.0|^9.0
- lab404/laravel-impersonate: ^1.7.5
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
Installation
You can install the package via composer:
composer require xlite-dev/filament-impersonate
Usage
1. Add Table
action
Open the resource where you want the impersonate action to appear. This is generally going to be your UserResource
class.
Go down to the table
method. Inside actions
or prependActions
add ImpersonateAction::make
as a new action for the table. Your class should look like this:
namespace App\Filament\Resources; use Filament\Resources\Resource; use XliteDev\FilamentImpersonate\Tables\Actions\ImpersonateAction; // <--- class UserResource extends Resource { // ... public static function table(Table $table) { return $table ->columns([ // ... ]) ->actions([ ImpersonateAction::make(), // <--- // ... ]); }
You should now see an action icon next to each user in your Filament UserResource
list:
When you click on the impersonate icon you will be logged in as that user, and redirected to your main app. You will see the impersonation banner at the top of the page, with a button to leave and return to Filament:
2. Add Page
action
Open EditUser
or ViewUser
class, where you want the impersonate action to appear.
Go down to the getActions
method and add ImpersonateAction::make
as a new action for the page. Your class should look like this:
namespace App\Filament\Resources; use Filament\Resources\Resource; use XliteDev\FilamentImpersonate\Pages\Actions\ImpersonateAction; // <--- class EditUser extends ViewRecord { // ... protected function getActions(): array { return [ ImpersonateAction::make()->record($this->getRecord()), // <--- // ... ]; }
You should now see an action icon on the EditUser
or ViewUser
page:
Configuration
You can publish the config file with:
php artisan vendor:publish --tag="filament-impersonate-config"
This is the contents of the published config file:
return [ // This is the guard used when logging in as the impersonated user. 'guard' => env('FILAMENT_IMPERSONATE_GUARD', 'web'), // After impersonating this is where we'll redirect you to. 'redirect_to' => env('FILAMENT_IMPERSONATE_REDIRECT', '/'), // We wire up a route for the "leave" button. You can change the middleware stack here if needed. 'leave_middlewares' => [ env('FILAMENT_IMPERSONATE_LEAVE_MIDDLEWARE', 'web'), ], 'banner' => [ // Currently supports 'dark' and 'light'. 'style' => env('FILAMENT_IMPERSONATE_BANNER_STYLE', 'dark'), // Turn this off if you want `absolute` positioning, so the banner scrolls out of view 'fixed' => env('FILAMENT_IMPERSONATE_BANNER_FIXED', true), // Currently supports 'top' and 'bottom'. 'position' => env('FILAMENT_IMPERSONATE_BANNER_POSITION', 'top'), ], ];
Authorization
By default, only Filament admins can impersonate other users. You can control this by adding a canImpersonate
method to your FilamentUser
class:
class User implements FilamentUser { public function canImpersonate() { return true; } }
You can also control which targets can be impersonated. Just add a canBeImpersonated
method to the user class with whatever logic you need:
class User { public function canBeImpersonated() { // Let's prevent impersonating other users at our own company return !Str::endsWith($this->email, '@mycorp.com'); } }
Customizing the banner
You can publish the views using
php artisan vendor:publish --tag="filament-impersonate-views"
The blade component has a few options you can customize.
Style
The banner is dark by default, you can set this to light:
<x-filament-impersonate::banner style='light'/>
Display name
The banner will show the name of the impersonated user, assuming there is a name
attribute. You can customize this if needed:
<x-filament-impersonate::banner :display='auth()->user()->email'/>
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.