Skip to content

Filament Impersonate is a plugin that allows you to authenticate as your users.

License

Notifications You must be signed in to change notification settings

xlited/filament-impersonate

Repository files navigation

Filament Impersonate - Authenticate as your users

Latest Version on Packagist Total Downloads

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:

image

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:

image

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:

image

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.

About

Filament Impersonate is a plugin that allows you to authenticate as your users.

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •