CluPilotCloud/app/Http/Controllers/ImpersonationController.php

41 lines
1.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
/**
* Admin impersonation: an admin can log in as a customer's portal account to
* inspect their portal during an incident, then return to the admin session.
*/
class ImpersonationController extends Controller
{
/** Admin-gated: become the customer. */
public function start(Customer $customer): RedirectResponse
{
$user = $customer->ensureUser();
session(['impersonator_id' => Auth::id()]);
Auth::login($user);
return redirect()->route('dashboard');
}
/** Return to the admin session (available while impersonating). */
public function leave(): RedirectResponse
{
$adminId = session()->pull('impersonator_id');
if ($adminId !== null && ($admin = User::query()->find($adminId)) !== null) {
Auth::login($admin);
return redirect()->route('admin.overview');
}
return redirect()->route('dashboard');
}
}