Giter Site home page Giter Site logo

filament-impersonate's Introduction

Filament Impersonate

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'/>

filament-impersonate's People

Contributors

arjendejong12 avatar danharrin avatar eliaszobody avatar iantasker avatar intrepidws avatar jszobody avatar juliomotol avatar lloricode avatar matthans0n avatar pxlrbt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.