xref: /webtrees/app/Http/RequestHandlers/LoginPage.php (revision 02c03deecc18e2785ad2470bdc1c4d2d6eab14ce)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d403609dSGreg Roach * This program is free software: you can redistribute it and/or modify
7d403609dSGreg Roach * it under the terms of the GNU General Public License as published by
8d403609dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d403609dSGreg Roach * (at your option) any later version.
10d403609dSGreg Roach * This program is distributed in the hope that it will be useful,
11d403609dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d403609dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d403609dSGreg Roach * GNU General Public License for more details.
14d403609dSGreg 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/>.
16d403609dSGreg Roach */
17fcfa147eSGreg Roach
18d403609dSGreg Roachdeclare(strict_types=1);
19d403609dSGreg Roach
20d403609dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d403609dSGreg Roach
2298b7e8b3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
23d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
245fb051e9SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
25d403609dSGreg Roachuse Fisharebest\Webtrees\Site;
26a91af26aSGreg Roachuse Fisharebest\Webtrees\Tree;
27d403609dSGreg Roachuse Fisharebest\Webtrees\User;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
30d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3198b7e8b3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32d403609dSGreg Roach
33d403609dSGreg Roach/**
34d403609dSGreg Roach * Show a login form.
35d403609dSGreg Roach */
3698b7e8b3SGreg Roachclass LoginPage implements RequestHandlerInterface
37d403609dSGreg Roach{
3898b7e8b3SGreg Roach    use ViewResponseTrait;
3998b7e8b3SGreg Roach
40c4943cffSGreg Roach    private TreeService $tree_service;
415fb051e9SGreg Roach
425fb051e9SGreg Roach    /**
435fb051e9SGreg Roach     * @param TreeService $tree_service
445fb051e9SGreg Roach     */
455fb051e9SGreg Roach    public function __construct(TreeService $tree_service)
465fb051e9SGreg Roach    {
475fb051e9SGreg Roach        $this->tree_service = $tree_service;
485fb051e9SGreg Roach    }
495fb051e9SGreg Roach
50d403609dSGreg Roach    /**
51d403609dSGreg Roach     * @param ServerRequestInterface $request
52d403609dSGreg Roach     *
53d403609dSGreg Roach     * @return ResponseInterface
54d403609dSGreg Roach     */
55d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
56d403609dSGreg Roach    {
57b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
58b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
59d403609dSGreg Roach
60d403609dSGreg Roach        // Already logged in?
61d403609dSGreg Roach        if ($user instanceof User) {
628e0e1b25SGreg Roach            return redirect(route(UserPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : '']));
63d403609dSGreg Roach        }
64d403609dSGreg Roach
659f0bdfcdSGreg Roach        $url      = Validator::queryParams($request)->isLocalUrl()->string('url', route(HomePage::class));
66*02c03deeSGreg Roach        $username = Validator::queryParams($request)->string('username', '');
67d403609dSGreg Roach
685fb051e9SGreg Roach        // No tree?  perhaps we came here from a page without one.
695fb051e9SGreg Roach        if ($tree === null) {
705fb051e9SGreg Roach            $default = Site::getPreference('DEFAULT_GEDCOM');
715fb051e9SGreg Roach            $tree    = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
725fb051e9SGreg Roach
735fb051e9SGreg Roach            if ($tree instanceof Tree) {
745fb051e9SGreg Roach                return redirect(route(self::class, ['tree' => $tree->name(), 'url' => $url]));
755fb051e9SGreg Roach            }
765fb051e9SGreg Roach        }
775fb051e9SGreg Roach
78d403609dSGreg Roach        $title = I18N::translate('Sign in');
79d403609dSGreg Roach
80d403609dSGreg Roach        switch (Site::getPreference('WELCOME_TEXT_AUTH_MODE')) {
81320f6a24SGreg Roach            case '1':
82d403609dSGreg Roach            default:
83d403609dSGreg Roach                $welcome = I18N::translate('Anyone with a user account can access this website.');
84d403609dSGreg Roach                break;
85320f6a24SGreg Roach            case '2':
86d403609dSGreg Roach                $welcome = I18N::translate('You need to be an authorized user to access this website.');
87d403609dSGreg Roach                break;
88320f6a24SGreg Roach            case '3':
89d403609dSGreg Roach                $welcome = I18N::translate('You need to be a family member to access this website.');
90d403609dSGreg Roach                break;
91320f6a24SGreg Roach            case '4':
9265cf5706SGreg Roach                $welcome = Site::getPreference('WELCOME_TEXT_AUTH_MODE_' . I18N::languageTag());
93d403609dSGreg Roach                break;
94d403609dSGreg Roach        }
95d403609dSGreg Roach
96d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') === '1') {
97b33958b8SGreg Roach            $welcome .= '<br>' . I18N::translate('You can apply for an account using the link below.');
98d403609dSGreg Roach        }
99d403609dSGreg Roach
100d403609dSGreg Roach        $can_register = Site::getPreference('USE_REGISTRATION_MODULE') === '1';
101d403609dSGreg Roach
102d403609dSGreg Roach        return $this->viewResponse('login-page', [
103d403609dSGreg Roach            'can_register' => $can_register,
104d403609dSGreg Roach            'title'        => $title,
105d403609dSGreg Roach            'url'          => $url,
1060c0910bfSGreg Roach            'tree'         => $tree,
107d403609dSGreg Roach            'username'     => $username,
108d403609dSGreg Roach            'welcome'      => $welcome,
109d403609dSGreg Roach        ]);
110d403609dSGreg Roach    }
111d403609dSGreg Roach}
112