xref: /webtrees/app/Http/RequestHandlers/UsersCleanupAction.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
12474349cSGreg Roach<?php
22474349cSGreg Roach
32474349cSGreg Roach/**
42474349cSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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;
272474349cSGreg Roachuse Psr\Http\Message\ResponseInterface;
282474349cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
292474349cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
302474349cSGreg Roach
312474349cSGreg Roachuse function e;
322474349cSGreg Roachuse function redirect;
332474349cSGreg Roachuse function route;
342474349cSGreg Roach
352474349cSGreg Roach/**
362474349cSGreg Roach * Delete old/inactive users.
372474349cSGreg Roach */
382474349cSGreg Roachclass UsersCleanupAction implements RequestHandlerInterface
392474349cSGreg Roach{
402474349cSGreg Roach    /** @var UserService */
412474349cSGreg Roach    private $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    {
58b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
59b46c87bdSGreg Roach
60b46c87bdSGreg Roach        $delete = $params['delete'] ?? [];
612474349cSGreg Roach
622474349cSGreg Roach        foreach ($delete as $user_id) {
632474349cSGreg Roach            $user = $this->user_service->find($user_id);
642474349cSGreg Roach            if ($user instanceof UserInterface) {
652474349cSGreg Roach                $this->user_service->delete($user);
662474349cSGreg Roach
672474349cSGreg Roach                Log::addAuthenticationLog('Deleted user: ' . $user->userName());
682474349cSGreg Roach
692474349cSGreg Roach                FlashMessages::addMessage(I18N::translate('The user %s has been deleted.', e($user->userName())), 'success');
702474349cSGreg Roach            }
712474349cSGreg Roach        }
722474349cSGreg Roach
732474349cSGreg Roach        $url = route(UsersCleanupPage::class);
742474349cSGreg Roach
752474349cSGreg Roach        return redirect($url);
762474349cSGreg Roach    }
772474349cSGreg Roach}
78