octopyid/laravel-impersonate
Laravel Impersonate lets admins log in as any user without passwords, then switch back safely. It includes middleware, events, and multi-guard support for secure, flexible user impersonation in Laravel apps.
.env and any published config:
IMPERSONATE_ENABLED → IMPERSONATE_UI_ENABLEDIMPERSONATE_WIDTH → IMPERSONATE_UI_WIDTHIMPERSONATE_DELAY → IMPERSONATE_UI_DEBOUNCEimpersonate.enabled → impersonate.interface.enabledHasImpersonation trait to the model that should be impersonatable. v5 provides usable defaults: display text is $this->name and search field is ['name'], so basic usage needs no trait overrides.scopeImpersonatable() on that model. This is now the required place to enforce who can be searched and impersonated at the database level:public function scopeImpersonatable($query)
{
return $query->where('is_active', true);
}
IMPERSONATE_UI_ENABLED=true, define the impersonation scope, then use the widget to search within the scoped user set. For backend/admin flows, use the fluent shortcut:Impersonate::loginAs($user);
interface block, the HasImpersonation trait, the model’s scopeImpersonatable() method, and the /_impersonate/users and /_impersonate/login endpoint authorization behavior.scopeImpersonatable() as the primary guardrail for search results and impersonation eligibility. Use it for tenant boundaries, active flags, role checks, or organization constraints rather than filtering in PHP after the query..total() values stay accurate. Avoid reintroducing in-memory filtering around package queries.getImpersonateDisplayText() or getImpersonateSearchField() only where the UI should show or search different columns.scopeImpersonatable() methods or Laravel’s #[Scope] attribute convention, depending on the model’s existing style.Impersonate::loginAs($user) when an admin action, console command, or internal tool needs to impersonate without explicitly resolving/passing the impersonator. Keep the surrounding controller/action authorized as you would any sensitive admin operation.403 Forbidden for unauthorized access. Ensure your normal auth middleware and app-level authorization policies still wrap or complement these routes as needed.impersonate.enabled was moved under impersonate.interface.enabled.ImpersonateCollection filtering no longer happens after query execution. If scopeImpersonatable() is missing or too broad, the searchable/impersonatable set may be broader than intended.php artisan config:clear after updating .env or config files, especially when debugging why IMPERSONATE_UI_* settings are not taking effect.IMPERSONATE_DELAY is now IMPERSONATE_UI_DEBOUNCE; do not assume the old delay key still controls search debounce behavior.ImpersonateException. If you customize endpoints or repositories, catch/report that exception rather than expecting raw database errors.impersonate.interface for UI state, and Impersonate::loginAs() for controlled backend impersonation.How can I help you explore Laravel packages today?