PHPackages                             nycu-csit/laravel-impersonation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. nycu-csit/laravel-impersonation

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

nycu-csit/laravel-impersonation
===============================

3.0.0(1y ago)069MITPHP

Since Sep 13Pushed 5mo agoCompare

[ Source](https://github.com/nycu-csit/laravel-impersonation)[ Packagist](https://packagist.org/packages/nycu-csit/laravel-impersonation)[ Docs](https://gitlab.it.cs.nycu.edu.tw/nycu-csit/laravel-impersonation)[ RSS](/packages/nycu-csit-laravel-impersonation/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

Impersonation
=============

[](#impersonation)

This package is aimed for role-based Laravel site, enabling user to impersonate as another user.

Usage
-----

[](#usage)

This package will register a route of `/impersonation`, which list all of the users.

After impersonating as an user, you have to logout then login again in order to getting back to your account.

Since this package uses `Auth::loginUsingId()`, when impersonating, the user getting impersonated will be shown on the debugbar.

Installation
------------

[](#installation)

```
composer require nycu-csit/laravel-impersonation:^3.0.0
```

Configuration
-------------

[](#configuration)

```
php artisan vendor:publish --tag=impersonation
```

Available options:

- `enabled`: Whether to enable this package or not.
- `impersonable_roles`: Array of role names that can impersonate as other users.
- `post_impersonation_route`: The path to redirect to after impersonation.
- `display_columns`: Array of column names getting displayed when visiting `/impersonation`. Leave empty to show all columns.

Test
----

[](#test)

```
./vendor/bin/phpunit
```

Customize Impersonation Policy
------------------------------

[](#customize-impersonation-policy)

This package uses [policy discovery](https://laravel.com/docs/12.x/authorization#policy-discovery) mechanism provided by Laravel. That is, by default, if the user model is called `User` in your app, you should implement `impersonate(User $user): bool` in your `UserPolicy` class. However, if you bind another policy onto your user model, you should implement `impersonate(User $user): bool` in such policy instead.

For example, in `app/Policies/UserPolicy.php`, you should either

1. use the trait [`NycuCsit\Impersonation\Traits\ImpersonationPolicyTrait`](./src/Traits/ImpersonationPolicyTrait.php), or
2. provide custom impersonation logic like the following snippet

    ```
