. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Log; use Fisharebest\Webtrees\Services\UserService; use Fisharebest\Webtrees\Session; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use function response; /** * Masquerade as another user, for testing and administration. */ class Masquerade implements RequestHandlerInterface { /** @var UserService */ private $user_service; /** * @param UserService $user_service */ public function __construct(UserService $user_service) { $this->user_service = $user_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $user_id = (int) $request->getAttribute('user_id'); $user = $this->user_service->find($user_id); if ($user === null) { throw new NotFoundHttpException('User ID ' . $user_id . ' not found'); } if ($request->getAttribute('user')->id() !== $user_id) { Log::addAuthenticationLog('Masquerade as user: ' . $user->userName()); Auth::login($user); Session::put('masquerade', '1'); } return response(); } }