https://github.com/404labfr/laravel-impersonate
https://stackoverflow.com/questions/45049601/php-the-requested-php-extension-bcmath-is-missing-from-your-system
<?php
1)
composer require lab404/laravel-impersonate
2)
// ========== /config/app.php in providers array==============
Lab404\Impersonate\ImpersonateServiceProvider::class,
// ======= ROUTES ============
// ADMIN IMPERSONATE USERS
Route::get('admin/impersonate', 'ImpersonateController@index')->name('impersonate.index');
Route::get('admin/impersonate/{user_id}', 'ImpersonateController@impersonate')->name('impersonate');
Route::get('admin/impersonate_leave', 'ImpersonateController@impersonate_leave')->name('impersonate_leave');
// ========== User MODEL =========
use Lab404\Impersonate\Models\Impersonate ; // Namespace
use Impersonate; // inside controller
// ========= IMPERSONATE CONTROLLER =========
namespace App\Http\Controllers;
use Auth;
use Session;
use App\User;
use Illuminate\Http\Request;
class ImpersonateController extends Controller
{
/*_____________________________________________________
CONSTRUCTOR - APPLY ADMIN MIDDLEWARE FOR THIS CLASS
-----------------------------------------------------*/
public function __construct() {
$this->middleware('admin') ;
}
/*________________________________________________
LIST OF USERS TO IMPERSONATE
------------------------------------------------*/
public function index() {
$users = User::where('id', '!=', Auth::id() )->get();
return view('admin.impersonate.index')->with('users' ,$users) ;
}
/*________________________________________________
ADMIN IMPERSONATE USERS
------------------------------------------------*/
public function impersonate ($user_id ) {
$user = User::find($user_id);
Auth::user()->impersonate($user) ;
Session::flash('info' , 'Impersonating: '. $user->name ) ;
return redirect()->back() ;
}
/*________________________________________________
ADMIN IMPERSONATE LEAVE
------------------------------------------------*/
public function impersonate_leave() {
Auth::user()->leaveImpersonation() ;
Session::flash('success', 'Back to Admin Account') ;
return redirect()->back() ;
}
}
// ============ IMPERSONATOR VIEW =============
@forelse($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>
<span><img src="{{ url('/profile') . '/' . $user->profile }}" class="leaderboard_image"></span>
<span>{{ $user->username }}</span>
</td>
<td>{{ $user->email }}</td>
<td>{{ $user->admin }}</td>
<td>{{ $user->active }}</td>
<td>
<a href="{{ route('impersonate', ['user_id' => $user->id ]) }}" class="btn btn-md btn-default"><i class="fa fa-user-secret" aria-hidden="true"></i> Impersonate this user</a>
</td>
</tr>
@empty
<tr>
<th colspan="5" class="text-center">No Users yet</th>
</tr>
@endforelse
// ======== AUTH WITH IMPERSONATION ==========
<ul class="nav navbar-nav">
<!-- Authentication Links -->
@if (Auth::guest())
<li><a href="{{ route('login') }}">Login</a></li>
<li><a href="{{ route('register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<img src="{{ url('/profile') . '/' . Auth::user()->profile }}" class="user-image" alt="User Image">
<span class="hidden-xs">{{ Auth::user()->name }} <i class="fa fa-caret-square-o-down" aria-hidden="true"></i></span>
</a>
<ul class="dropdown-menu" role="menu">
@if( session('impersonated_by') )
<li>
<a class="dropdown-toggle" href="{{ route('impersonate_leave') }}">
Back to Admin Account
</a>
</li>
@endif
<li>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
<i class="fa fa-sign-out" aria-hidden="true"></i> Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
@endif
</ul>