Skip to main content
Back to packages
324 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

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

Queues in Laravel 13

18 lessons
1 h 12 min read

How to Build Laravel 13 API From Scratch

30 lessons
1 h 23 min