Events
All events carry an Impersonation DTO (impersonator, impersonatee,
timestamp, level).
Lifecycle
| Event | When |
|---|---|
ImpersonationStarting | Before stack/login changes; cancellable |
ImpersonationStarted | After login and session save |
ImpersonationFinished | After a level is restored (login + save) |
Events are synchronous. Queue work in your listeners, not on the event classes.
Cancelling a start
Return false from a listener:
use BradieTilley\Impersonation\Events\ImpersonationStarting;
use Illuminate\Support\Facades\Event;
Event::listen(ImpersonationStarting::class, function (ImpersonationStarting $event) {
if ($event->impersonation->impersonatee->isBanned()) {
return false;
}
});
Or call abort():
Event::listen(ImpersonationStarting::class, function (ImpersonationStarting $event) {
$event->abort('User cannot be impersonated right now');
});
Either path throws ImpersonationCancelledException and leaves auth/session
unchanged.
Auditing example
Event::listen(ImpersonationStarted::class, function (ImpersonationStarted $event) {
Log::info('Impersonation started', [
'impersonator' => $event->impersonation->impersonator->getKey(),
'impersonatee' => $event->impersonation->impersonatee->getKey(),
'level' => $event->impersonation->level,
]);
});
See Extensibility for deeper customization.