Allows a user to impersonate another user.
466
0
2
Package Data
Maintainer Username: ChrisThompsonTLDR
Maintainer Contact: christhompsontldr@gmail.com (Chris Thompson)
Package Create Date: 2020-01-15
Package Last Update: 2020-10-13
Language: PHP
License: Unknown
Last Refreshed: 2024-10-25 03:01:31
Package Statistics
Total Downloads: 466
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Impersonate

Allow a user to impersonate another user.

Installation

Composer

Require this package with composer:

composer require christhompsontldr/impersonate

Service Provider

After updating composer, add the ServiceProvider to the providers array in config/app.php

Laravel 5.4 or below:

Christhompsontldr\Impersonate\ImpersonateServiceProvider::class,

Laravel 5.5 or above will auto discover the needed service provider.

Setup

This will apply a trait to the user model configured in config/auth.php. setup runs both the add-trait and publish commands.

php artisan impersonate:setup

This will run two commands (which can be run independently):

php artisan impersonate:add-trait
php artisan impersonate:publish

publish publishes the config and add-trait applies a trait to the user model.

Access Control

You must complete this step, or none of your users will have permission to impersonate.

The authorized users that can impersonate and which users they can impersonate is controlled via the trait. This can be overloaded on your user model

In this example, the user model has an is_admin attribute that is being checked.

public function canImpersonate($id)
{
    return $this->is_admin ?: false;
}

Or if you are using Laratrust

public function canImpersonate($id)
{
    return $this->hasRole('admin');
}

Issues

Log out will be performed on both the main user and the impersonated user.

Source

This package is based off an this example.