xref: /webtrees/app/Http/RequestHandlers/UsersCleanupPage.php (revision 2474349caa7b2aa11793e6008b000ebb1812e425)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Carbon;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\DatatablesService;
26use Fisharebest\Webtrees\Services\EmailService;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Services\TreeService;
29use Fisharebest\Webtrees\Services\UserService;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34/**
35 * Delete old/inactive users.
36 */
37class UsersCleanupPage implements RequestHandlerInterface
38{
39    use ViewResponseTrait;
40
41    /** @var DatatablesService */
42    private $datatables_service;
43
44    /** @var EmailService */
45    private $email_service;
46
47    /** @var ModuleService */
48    private $module_service;
49
50    /** @var UserService */
51    private $user_service;
52
53    /** @var TreeService */
54    private $tree_service;
55
56    /**
57     * @param DatatablesService $datatables_service
58     * @param EmailService      $email_service
59     * @param ModuleService     $module_service
60     * @param TreeService       $tree_service
61     * @param UserService       $user_service
62     */
63    public function __construct(
64        DatatablesService $datatables_service,
65        EmailService $email_service,
66        ModuleService $module_service,
67        TreeService $tree_service,
68        UserService $user_service
69    ) {
70        $this->datatables_service = $datatables_service;
71        $this->email_service      = $email_service;
72        $this->module_service     = $module_service;
73        $this->tree_service       = $tree_service;
74        $this->user_service       = $user_service;
75    }
76
77    /**
78     * @param ServerRequestInterface $request
79     *
80     * @return ResponseInterface
81     */
82    public function handle(ServerRequestInterface $request): ResponseInterface
83    {
84        $inactive_threshold   = Carbon::now()->subMonths(6)->timestamp;
85        $unverified_threshold = Carbon::now()->subDays(7)->timestamp;
86
87        $inactive_users = $this->user_service->all()
88            ->filter($this->user_service->filterInactive($inactive_threshold))
89            ->sort($this->user_service->sortByLastLogin());
90
91        $unverified_users = $this->user_service->unverified()
92            ->filter($this->user_service->filterInactive($unverified_threshold))
93            ->sort($this->user_service->sortByLastLogin());
94
95        $title = I18N::translate('Delete inactive users');
96
97        $this->layout = 'layouts/administration';
98
99        return $this->viewResponse('admin/users-cleanup', [
100            'title'            => $title,
101            'inactive_users'   => $inactive_users,
102            'unverified_users' => $unverified_users,
103        ]);
104    }
105}
106