2017 © Pedro Peláez
 

library laravel-pretend

Symfony style impersonation for the Laravel Framework.

image

scif/laravel-pretend

Symfony style impersonation for the Laravel Framework.

  • Sunday, December 10, 2017
  • by SCIF
  • Repository
  • 2 Watchers
  • 7 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Code Coverage Build Status Scrutinizer Code Quality, (*1)

Impersonate package for the Laravel Framework

На русском, (*2)

What is that?

Laravel has no default impersonation wrapper for low-level methods. This package highly inspired by Symfony impersonation which looks much more flexible rather than several inspected Laravel implementations. Package fully implement GET-parameter-driven behavior. Also, this package does not restrict you in using custom user providers (for instance, if you use Propel), guards and can be used with Twig as view templater. Some ideas inspired by existing impersonation packages for Laravel (:+1: thanks to those authors!)., (*3)

Installation

composer require scif/laravel-pretend, (*4)

Add service provider to config/app.php after Laravel service providers but before your own:, (*5)

php 'providers' => [ … Scif\LaravelPretend\LaravelPretendServiceProvider::class, /* * Application Service Providers... */ … ], (*6)

Add middleware handling impersonation: * to your Kernel class: php protected $middlewareGroups = [ 'web' => [ … Scif\LaravelPretend\Middleware\Impersonate::class, ], This way is most common and covers all cases I can assume. * or by any suitable methods for some especial cases., (*7)

The latest step of installation is a configuring authorization gate. Package bundled with gate called impersonate. This gate checks if your user model implements Scif\LaravelPretend\Interfaces\Impersonable and check method canImpersonate(): bool., (*8)

So your model can looks like:, (*9)

class User extends Authenticatable implements Impersonable
{
…

    public function canImpersonate(): bool
    {
        return $this->isAdmin();
    }
}

:point_up: You can use out of box implementation of this gate or override it in your own AuthServiceProvider. You can override name of gate used to check permissions in configuration as well., (*10)

Configuration

Configuration file can be easily copied to your project by vendor:publish command:, (*11)

php php ./artisan vendor:publish --provider=Scif\\LaravelPretend\\LaravelPretendServiceProvider --tag=config, (*12)

Configuration consist of just two options:, (*13)

return [
    'impersonate' => [
        'user_identifier' => 'email',
        'auth_check' => 'impersonate',
    ]
];
  • user_identifier — this string will be used as name of field using to retrieve user object from user provider (method retrieveByCredentials()). The default value email makes your impersonation urls beauty: ?_switch_user=admin@site.com is much clear rather than ?_switch_user=43. But it's up to you
  • auth_check — this string is a name of Gate used to check ability of user to impersonate. In fact the default Gate could be easily overriden in AuthServiceProvider of your application.

Usage

As mentioned above, this package repeats Symfony style of using GET-parameters to manage impersonation., (*14)

Blade using is pretty straightforward:, (*15)

// generates link with impersonation
{{ route('home', ['_switch_user' => 'admin@site.com']) }}

// exit impersonation
    @if ($app['impersonator']->isImpersonated())
        <a href="{{ route('home', ['_switch_user' => '_exit']) }}">Exit impersonation</a>
    @else
        <a href="{{ route('logout') }}">Logout</a>
    @endif

And here is a simple example using in twig:, (*16)

// generates link with impersonation
{{ route('home', {'_switch_user': 'admin@site.com'}) }}

// more advance usage
                {% if auth_user() %}
                    {% if app.impersonator.impersonated %}
                        <a class="btn btn-default" href="{{ route('home', {'_switch_user': '_exit'}) }}">Exit impersonation</a>
                    {% else %}
                        <form action="{{ route('logout') }}" method="post">
                            {{ csrf_field() }}
                            <button class="btn btn-default">Logout</button>
                        </form>
                    {% endif %}
                {% endif %}

Events

On entering and exitting impersontaion this package raises events: Scif\LaravelPretend\Event\Impersontated, Scif\LaravelPretend\Event\Unimpersontated. Name of events is their fully qualified class names, so simplest event listener will looks like:, (*17)

use Scif\LaravelPretend\Event\Impersontated;
…
    Event::listen(Impersonated::class, function (Impersonated $event) {
        //
    });

Forbid impersonation

You can use bundled ForbidImpersonation middleware to forbid using of impersonation for some route groups, routes or controllers., (*18)

PHP7? Ugh! Wtf??

Yes, PHP7 is awesome! So, if you want to use it with PHP5 — create an issue and I will think about a separate branch or other suitable solution., (*19)

Contribution

Any type of contributions is highly appreciated. Don't be a shy — help this project become even better!, (*20)

The Versions

10/12 2017

dev-master

9999999-dev

Symfony style impersonation for the Laravel Framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel impersonate symfony-style

18/07 2017

v1.1.1

1.1.1.0

Symfony style impersonation for the Laravel Framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel impersonate symfony-style

14/04 2017

v1.1

1.1.0.0

Symfony style impersonation for the Laravel Framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel impersonate symfony-style

02/04 2017

v1.0.1

1.0.1.0

Symfony style impersonation for the Laravel Framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel impersonate symfony-style

25/03 2017

v1.0

1.0.0.0

Symfony style impersonation for the Laravel Framework.

  Sources   Download

MIT

The Requires

 

laravel impersonate symfony-style