This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider becoming a backer or sponsor on Patreon.
- Laravel 8.0+
- PHP 7.3+
composer require genealabs/laravel-impersonator
genealabs-laravel-impersonator.layout
: master blade layout view for your application (defaultlayouts.app
).genealabs-laravel-impersonator.content-section
: name of content section in master layout blade view (defaultcontent
).genealabs-laravel-impersonator.user-model
: user model of your application (defaultconfig('auth.providers.users.model')
).genealabs-laravel-impersonator.middleware
: the middleware to use for the impersonatee routes (default:['web', 'auth']
). You may specifyonly
orexcept
parameters. E.g.['web', 'auth', 'password.confirm' => ['except' => 'destroy']]
If you need to customize these settings:
php artisan impersonator:publish --config
- Add trait
GeneaLabs\LaravelImpersonator\Traits\Impersonatable
to your user model. - Override trait method
public function getCanImpersonateAttribute() : bool
that determines if a given user can impersonate other users. - (optional) Override trait method
public function getCanBeImpersonatedAttribute() : bool
that determines if a given user can be impersonated. - Use
route('impersonatees.index')
to view a list of all impersonatable users. You could add something like the following to your menu:
@if ((auth()->user()->canImpersonate ?? false) && ! session('impersonator'))
<a class="dropdown-item" href="{{ route('impersonatees.index') }}">Impersonator</a>
@endif
- (optional) Add something like the following to your menu view to allow imporsonator to stop impersonating:
@if (session('impersonator'))
<a href="{{ url('/logout') }}"
class="dropdown-item"
onclick="event.preventDefault(); document.getElementById('end-personation-session-form').submit();"
>
End Impersonation Session
</a>
<form action="{{ route('impersonatees.destroy', auth()->user()) }}"
method="POST"
style="display: none;"
id="end-personation-session-form"
>
{{ csrf_field () }}
{{ method_field ('DELETE') }}
</form>
@else
<a href="{!! route('logout') !!}"
class="dropdown-item"
onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form method="POST"
action="{{ route('logout') }}"
accept-charset="UTF-8"
id="logout-form"
style="display:none;"
>
{{ csrf_field () }}
</form>
@endif
php artisan impersonator:publish --views
In large part prodded and inspired by LaraCasts' tutorial: https://laracasts.com/series/how-do-i/episodes/17. Thank you @JeffreyWay!