1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Exception; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Carbon; 25use Fisharebest\Webtrees\Contracts\UserInterface; 26use Fisharebest\Webtrees\FlashMessages; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Log; 29use Fisharebest\Webtrees\Services\UpgradeService; 30use Fisharebest\Webtrees\Services\UserService; 31use Fisharebest\Webtrees\Session; 32use Fisharebest\Webtrees\Tree; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\RequestHandlerInterface; 36 37use function route; 38 39/** 40 * Perform a login. 41 */ 42class LoginAction implements RequestHandlerInterface 43{ 44 private UpgradeService $upgrade_service; 45 46 private UserService $user_service; 47 48 /** 49 * LoginController constructor. 50 * 51 * @param UpgradeService $upgrade_service 52 * @param UserService $user_service 53 */ 54 public function __construct(UpgradeService $upgrade_service, UserService $user_service) 55 { 56 $this->upgrade_service = $upgrade_service; 57 $this->user_service = $user_service; 58 } 59 60 /** 61 * Perform a login. 62 * 63 * @param ServerRequestInterface $request 64 * 65 * @return ResponseInterface 66 */ 67 public function handle(ServerRequestInterface $request): ResponseInterface 68 { 69 $tree = $request->getAttribute('tree'); 70 71 $params = (array) $request->getParsedBody(); 72 73 $username = $params['username']; 74 $password = $params['password']; 75 $url = $params['url']; 76 77 try { 78 $this->doLogin($username, $password); 79 80 if (Auth::isAdmin() && $this->upgrade_service->isUpgradeAvailable()) { 81 FlashMessages::addMessage(I18N::translate('A new version of webtrees is available.') . ' <a class="alert-link" href="' . e(route(UpgradeWizardPage::class)) . '">' . I18N::translate('Upgrade to webtrees %s.', '<span dir="ltr">' . $this->upgrade_service->latestVersion() . '</span>') . '</a>'); 82 } 83 84 // Redirect to the target URL 85 $url = $url ?: route(HomePage::class); 86 87 return redirect($url); 88 } catch (Exception $ex) { 89 // Failed to log in. 90 FlashMessages::addMessage($ex->getMessage(), 'danger'); 91 92 return redirect(route(LoginPage::class, [ 93 'tree' => $tree instanceof Tree ? $tree->name() : null, 94 'username' => $username, 95 'url' => $url, 96 ])); 97 } 98 } 99 100 /** 101 * Log in, if we can. Throw an exception, if we can't. 102 * 103 * @param string $username 104 * @param string $password 105 * 106 * @return void 107 * @throws Exception 108 */ 109 private function doLogin(string $username, string $password): void 110 { 111 if ($_COOKIE === []) { 112 Log::addAuthenticationLog('Login failed (no session cookies): ' . $username); 113 throw new Exception(I18N::translate('You cannot sign in because your browser does not accept cookies.')); 114 } 115 116 $user = $this->user_service->findByIdentifier($username); 117 118 if ($user === null) { 119 Log::addAuthenticationLog('Login failed (no such user/email): ' . $username); 120 throw new Exception(I18N::translate('The username or password is incorrect.')); 121 } 122 123 if (!$user->checkPassword($password)) { 124 Log::addAuthenticationLog('Login failed (incorrect password): ' . $username); 125 throw new Exception(I18N::translate('The username or password is incorrect.')); 126 } 127 128 if ($user->getPreference(UserInterface::PREF_IS_EMAIL_VERIFIED) !== '1') { 129 Log::addAuthenticationLog('Login failed (not verified by user): ' . $username); 130 throw new Exception(I18N::translate('This account has not been verified. Please check your email for a verification message.')); 131 } 132 133 if ($user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') { 134 Log::addAuthenticationLog('Login failed (not approved by admin): ' . $username); 135 throw new Exception(I18N::translate('This account has not been approved. Please wait for an administrator to approve it.')); 136 } 137 138 Auth::login($user); 139 Log::addAuthenticationLog('Login: ' . Auth::user()->userName() . '/' . Auth::user()->realName()); 140 Auth::user()->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, (string) Carbon::now()->unix()); 141 142 Session::put('language', Auth::user()->getPreference(UserInterface::PREF_LANGUAGE)); 143 Session::put('theme', Auth::user()->getPreference(UserInterface::PREF_THEME)); 144 I18N::init(Auth::user()->getPreference(UserInterface::PREF_LANGUAGE)); 145 } 146} 147