xref: /webtrees/app/Http/RequestHandlers/Masquerade.php (revision 81b729d3a9a6e0a0e8b96285d1ad7955d2d0c659)
1040e7dbaSGreg Roach<?php
2040e7dbaSGreg Roach
3040e7dbaSGreg Roach/**
4040e7dbaSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6040e7dbaSGreg Roach * This program is free software: you can redistribute it and/or modify
7040e7dbaSGreg Roach * it under the terms of the GNU General Public License as published by
8040e7dbaSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9040e7dbaSGreg Roach * (at your option) any later version.
10040e7dbaSGreg Roach * This program is distributed in the hope that it will be useful,
11040e7dbaSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12040e7dbaSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13040e7dbaSGreg Roach * GNU General Public License for more details.
14040e7dbaSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16040e7dbaSGreg Roach */
17040e7dbaSGreg Roach
18040e7dbaSGreg Roachdeclare(strict_types=1);
19040e7dbaSGreg Roach
20040e7dbaSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21040e7dbaSGreg Roach
22040e7dbaSGreg Roachuse Fisharebest\Webtrees\Auth;
23*81b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
24040e7dbaSGreg Roachuse Fisharebest\Webtrees\Log;
25040e7dbaSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26040e7dbaSGreg Roachuse Fisharebest\Webtrees\Session;
27040e7dbaSGreg Roachuse Psr\Http\Message\ResponseInterface;
28040e7dbaSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29040e7dbaSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30040e7dbaSGreg Roach
31040e7dbaSGreg Roachuse function response;
32040e7dbaSGreg Roach
33040e7dbaSGreg Roach/**
34040e7dbaSGreg Roach * Masquerade as another user, for testing and administration.
35040e7dbaSGreg Roach */
36040e7dbaSGreg Roachclass Masquerade implements RequestHandlerInterface
37040e7dbaSGreg Roach{
38c4943cffSGreg Roach    private UserService $user_service;
39040e7dbaSGreg Roach
40040e7dbaSGreg Roach    /**
41040e7dbaSGreg Roach     * @param UserService $user_service
42040e7dbaSGreg Roach     */
43040e7dbaSGreg Roach    public function __construct(UserService $user_service)
44040e7dbaSGreg Roach    {
45040e7dbaSGreg Roach        $this->user_service = $user_service;
46040e7dbaSGreg Roach    }
47040e7dbaSGreg Roach
48040e7dbaSGreg Roach    /**
49040e7dbaSGreg Roach     * @param ServerRequestInterface $request
50040e7dbaSGreg Roach     *
51040e7dbaSGreg Roach     * @return ResponseInterface
52040e7dbaSGreg Roach     */
53040e7dbaSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
54040e7dbaSGreg Roach    {
55040e7dbaSGreg Roach        $user_id = (int) $request->getAttribute('user_id');
56040e7dbaSGreg Roach        $user    = $this->user_service->find($user_id);
57040e7dbaSGreg Roach
58040e7dbaSGreg Roach        if ($user === null) {
59d501c45dSGreg Roach            throw new HttpNotFoundException('User ID ' . $user_id . ' not found');
60040e7dbaSGreg Roach        }
61040e7dbaSGreg Roach
62040e7dbaSGreg Roach        if ($request->getAttribute('user')->id() !== $user_id) {
63040e7dbaSGreg Roach            Log::addAuthenticationLog('Masquerade as user: ' . $user->userName());
64040e7dbaSGreg Roach            Auth::login($user);
65040e7dbaSGreg Roach            Session::put('masquerade', '1');
66040e7dbaSGreg Roach        }
67040e7dbaSGreg Roach
68040e7dbaSGreg Roach        return response();
69040e7dbaSGreg Roach    }
70040e7dbaSGreg Roach}
71