xref: /webtrees/app/Http/RequestHandlers/DeleteUser.php (revision d11be7027e34e3121be11cc025421873364403f9)
1a0801ffbSGreg Roach<?php
23976b470SGreg Roach
3a0801ffbSGreg Roach/**
4a0801ffbSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a0801ffbSGreg Roach * This program is free software: you can redistribute it and/or modify
7a0801ffbSGreg Roach * it under the terms of the GNU General Public License as published by
8a0801ffbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a0801ffbSGreg Roach * (at your option) any later version.
10a0801ffbSGreg Roach * This program is distributed in the hope that it will be useful,
11a0801ffbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a0801ffbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a0801ffbSGreg Roach * GNU General Public License for more details.
14a0801ffbSGreg 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/>.
16a0801ffbSGreg Roach */
17fcfa147eSGreg Roach
18a0801ffbSGreg Roachdeclare(strict_types=1);
19a0801ffbSGreg Roach
20a0801ffbSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a0801ffbSGreg Roach
22a0801ffbSGreg Roachuse Fisharebest\Webtrees\Auth;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
25a0801ffbSGreg Roachuse Fisharebest\Webtrees\Log;
26a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
27a0801ffbSGreg Roachuse Psr\Http\Message\ResponseInterface;
28a0801ffbSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29a0801ffbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
303976b470SGreg Roach
31a0801ffbSGreg Roachuse function response;
32a0801ffbSGreg Roach
33a0801ffbSGreg Roach/**
34a0801ffbSGreg Roach * Delete a user.
35a0801ffbSGreg Roach */
361d1f373cSGreg Roachclass DeleteUser implements RequestHandlerInterface
37a0801ffbSGreg Roach{
38c4943cffSGreg Roach    private UserService $user_service;
39a0801ffbSGreg Roach
40a0801ffbSGreg Roach    /**
41a0801ffbSGreg Roach     * @param UserService $user_service
42a0801ffbSGreg Roach     */
43a0801ffbSGreg Roach    public function __construct(UserService $user_service)
44a0801ffbSGreg Roach    {
45a0801ffbSGreg Roach        $this->user_service = $user_service;
46a0801ffbSGreg Roach    }
47a0801ffbSGreg Roach
48a0801ffbSGreg Roach    /**
49a0801ffbSGreg Roach     * @param ServerRequestInterface $request
50a0801ffbSGreg Roach     *
51a0801ffbSGreg Roach     * @return ResponseInterface
52a0801ffbSGreg Roach     */
53a0801ffbSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
54a0801ffbSGreg Roach    {
55a2ae7ae7SGreg Roach        $user_id = (int) $request->getAttribute('user_id');
56a0801ffbSGreg Roach
57a0801ffbSGreg Roach        $user = $this->user_service->find($user_id);
58a0801ffbSGreg Roach
59a0801ffbSGreg Roach        if ($user === null) {
60d501c45dSGreg Roach            throw new HttpNotFoundException('User ID ' . $user_id . ' not found');
61a0801ffbSGreg Roach        }
62a0801ffbSGreg Roach
63a0801ffbSGreg Roach        if (Auth::isAdmin($user)) {
64d501c45dSGreg Roach            throw new HttpAccessDeniedException('Cannot delete an administrator');
65a0801ffbSGreg Roach        }
66a0801ffbSGreg Roach
67a0801ffbSGreg Roach        Log::addAuthenticationLog('Deleted user: ' . $user->userName());
68a0801ffbSGreg Roach        $this->user_service->delete($user);
69a0801ffbSGreg Roach
701d1f373cSGreg Roach        return response();
71a0801ffbSGreg Roach    }
72a0801ffbSGreg Roach}
73