Usage
Resolve the manager via the container or ImpersonationManager::make().
Start impersonating
use BradieTilley\Impersonation\ImpersonationManager;
$manager = ImpersonationManager::make();
$manager->impersonate($user);
This will:
- Resolve the current user via your configured (or default) user callback
- Authorize via
canImpersonate() - Dispatch
ImpersonationStarting(cancellable) - Push onto the session stack and log in as the impersonatee
- 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.