stechstudio/filament-impersonate

A Filament record action that allows you to impersonate your users.

2.11 2023-01-09 17:35 UTC

README

Latest Version on Packagist Software License

This is a plugin for Filament that makes it easy to impersonate your users.

Credit

This package uses https://github.com/404labfr/laravel-impersonate under the hood, and borrows heavily from https://github.com/KABBOUCHI/nova-impersonate.

Installation

You know the drill:

composer require stechstudio/filament-impersonate

Quickstart

1. Add Resource action

First 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. After defining the table columns, you want to prependActions and provide Impersonate::make as a new action for the table. Your class should look like this:

namespace App\Filament\Resources;

use Filament\Resources\Resource;
use STS\FilamentImpersonate\Impersonate;

class UserResource extends Resource {
    public static function table(Table $table)
    {
        return $table
            ->columns([
                // ...
            ])
            ->prependActions([
                Impersonate::make('impersonate'), // <--- 
            ]);
    }

If you already have an actions() method defined for your table, place the Impersonate::make directly into the actions array.

return $table
    ->columns([
        // ...
    ])
    ->actions([
        Impersonate::make('impersonate'), // <---
        // ...
    ]);

You can also define a guard and redirectTo for the action:

Impersonate::make('impersonate')
    ->guard('another-guard')
    ->redirectTo(route('some.other.route'));

2. Add the banner to your blade layout

The only other step is to display a notice in your app whenever you are impersonating another user. Open up your master layout file and add <x-impersonate::banner/> before the closing </body> tag.

3. Profit!

That's it. You should now see an action icon next to each user in your Filament UserResource list:

CleanShot 2022-01-03 at 14 10 36@2x

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:

banner

Configuration

All configuration can be managed with ENV variables, no need to publish and edit the config directly. Just check out the config file.

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

The blade component has a few options you can customize.

Style

The banner is dark by default, you can set this to light:

<x-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-impersonate::banner :display='auth()->user()->email'/>