xref: /webtrees/app/Http/RequestHandlers/LoginPage.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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;
28d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
29d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3098b7e8b3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31d403609dSGreg Roach
32d403609dSGreg Roach/**
33d403609dSGreg Roach * Show a login form.
34d403609dSGreg Roach */
3598b7e8b3SGreg Roachclass LoginPage implements RequestHandlerInterface
36d403609dSGreg Roach{
3798b7e8b3SGreg Roach    use ViewResponseTrait;
3898b7e8b3SGreg Roach
395fb051e9SGreg Roach    /** @var TreeService */
405fb051e9SGreg Roach    private $tree_service;
415fb051e9SGreg Roach
425fb051e9SGreg Roach    /**
435fb051e9SGreg Roach     * LoginPage constructor.
445fb051e9SGreg Roach     *
455fb051e9SGreg Roach     * @param TreeService $tree_service
465fb051e9SGreg Roach     */
475fb051e9SGreg Roach    public function __construct(TreeService $tree_service)
485fb051e9SGreg Roach    {
495fb051e9SGreg Roach        $this->tree_service = $tree_service;
505fb051e9SGreg Roach    }
515fb051e9SGreg Roach
52d403609dSGreg Roach    /**
53d403609dSGreg Roach     * @param ServerRequestInterface $request
54d403609dSGreg Roach     *
55d403609dSGreg Roach     * @return ResponseInterface
56d403609dSGreg Roach     */
57d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
58d403609dSGreg Roach    {
59d403609dSGreg Roach        $tree = $request->getAttribute('tree');
60d403609dSGreg Roach        $user = $request->getAttribute('user');
61d403609dSGreg Roach
62d403609dSGreg Roach        // Already logged in?
63d403609dSGreg Roach        if ($user instanceof User) {
648e0e1b25SGreg Roach            return redirect(route(UserPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : '']));
65d403609dSGreg Roach        }
66d403609dSGreg Roach
67d403609dSGreg Roach        $url      = $request->getQueryParams()['url'] ?? '';
68d403609dSGreg Roach        $username = $request->getQueryParams()['username'] ?? '';
69d403609dSGreg Roach
705fb051e9SGreg Roach        // No tree?  perhaps we came here from a page without one.
715fb051e9SGreg Roach        if ($tree === null) {
725fb051e9SGreg Roach            $default = Site::getPreference('DEFAULT_GEDCOM');
735fb051e9SGreg Roach            $tree    = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
745fb051e9SGreg Roach
755fb051e9SGreg Roach            if ($tree instanceof Tree) {
765fb051e9SGreg Roach                return redirect(route(self::class, ['tree' => $tree->name(), 'url' => $url]));
775fb051e9SGreg Roach            }
785fb051e9SGreg Roach        }
795fb051e9SGreg Roach
80d403609dSGreg Roach        $title = I18N::translate('Sign in');
81d403609dSGreg Roach
82d403609dSGreg Roach        switch (Site::getPreference('WELCOME_TEXT_AUTH_MODE')) {
83320f6a24SGreg Roach            case '1':
84d403609dSGreg Roach            default:
85d403609dSGreg Roach                $welcome = I18N::translate('Anyone with a user account can access this website.');
86d403609dSGreg Roach                break;
87320f6a24SGreg Roach            case '2':
88d403609dSGreg Roach                $welcome = I18N::translate('You need to be an authorized user to access this website.');
89d403609dSGreg Roach                break;
90320f6a24SGreg Roach            case '3':
91d403609dSGreg Roach                $welcome = I18N::translate('You need to be a family member to access this website.');
92d403609dSGreg Roach                break;
93320f6a24SGreg Roach            case '4':
9465cf5706SGreg Roach                $welcome = Site::getPreference('WELCOME_TEXT_AUTH_MODE_' . I18N::languageTag());
95d403609dSGreg Roach                break;
96d403609dSGreg Roach        }
97d403609dSGreg Roach
98d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') === '1') {
99d403609dSGreg Roach            $welcome .= ' ' . I18N::translate('You can apply for an account using the link below.');
100d403609dSGreg Roach        }
101d403609dSGreg Roach
102d403609dSGreg Roach        $can_register = Site::getPreference('USE_REGISTRATION_MODULE') === '1';
103d403609dSGreg Roach
104d403609dSGreg Roach        return $this->viewResponse('login-page', [
105d403609dSGreg Roach            'can_register' => $can_register,
106d403609dSGreg Roach            'title'        => $title,
107d403609dSGreg Roach            'url'          => $url,
1080c0910bfSGreg Roach            'tree'         => $tree,
109d403609dSGreg Roach            'username'     => $username,
110d403609dSGreg Roach            'welcome'      => $welcome,
111d403609dSGreg Roach        ]);
112d403609dSGreg Roach    }
113d403609dSGreg Roach}
114