PHP code example of hashmatwaziri / laravel-multi-auth-impersonate

1. Go to this page and download the library: Download hashmatwaziri/laravel-multi-auth-impersonate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hashmatwaziri / laravel-multi-auth-impersonate example snippets


return [
 /**
     * The session key used to store the original user id.
     */
    'session_key' => 'impersonated_by',

    /**
     * The session key used to stored the original user guard.
     */
    'session_guard' => 'impersonator_guard',

    /**
     * The session key used to stored what guard is impersonator using.
     */
    'session_guard_using' => 'impersonator_guard_using',

    /**
     * The default impersonator guard used.
     */
    'default_impersonator_guard' => 'web',
];

  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function takeRedirectTo(){

        return url('/after-being-impersonated');
    }

}

  class Employee extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function leaveRedirectTo(){

        return url('/workplace/dashboard');
    }

}

$other_user = App\Student::find(1);
Auth::user()->impersonate($other_user);
// You're now logged as the $other_user

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

Route::multiAuthImpersonate('impersonation');

namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
	// here you can supply an array of guards ex ['web','employee','etc'] so that each can impersonate other
        Route::middleware('web')->group(function (Router $router) {
            $router->multiAuthImpersonate('impersonation');
        });
    }
}

// Where $id is the ID of the user you want impersonate
route('impersonate', $id)

// You should also add `guardName`
route('impersonate', ['id' => $id, 'guardName' => 'admin'])

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


  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;


//    /**
//     * @return bool
//     */
    public function canImpersonate()
    {

        return true ;

    }

}

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

// With the app helper
app('impersonate')
// Dependency Injection
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }


$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();
bash
php artisan vendor:publish --provider="HashmatWaziri\LaravelMultiAuthImpersonate\LaravelMultiAuthImpersonateServiceProvider" --tag="multiAuthImpersonate"