2022-09-20 21:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
|
|
class ImpersonationController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2024-01-19 14:27:04 +01:00
|
|
|
$this->middleware('moderator');
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 16:11:21 +02:00
|
|
|
public function impersonate($userId)
|
2024-02-23 11:54:12 +01:00
|
|
|
{
|
2024-04-22 16:11:21 +02:00
|
|
|
$user = User::find($userId);
|
|
|
|
|
if (!$user) {
|
2024-01-19 14:27:04 +01:00
|
|
|
return $this->error([
|
2024-02-23 11:54:12 +01:00
|
|
|
'message' => 'User not found.',
|
2024-01-19 14:27:04 +01:00
|
|
|
]);
|
2024-02-23 11:54:12 +01:00
|
|
|
} elseif ($user->admin) {
|
2024-01-19 14:27:04 +01:00
|
|
|
return $this->error([
|
|
|
|
|
'message' => 'You cannot impersonate an admin.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 14:12:05 +02:00
|
|
|
AdminController::log('Impersonation started', [
|
2024-01-19 14:27:04 +01:00
|
|
|
'from_id' => auth()->id(),
|
|
|
|
|
'from_email' => auth()->user()->email,
|
|
|
|
|
'target_id' => $user->id,
|
|
|
|
|
'target_email' => $user->id,
|
2022-09-20 21:59:52 +02:00
|
|
|
]);
|
|
|
|
|
|
2024-05-06 14:12:05 +02:00
|
|
|
$token = auth()->claims(
|
|
|
|
|
auth()->user()->admin ? [] : [
|
|
|
|
|
'impersonating' => true,
|
|
|
|
|
'impersonator_id' => auth()->id(),
|
|
|
|
|
]
|
|
|
|
|
)->login($user);
|
2024-01-24 17:45:29 +01:00
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
return $this->success([
|
2024-02-23 11:54:12 +01:00
|
|
|
'token' => $token,
|
2025-03-10 10:32:17 +01:00
|
|
|
'token_type' => 'bearer',
|
|
|
|
|
'expires_in' => auth()->getPayload()->get('exp') - time(),
|
2022-09-20 21:59:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|