Download the PHP package rickycezar/laravel-jwt-impersonate without Composer
On this page you can find all versions of the php package rickycezar/laravel-jwt-impersonate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rickycezar/laravel-jwt-impersonate
More information about rickycezar/laravel-jwt-impersonate
Files in rickycezar/laravel-jwt-impersonate
Package laravel-jwt-impersonate
Short Description Laravel Impersonate is a plugin that allows to you to authenticate as your users.
License MIT
Informations about the package laravel-jwt-impersonate
Laravel JWT Impersonate
DISCLAIMER: This is a fork of lab404/laravel-impersonate patched to work with JWTAuth in a REST API application. I'll Always recommend you to use the original component.
Laravel JWT 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.
- Requirements
- Installation
- Simple usage
- Using the built-in controller
- Advanced Usage
- Defining impersonation authorization
- Using your own strategy
- Middleware
- Exceptions
- Events
- Configuration
Requirements
- Laravel >= 5.8
- PHP >= 7.1
- JWT-Auth >= dev-develop
Installation
-
Require it with Composer:
-
Add the service provider at the end of your
config/app.php
: - Add the trait
Rickycezar\Impersonate\Models\Impersonate
to your User model.
Simple usage
Impersonate a user:
Leave impersonation:
Using the built-in controller
In your routes file you can call the impersonate
route macro if you want to use the built-in controller.
Alternatively, you can execute this macro with your RouteServiceProvider
.
Advanced Usage
Defining impersonation authorization
By default all users can impersonate an user.
You need to add the method canImpersonate()
to your user model:
By default all users can be impersonated.
You need to add the method canBeImpersonated()
to your user model to extend this behavior:
Using your own strategy
It is possible to implement your own controller to deal with impersonation:
- Working with the manager:
Middleware
Protect From Impersonation
You can use the middleware impersonate.protect
to protect your routes against user impersonation.
This middleware can be useful when you want to protect specific pages like users subscriptions, users credit cards, ...
Exceptions
There are six possible exceptions thrown by the service:
AlreadyImpersonatingException
is thrown when an impersonator tries to take another persona without leaving the first one.CantBeImpersonatedException
is thrown when the methodcanBeImpersonated()
fails.CantImpersonateException
is thrown when the methodcanImpersonate()
fails.CantImpersonateSelfException
is thrown when an user tries to impersonate self.NotImpersonatingException
is thrown when an user tries to leave an impersonation without being impersonating.ProtectedFromImpersonationException
is thrown when an impersonator tries to get access to a route protected by the middleware.
Each exception have a message and a status code available through the respective methods getErrorMessage()
and getErrorCode()
.
Events
There are two events available that can be used to improve your workflow:
TakeImpersonation
is fired when an impersonation is taken.LeaveImpersonation
is fired when an impersonation is left.
Each events returns two properties $event->impersonator
and $event->impersonated
containing a User model isntance.
Configuration
The package comes with a configuration file.
Publish it with the following command:
Available options:
Licence
MIT