{"id":13396063,"url":"https://github.com/404labfr/laravel-impersonate","last_synced_at":"2025-05-09T23:18:45.195Z","repository":{"id":17270562,"uuid":"81589681","full_name":"404labfr/laravel-impersonate","owner":"404labfr","description":"Laravel Impersonate is a plugin that allows you to authenticate as your users.","archived":false,"fork":false,"pushed_at":"2025-02-24T16:19:06.000Z","size":148,"stargazers_count":2138,"open_issues_count":69,"forks_count":218,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-09T01:14:58.586Z","etag":null,"topics":["impersonate","laravel","laravel-package","php","user-management"],"latest_commit_sha":null,"homepage":"https://marceau.casals.fr","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/404labfr.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-02-10T17:35:27.000Z","updated_at":"2025-05-08T22:46:30.000Z","dependencies_parsed_at":"2024-01-15T09:02:40.503Z","dependency_job_id":"fb204251-7c2b-43cf-97e0-b0d37dc5ee1f","html_url":"https://github.com/404labfr/laravel-impersonate","commit_stats":{"total_commits":123,"total_committers":33,"mean_commits":3.727272727272727,"dds":0.5203252032520325,"last_synced_commit":"d8ab69f05daab4117b313e11ca007fbf3199a1ab"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/404labfr%2Flaravel-impersonate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/404labfr%2Flaravel-impersonate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/404labfr%2Flaravel-impersonate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/404labfr%2Flaravel-impersonate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/404labfr","download_url":"https://codeload.github.com/404labfr/laravel-impersonate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253337062,"owners_count":21892824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["impersonate","laravel","laravel-package","php","user-management"],"created_at":"2024-07-30T18:00:38.768Z","updated_at":"2025-05-09T23:18:45.172Z","avatar_url":"https://github.com/404labfr.png","language":"PHP","funding_links":[],"categories":["Popular Packages","PHP","Packages"],"sub_categories":["Authentication/Security"],"readme":"# Laravel Impersonate\n\n[![Build Status](https://travis-ci.org/404labfr/laravel-impersonate.svg?branch=master)](https://travis-ci.org/404labfr/laravel-impersonate) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/404labfr/laravel-impersonate/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/404labfr/laravel-impersonate/?branch=master)\n\n**Laravel Impersonate** makes it easy to **authenticate as your users**. Add a simple **trait** to your **user model** and impersonate as one of your users in one click.\n \n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Simple usage](#simple-usage)\n    - [Using the built-in controller](#using-the-built-in-controller)\n- [Advanced Usage](#advanced-usage)\n    - [Defining impersonation authorization](#defining-impersonation-authorization)\n    - [Using your own strategy](#using-your-own-strategy)\n    - [Middleware](#middleware)\n    - [Events](#events)\n- [Configuration](#configuration)\n- [Blade](#blade)\n- [Tests](#tests)\n- [Contributors](#contributors)\n- [Why Not Just Use loginAsId()?](#rationale)\n\n## Requirements\n\n- Laravel 6.x to 12.x\n- PHP \u003e= 7.2 or \u003e= 8.0\n\n### Laravel support\n\n| Version       | Release       |\n|:-------------:|:-------------:|\n| 6.x to 12.x   | 1.7           |\n| 6.x, 7.x      | 1.6           |\n| 5.8           | 1.5           |\n| 5.7, 5.6      | 1.2           |\n| 5.5, 5.4      | 1.1           |\n\n## Installation\n\n- Require it with Composer:\n```bash\ncomposer require lab404/laravel-impersonate\n```\n\n- Add the service provider at the end of your `config/app.php`:\n```php\n'providers' =\u003e [\n    // ...\n    Lab404\\Impersonate\\ImpersonateServiceProvider::class,\n],\n```\n\n- Add the trait `Lab404\\Impersonate\\Models\\Impersonate` to your **User** model.\n\n## Simple usage\n\nImpersonate a user:\n```php\nAuth::user()-\u003eimpersonate($other_user);\n// You're now logged as the $other_user\n```\n\nLeave impersonation:\n```php\nAuth::user()-\u003eleaveImpersonation();\n// You're now logged as your original user.\n```\n\n### Using the built-in controller\n\nIn your routes file, under web middleware, you must call the `impersonate` route macro. \n\n```php\nRoute::impersonate();\n```\n\nAlternatively, you can execute this macro with your `RouteServiceProvider`.\n\n```php\nnamespace App\\Providers;\n\nclass RouteServiceProvider extends ServiceProvider\n{\n    public function map() {\n        Route::middleware('web')-\u003egroup(function (Router $router) {\n            $router-\u003eimpersonate();\n        });\n    }\n}\n```\n\n```php\n// Where $id is the ID of the user you want impersonate\nroute('impersonate', $id)\n\n// Or in case of multi guards, you should also add `guardName` (defaults to `web`)\nroute('impersonate', ['id' =\u003e $id, 'guardName' =\u003e 'admin'])\n\n// Generate an URL to leave current impersonation\nroute('impersonate.leave')\n```\n\n## Advanced Usage\n\n### Defining impersonation authorization\n\nBy default all users can **impersonate** an user.  \nYou need to add the method `canImpersonate()` to your user model:\n\n```php\n    /**\n     * @return bool\n     */\n    public function canImpersonate()\n    {\n        // For example\n        return $this-\u003eis_admin == 1;\n    }\n```\n\nBy default all users can **be impersonated**.  \nYou need to add the method `canBeImpersonated()` to your user model to extend this behavior:\n\n```php\n    /**\n     * @return bool\n     */\n    public function canBeImpersonated()\n    {\n        // For example\n        return $this-\u003ecan_be_impersonated == 1;\n    }\n```\n\n### Using your own strategy\n\n- Getting the manager:\n```php\n// With the app helper\napp('impersonate')\n// Dependency Injection\npublic function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }\n```\n\n- Working with the manager:\n```php\n$manager = app('impersonate');\n\n// Find an user by its ID\n$manager-\u003efindUserById($id);\n\n// TRUE if your are impersonating an user.\n$manager-\u003eisImpersonating();\n\n// Impersonate an user. Pass the original user and the user you want to impersonate\n$manager-\u003etake($from, $to);\n\n// Leave current impersonation\n$manager-\u003eleave();\n\n// Get the impersonator ID\n$manager-\u003egetImpersonatorId();\n```\n\n### Middleware\n\n**Protect From Impersonation**\n\nYou can use the middleware `impersonate.protect` to protect your routes against user impersonation.  \nThis middleware can be useful when you want to protect specific pages like users subscriptions, users credit cards, ... \n\n```php\nRouter::get('/my-credit-card', function() {\n    echo \"Can't be accessed by an impersonator\";\n})-\u003emiddleware('impersonate.protect');\n```\n\n### Events\n\nThere are two events available that can be used to improve your workflow:\n- `TakeImpersonation` is fired when an impersonation is taken.\n- `LeaveImpersonation` is fired when an impersonation is leaved.\n\nEach events returns two properties `$event-\u003eimpersonator` and `$event-\u003eimpersonated` containing User model instance.\n\n## Configuration\n\nThe package comes with a configuration file.  \n\nPublish it with the following command:\n```bash\nphp artisan vendor:publish --tag=impersonate\n```\n\nAvailable options:\n```php\n    // The session key used to store the original user id.\n    'session_key' =\u003e 'impersonated_by',\n    // Where to redirect after taking an impersonation.\n    // Only used in the built-in controller.\n    // You can use: an URI, the keyword back (to redirect back) or a route name\n    'take_redirect_to' =\u003e '/',\n    // Where to redirect after leaving an impersonation.\n    // Only used in the built-in controller.\n    // You can use: an URI, the keyword back (to redirect back) or a route name\n    'leave_redirect_to' =\u003e '/'\n```\n\n## Blade\n\nThere are three Blade directives available.\n\n### When the user can impersonate\n\n```blade\n@canImpersonate($guard = null)\n    \u003ca href=\"{{ route('impersonate', $user-\u003eid) }}\"\u003eImpersonate this user\u003c/a\u003e\n@endCanImpersonate\n```\n\n### When the user can be impersonated\n\nThis comes in handy when you have a user list and want to show an \"Impersonate\" button next to all the users.\nBut you don\\'t want that button next to the current authenticated user neither to that users which should not be able to impersonated according your implementation of `canBeImpersonated()` . \n\n```blade\n@canBeImpersonated($user, $guard = null)\n    \u003ca href=\"{{ route('impersonate', $user-\u003eid) }}\"\u003eImpersonate this user\u003c/a\u003e\n@endCanBeImpersonated\n```\n\n### When the user is impersonated\n\n```blade\n@impersonating($guard = null)\n    \u003ca href=\"{{ route('impersonate.leave') }}\"\u003eLeave impersonation\u003c/a\u003e\n@endImpersonating\n```\n\n## Tests\n\n```bash\nvendor/bin/phpunit\n```\n\n## Contributors\n\n- This package was created by [MarceauKa](https://github.com/MarceauKa) and [tghpow](https://github.com/tghpow). Many thanks to all of our [contributors](https://github.com/404labfr/laravel-impersonate/graphs/contributors).\n\n## Rationale\n\n### Why not just use `loginAsId()`?\n\nThis package adds broader functionality, including Blade directives to allow you to override analytics and other tracking events when impersonating, fire events based on impersonation status, and more. Brief discussion at [issues/5](https://github.com/404labfr/laravel-impersonate/issues/5)\n\n## Licence\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F404labfr%2Flaravel-impersonate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F404labfr%2Flaravel-impersonate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F404labfr%2Flaravel-impersonate/lists"}