xref: /webtrees/app/Http/RequestHandlers/AccountDelete.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a49d0e3fSGreg Roach * This program is free software: you can redistribute it and/or modify
7a49d0e3fSGreg Roach * it under the terms of the GNU General Public License as published by
8a49d0e3fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a49d0e3fSGreg Roach * (at your option) any later version.
10a49d0e3fSGreg Roach * This program is distributed in the hope that it will be useful,
11a49d0e3fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a49d0e3fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a49d0e3fSGreg Roach * GNU General Public License for more details.
14a49d0e3fSGreg 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/>.
16a49d0e3fSGreg Roach */
17a49d0e3fSGreg Roach
18a49d0e3fSGreg Roachdeclare(strict_types=1);
19a49d0e3fSGreg Roach
20a49d0e3fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a49d0e3fSGreg Roach
22a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\User;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
29a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31a49d0e3fSGreg Roach
32a49d0e3fSGreg Roachuse function redirect;
33a49d0e3fSGreg Roachuse function route;
34a49d0e3fSGreg Roach
35a49d0e3fSGreg Roach/**
36a49d0e3fSGreg Roach * Delete a user account.
37a49d0e3fSGreg Roach */
38a49d0e3fSGreg Roachclass AccountDelete implements RequestHandlerInterface
39a49d0e3fSGreg Roach{
40c4943cffSGreg Roach    private UserService $user_service;
41a49d0e3fSGreg Roach
42a49d0e3fSGreg Roach    /**
43a49d0e3fSGreg Roach     * @param UserService $user_service
44a49d0e3fSGreg Roach     */
45a49d0e3fSGreg Roach    public function __construct(UserService $user_service)
46a49d0e3fSGreg Roach    {
47a49d0e3fSGreg Roach        $this->user_service = $user_service;
48a49d0e3fSGreg Roach    }
49a49d0e3fSGreg Roach
50a49d0e3fSGreg Roach    /**
51a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
52a49d0e3fSGreg Roach     *
53a49d0e3fSGreg Roach     * @return ResponseInterface
54a49d0e3fSGreg Roach     */
55a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
56a49d0e3fSGreg Roach    {
57b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
58b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
59a49d0e3fSGreg Roach
60a49d0e3fSGreg Roach        // An administrator can only be deleted by another administrator
611fe542e9SGreg Roach        if ($user instanceof User && $user->getPreference(UserInterface::PREF_IS_ADMINISTRATOR) !== '1') {
62a49d0e3fSGreg Roach            $this->user_service->delete($user);
63a49d0e3fSGreg Roach            Auth::logout();
64a49d0e3fSGreg Roach        }
65a49d0e3fSGreg Roach
6681bf3221SGreg Roach        return redirect(route(AccountEdit::class, ['tree' => $tree?->name()]));
67a49d0e3fSGreg Roach    }
68a49d0e3fSGreg Roach}
69