Projectslaravel-impersonationUsage

Laravel Impersonation

Package

Session-based user impersonation for Laravel.

Upgrade Guide

Usage

Resolve the manager via the container or ImpersonationManager::make().

Start impersonating

use BradieTilley\Impersonation\ImpersonationManager;

$manager = ImpersonationManager::make();

$manager->impersonate($user);

This will:

  1. Resolve the current user via your configured (or default) user callback
  2. Authorize via canImpersonate()
  3. Dispatch ImpersonationStarting (cancellable)
  4. Push onto the session stack and log in as the impersonatee
  5. Dispatch ImpersonationStarted

Check state

$manager->isImpersonating(); // bool
$manager->level();           // int depth
$manager->getCurrentImpersonation(); // ?Impersonation DTO
$manager->impersonations();  // array of Impersonation
$manager->impersonator();    // immediate parent user, or null
$manager->originalUser();    // root impersonator, or null

Stop impersonating

$manager->stopImpersonating();     // one level
$manager->stopImpersonating(2);    // two levels
$manager->stopAllImpersonating();  // unwind the full stack

After each level is restored, ImpersonationFinished is dispatched.

Authorization helper

$manager->canImpersonate($impersonator, $impersonatee); // bool

Self-impersonation (same Eloquent model) is always denied. Depth limits and your authorize callback also apply.

See Authorization and Events.