Skip to main content
Back to packages
325 GitHub stars

OctopyID/LaraPersonate

View on GitHub

Description

Is an Impersonation package for the Laravel Framework. With this package you can easily impersonate other users either manually or using the interface we provide

Basic Usage

By default, you don't need to do anything, but keep in mind, Impersonation can be done by anyone if you don't define the rules of who can do impersonation or who can be impersonated.

Defining Limitation

To limit who can do impersonation or who is can be impersonated, add setImpersonateAuthorization(Authorization $authorization) on the Model to enforce the limitation.

The impersonator method is intended for who can perform the impersonation and the impersonated method is intended for anyone who is allowed to be imitated.

Warning: Not defining the Authorization rules in the Model or misdefining them can lead to serious security issues.

The example below uses Laratrust for role management where SUPER_ADMIN can perform impersonation against CUSTOMER. Feel free to use any other Role Management you like.

use Octopy\Impersonate\Concerns\HasImpersonation;
use Octopy\Impersonate\Authorization;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable
{
use HasImpersonation;
 
/**
* @param Authorization $authorization
* @return void
*/
public function setImpersonateAuthorization(Authorization $authorization) : void
{
$authorization->impersonator(function (User $user) {
return $user->hasRole('SUPER_ADMIN');
});
 
$authorization->impersonated(function (User $user) {
return $user->hasRole('CUSTOMER');
});
}
}

Recent Courses on Laravel Daily

Next.js Basics for Laravel Developers

11 lessons
58 min

Roles and Permissions in Laravel 13

14 lessons
57 min

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.