Projectslaravel-impersonationInstallation

Laravel Impersonation

Package

Session-based user impersonation for Laravel.

Upgrade Guide

Installation

Requirements

  • PHP 8.3+
  • Laravel 13+

Install

composer require bradietilley/laravel-impersonation

The service provider is auto-discovered.

Publish the config (optional)

php artisan vendor:publish --tag=impersonation-config

See Configuration for all options.

Implement Impersonateable

Your user model (and any model you impersonate as) must implement the marker interface. Default auth handlers ignore users that do not:

use BradieTilley\Impersonation\Contracts\Impersonateable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Impersonateable
{
    //
}

Configure authorization

In a service provider boot method:

use BradieTilley\Impersonation\ImpersonationManager;

ImpersonationManager::configure(
    authorize: function ($impersonator, $impersonatee): bool {
        return $impersonator->isAdmin() && ! $impersonatee->isAdmin();
    },
);

Without configure(), impersonation throws MissingImpersonationConfigurationException.

Next steps