lab404/laravel-impersonate
Laravel package that lets admins impersonate other users in a secure, reversible way. Start/stop impersonation via middleware and helpers, with session-based tracking and easy integration into your auth flow for debugging, support, and admin panels.
Authenticatable models).getImpersonator() and guard-specific impersonation.ImpersonateStarted, ImpersonateEnded) for auditability, aligning with Laravel’s ecosystem (e.g., logging, monitoring).@impersonatable and @canImpersonate, reducing custom template logic.Misalignment Risks:
Authenticatable implementations without getAuthIdentifier(), additional configuration is needed (see #105).Impersonate::impersonate($user)). Middleware and routes are optional but recommended.sessions table).@canImpersonate).Blockers:
getAuthIdentifier() overrides.| Risk Area | Severity | Mitigation |
|---|---|---|
| Session Hijacking | High | Use middleware to restrict impersonation to trusted roles (e.g., admins). |
| Auditability Gaps | Medium | Integrate events with logging (e.g., Laravel’s Log::channel()). |
| Multi-Guard Conflicts | Low | Test guard-specific impersonation early (package fixes issues like #120). |
| Performance Overhead | Low | Session storage is minimal; no DB queries during impersonation. |
| Upgrade Risks | Low | Active maintenance (releases every 6–12 months). |
Critical Questions for TPM:
getAuthIdentifier()?Authenticatable contracts (e.g., getAuthIdentifier()).@impersonatable).Non-Fit Scenarios:
getAuthIdentifier() overrides.Pre-Integration:
ImpersonateStarted).Installation:
composer require lab404/laravel-impersonate
php artisan vendor:publish --tag="impersonate-config".Core Setup:
/impersonate/{user}):
Route::middleware(['auth', 'can:impersonate'])->get('/impersonate/{user}', [ImpersonateController::class, 'impersonate']);
public function handle($request, Closure $next) {
if (!auth()->user()->can('impersonate')) {
abort(403);
}
return $next($request);
}
UI Integration:
@impersonatable
<button>Impersonate</button>
@endimpersonatable
Testing:
| Component | Compatibility |
|---|---|
| Laravel 8.x–13.x | ✅ Fully supported. |
| PHP 8.0–8.4 | ✅ Tested. |
| Eloquent Models | ✅ Assumed (use getAuthIdentifier()). |
| Multi-Guard | ✅ Supported (fixes for #120). |
| Database Sessions | ✅ Works natively. |
| API (Stateless) | ⚠️ Requires custom session storage (e.g., Redis). |
| Custom Auth | ⚠️ Needs getAuthIdentifier() implementation. |
Phase 1: Core Integration (1–2 sprints)
Phase 2: Policy Enforcement (1 sprint)
Phase 3: UI/UX (1 sprint)
Phase 4: Validation (Ongoing)
Maintenance Tasks:
Impersonate::leave()).Support Risks:
How can I help you explore Laravel packages today?