xref: /webtrees/app/Http/RequestHandlers/UsersCleanupAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
12474349cSGreg Roach<?php
22474349cSGreg Roach
32474349cSGreg Roach/**
42474349cSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
62474349cSGreg Roach * This program is free software: you can redistribute it and/or modify
72474349cSGreg Roach * it under the terms of the GNU General Public License as published by
82474349cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
92474349cSGreg Roach * (at your option) any later version.
102474349cSGreg Roach * This program is distributed in the hope that it will be useful,
112474349cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
122474349cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132474349cSGreg Roach * GNU General Public License for more details.
142474349cSGreg 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/>.
162474349cSGreg Roach */
172474349cSGreg Roach
182474349cSGreg Roachdeclare(strict_types=1);
192474349cSGreg Roach
202474349cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
212474349cSGreg Roach
222474349cSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
232474349cSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
242474349cSGreg Roachuse Fisharebest\Webtrees\I18N;
252474349cSGreg Roachuse Fisharebest\Webtrees\Log;
262474349cSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
27748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
282474349cSGreg Roachuse Psr\Http\Message\ResponseInterface;
292474349cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
302474349cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
312474349cSGreg Roach
322474349cSGreg Roachuse function e;
332474349cSGreg Roachuse function redirect;
342474349cSGreg Roachuse function route;
352474349cSGreg Roach
362474349cSGreg Roach/**
372474349cSGreg Roach * Delete old/inactive users.
382474349cSGreg Roach */
392474349cSGreg Roachclass UsersCleanupAction implements RequestHandlerInterface
402474349cSGreg Roach{
4194bb8cffSGreg Roach    private UserService $user_service;
422474349cSGreg Roach
432474349cSGreg Roach    /**
442474349cSGreg Roach     * @param UserService $user_service
452474349cSGreg Roach     */
462474349cSGreg Roach    public function __construct(UserService $user_service)
472474349cSGreg Roach    {
482474349cSGreg Roach        $this->user_service = $user_service;
492474349cSGreg Roach    }
502474349cSGreg Roach
512474349cSGreg Roach    /**
522474349cSGreg Roach     * @param ServerRequestInterface $request
532474349cSGreg Roach     *
542474349cSGreg Roach     * @return ResponseInterface
552474349cSGreg Roach     */
562474349cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
572474349cSGreg Roach    {
58748dbe15SGreg Roach        $delete = Validator::parsedBody($request)->array('delete');
592474349cSGreg Roach
602474349cSGreg Roach        foreach ($delete as $user_id) {
6194bb8cffSGreg Roach            $user = $this->user_service->find((int) $user_id);
622474349cSGreg Roach            if ($user instanceof UserInterface) {
632474349cSGreg Roach                $this->user_service->delete($user);
642474349cSGreg Roach
652474349cSGreg Roach                Log::addAuthenticationLog('Deleted user: ' . $user->userName());
662474349cSGreg Roach
672474349cSGreg Roach                FlashMessages::addMessage(I18N::translate('The user %s has been deleted.', e($user->userName())), 'success');
682474349cSGreg Roach            }
692474349cSGreg Roach        }
702474349cSGreg Roach
712474349cSGreg Roach        $url = route(UsersCleanupPage::class);
722474349cSGreg Roach
732474349cSGreg Roach        return redirect($url);
742474349cSGreg Roach    }
752474349cSGreg Roach}
76