xref: /webtrees/app/Http/RequestHandlers/LoginPage.php (revision 5fb051e95bc3d8a26af94d588bd85bf2d37f583c)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5d403609dSGreg Roach * Copyright (C) 2019 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
15d403609dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16d403609dSGreg Roach */
17fcfa147eSGreg Roach
18d403609dSGreg Roachdeclare(strict_types=1);
19d403609dSGreg Roach
20d403609dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d403609dSGreg Roach
22*5fb051e9SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2390a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
24d403609dSGreg Roachuse Fisharebest\Webtrees\Http\Controllers\AbstractBaseController;
25d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
26*5fb051e9SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
27d403609dSGreg Roachuse Fisharebest\Webtrees\Site;
28a91af26aSGreg Roachuse Fisharebest\Webtrees\Tree;
29d403609dSGreg Roachuse Fisharebest\Webtrees\User;
30d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
31d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32d403609dSGreg Roach
3390a2f718SGreg Roachuse function assert;
3490a2f718SGreg Roach
35d403609dSGreg Roach/**
36d403609dSGreg Roach * Show a login form.
37d403609dSGreg Roach */
38d403609dSGreg Roachclass LoginPage extends AbstractBaseController
39d403609dSGreg Roach{
40*5fb051e9SGreg Roach    /** @var TreeService */
41*5fb051e9SGreg Roach    private $tree_service;
42*5fb051e9SGreg Roach
43*5fb051e9SGreg Roach    /**
44*5fb051e9SGreg Roach     * LoginPage constructor.
45*5fb051e9SGreg Roach     *
46*5fb051e9SGreg Roach     * @param TreeService $tree_service
47*5fb051e9SGreg Roach     */
48*5fb051e9SGreg Roach    public function __construct(TreeService $tree_service)
49*5fb051e9SGreg Roach    {
50*5fb051e9SGreg Roach        $this->tree_service = $tree_service;
51*5fb051e9SGreg Roach    }
52*5fb051e9SGreg Roach
53d403609dSGreg Roach    /**
54d403609dSGreg Roach     * @param ServerRequestInterface $request
55d403609dSGreg Roach     *
56d403609dSGreg Roach     * @return ResponseInterface
57d403609dSGreg Roach     */
58d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
59d403609dSGreg Roach    {
6090a2f718SGreg Roach        $locale = $request->getAttribute('locale');
6190a2f718SGreg Roach        assert($locale instanceof LocaleInterface);
6290a2f718SGreg Roach
63d403609dSGreg Roach        $tree = $request->getAttribute('tree');
64d403609dSGreg Roach        $user = $request->getAttribute('user');
65d403609dSGreg Roach
66d403609dSGreg Roach        // Already logged in?
67d403609dSGreg Roach        if ($user instanceof User) {
68a91af26aSGreg Roach            return redirect(route('user-page', ['tree' => $tree instanceof Tree ? $tree->name() : '']));
69d403609dSGreg Roach        }
70d403609dSGreg Roach
71d403609dSGreg Roach        $url      = $request->getQueryParams()['url'] ?? '';
72d403609dSGreg Roach        $username = $request->getQueryParams()['username'] ?? '';
73d403609dSGreg Roach
74*5fb051e9SGreg Roach        // No tree?  perhaps we came here from a page without one.
75*5fb051e9SGreg Roach        if ($tree === null) {
76*5fb051e9SGreg Roach            $default = Site::getPreference('DEFAULT_GEDCOM');
77*5fb051e9SGreg Roach            $tree = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
78*5fb051e9SGreg Roach
79*5fb051e9SGreg Roach            if ($tree instanceof Tree) {
80*5fb051e9SGreg Roach                return redirect(route(self::class, ['tree' => $tree->name(), 'url' => $url]));
81*5fb051e9SGreg Roach            }
82*5fb051e9SGreg Roach        }
83*5fb051e9SGreg Roach
84d403609dSGreg Roach        $title = I18N::translate('Sign in');
85d403609dSGreg Roach
86d403609dSGreg Roach        switch (Site::getPreference('WELCOME_TEXT_AUTH_MODE')) {
87320f6a24SGreg Roach            case '1':
88d403609dSGreg Roach            default:
89d403609dSGreg Roach                $welcome = I18N::translate('Anyone with a user account can access this website.');
90d403609dSGreg Roach                break;
91320f6a24SGreg Roach            case '2':
92d403609dSGreg Roach                $welcome = I18N::translate('You need to be an authorized user to access this website.');
93d403609dSGreg Roach                break;
94320f6a24SGreg Roach            case '3':
95d403609dSGreg Roach                $welcome = I18N::translate('You need to be a family member to access this website.');
96d403609dSGreg Roach                break;
97320f6a24SGreg Roach            case '4':
9890a2f718SGreg Roach                $welcome = Site::getPreference('WELCOME_TEXT_AUTH_MODE_' . $locale->languageTag());
99d403609dSGreg Roach                break;
100d403609dSGreg Roach        }
101d403609dSGreg Roach
102d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') === '1') {
103d403609dSGreg Roach            $welcome .= ' ' . I18N::translate('You can apply for an account using the link below.');
104d403609dSGreg Roach        }
105d403609dSGreg Roach
106d403609dSGreg Roach        $can_register = Site::getPreference('USE_REGISTRATION_MODULE') === '1';
107d403609dSGreg Roach
108d403609dSGreg Roach        return $this->viewResponse('login-page', [
109d403609dSGreg Roach            'can_register' => $can_register,
110d403609dSGreg Roach            'title'        => $title,
111d403609dSGreg Roach            'url'          => $url,
1120c0910bfSGreg Roach            'tree'         => $tree,
113d403609dSGreg Roach            'username'     => $username,
114d403609dSGreg Roach            'welcome'      => $welcome,
115d403609dSGreg Roach        ]);
116d403609dSGreg Roach    }
117d403609dSGreg Roach}
118