2017 © Pedro Peláez
 

library laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

image

lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  • Friday, January 19, 2018
  • by MarceauKa
  • Repository
  • 21 Watchers
  • 483 Stars
  • 95,661 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 54 Forks
  • 18 Open issues
  • 13 Versions
  • 38 % Grown

The README.md

Laravel Impersonate

Build Status Scrutinizer Code Quality, (*1)

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., (*2)

Requirements

  • Laravel 6.x to 11.x
  • PHP >= 7.2 or >= 8.0

Laravel support

Version Release
6.x to 11.x 1.7
6.x, 7.x 1.6
5.8 1.5
5.7, 5.6 1.2
5.5, 5.4 1.1

Installation

  • Require it with Composer:
composer require lab404/laravel-impersonate
  • Add the service provider at the end of your config/app.php:
'providers' => [
    // ...
    Lab404\Impersonate\ImpersonateServiceProvider::class,
],
  • Add the trait Lab404\Impersonate\Models\Impersonate to your User model.

Simple usage

Impersonate a user:, (*3)

Auth::user()->impersonate($other_user);
// You're now logged as the $other_user

Leave impersonation:, (*4)

Auth::user()->leaveImpersonation();
// You're now logged as your original user.

Using the built-in controller

In your routes file, under web middleware, you must call the impersonate route macro., (*5)

Route::impersonate();

Alternatively, you can execute this macro with your RouteServiceProvider., (*6)

namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
        Route::middleware('web')->group(function (Router $router) {
            $router->impersonate();
        });
    }
}
// Where $id is the ID of the user you want impersonate
route('impersonate', $id)

// Or in case of multi guards, you should also add `guardName` (defaults to `web`)
route('impersonate', ['id' => $id, 'guardName' => 'admin'])

// Generate an URL to leave current impersonation
route('impersonate.leave')

Advanced Usage

Defining impersonation authorization

By default all users can impersonate an user.
You need to add the method canImpersonate() to your user model:, (*7)

    /**
     * @return bool
     */
    public function canImpersonate()
    {
        // For example
        return $this->is_admin == 1;
    }

By default all users can be impersonated.
You need to add the method canBeImpersonated() to your user model to extend this behavior:, (*8)

    /**
     * @return bool
     */
    public function canBeImpersonated()
    {
        // For example
        return $this->can_be_impersonated == 1;
    }

Using your own strategy

  • Getting the manager:
// With the app helper
app('impersonate')
// Dependency Injection
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
  • Working with the manager:
$manager = app('impersonate');

// Find an user by its ID
$manager->findUserById($id);

// TRUE if your are impersonating an user.
$manager->isImpersonating();

// Impersonate an user. Pass the original user and the user you want to impersonate
$manager->take($from, $to);

// Leave current impersonation
$manager->leave();

// Get the impersonator ID
$manager->getImpersonatorId();

Middleware

Protect From Impersonation, (*9)

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, ..., (*10)

Router::get('/my-credit-card', function() {
    echo "Can't be accessed by an impersonator";
})->middleware('impersonate.protect');

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 leaved., (*11)

Each events returns two properties $event->impersonator and $event->impersonated containing User model instance., (*12)

Configuration

The package comes with a configuration file., (*13)

Publish it with the following command:, (*14)

php artisan vendor:publish --tag=impersonate

Available options:, (*15)

    // The session key used to store the original user id.
    'session_key' => 'impersonated_by',
    // Where to redirect after taking an impersonation.
    // Only used in the built-in controller.
    // You can use: an URI, the keyword back (to redirect back) or a route name
    'take_redirect_to' => '/',
    // Where to redirect after leaving an impersonation.
    // Only used in the built-in controller.
    // You can use: an URI, the keyword back (to redirect back) or a route name
    'leave_redirect_to' => '/'

Blade

There are three Blade directives available., (*16)

When the user can impersonate

@canImpersonate($guard = null)
    <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanImpersonate

When the user can be impersonated

This comes in handy when you have a user list and want to show an "Impersonate" button next to all the users. But 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() ., (*17)

@canBeImpersonated($user, $guard = null)
    <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanBeImpersonated

When the user is impersonated

@impersonating($guard = null)
    <a href="{{ route('impersonate.leave') }}">Leave impersonation</a>
@endImpersonating

Tests

vendor/bin/phpunit

Contributors

Rationale

Why not just use loginAsId()?

This 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, (*18)

Licence

MIT, (*19)

The Versions

19/01 2018

dev-master

9999999-dev

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

plugin laravel user auth package impersonate laravel-package laravel-plugin impersonation

19/01 2018

1.2.2

1.2.2.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

plugin laravel user auth package impersonate laravel-package laravel-plugin impersonation

03/09 2017

1.2.1

1.2.1.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

plugin laravel user auth package impersonate laravel-package laravel-plugin impersonation

28/07 2017

1.2.0

1.2.0.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

plugin laravel user auth package impersonate laravel-package laravel-plugin impersonation

29/05 2017

1.1.1

1.1.1.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

plugin laravel user auth package impersonate laravel-package laravel-plugin impersonation

05/04 2017

1.1.0

1.1.0.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

05/04 2017

1.0.x-dev

1.0.9999999.9999999-dev

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

05/04 2017

1.0.11

1.0.11.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

28/02 2017

1.0.10

1.0.10.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

20/02 2017

1.0.9

1.0.9.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

18/02 2017

1.0.8

1.0.8.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

17/02 2017

1.0.7

1.0.7.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab

16/02 2017

1.0.6

1.0.6.0

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

  Sources   Download

MIT

The Requires

 

The Development Requires

by 404lab