Impersonation

Sign in as a user from the admin to see what they see, with a ribbon on every page until you leave. Two Blade partials over lab404/laravel-impersonate.

The profile page with the amber impersonation ribbon across the top
While impersonating, every kit layout shows the ribbon with an exit link.

What it adds

FilePurpose
resources/views/partials/impersonation-banner.blade.phpThe ribbon. <x-ui-kit::banners /> includes it automatically when impersonating.
resources/views/partials/impersonation-button.blade.phpA Login as button for a user row
canImpersonate() and canBeImpersonated() in app/Models/Concerns/UiKitUser.phpAdmins can impersonate; nobody can impersonate an admin

Install

php artisan ui-kit:install-module impersonation

Composer: lab404/laravel-impersonate:^1.7.5.

What the installer changes

  • Publishes the package config to config/laravel-impersonate.php.
  • Regenerates the UiKitUser trait with the two methods and re-applies it.
  • Adds Route::impersonate(); between the markers in routes/ui-kit-user.php, which registers GET /impersonate/take/{id} as impersonate and GET /impersonate/leave as impersonate.leave, both under web and auth. lab404/laravel-impersonate ships those routes as a router macro the app has to call; the installer calls it so the partials have something to link to. ui-kit:doctor checks both names exist.

The button

The admin user detail page shows Login as beside the user's name on its own: since 0.4.1 its view includes the partial whenever it exists, with @includeIf('partials.impersonation-button', ['user' => $user]). ui-kit:doctor checks it under impersonation button wired. A user detail view published by 0.4.0 or earlier lacks the line; add it, or re-run the installer with --force.

For screens of your own, include the partial next to each user:

@include('partials.impersonation-button', ['user' => $user])

It renders only when the current user can impersonate, the target can be impersonated, and the two are not the same user. Admins cannot be impersonated.

After taking over

The package redirects to take_redirect_to from its config, / by default, and the ribbon appears on every page that renders <x-ui-kit::banners />, which all three kit layouts do. Exit impersonation in the ribbon returns you to your own session.

Removing

Delete the two partials, remove the Route::impersonate(); line from routes/ui-kit-user.php and the @include, remove the two methods from the trait, remove impersonation from installed_modules, and composer remove lab404/laravel-impersonate. The routes line has to go before the package does, or the next request fails on the missing macro.

Was this page helpful?