|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * webtrees: online genealogy |
| 5 | + * Copyright (C) 2024 webtrees development team |
| 6 | + * <http://webtrees.net> |
| 7 | + * |
| 8 | + * Fancy Research Links (webtrees custom module): |
| 9 | + * Copyright (C) 2022 Carmen Just |
| 10 | + * <https://justcarmen.nl> |
| 11 | + * |
| 12 | + * OAuth2Client (webtrees custom module): |
| 13 | + * Copyright (C) 2024 Markus Hemprich |
| 14 | + * <http://www.familienforschung-hemprich.de> |
| 15 | + * |
| 16 | + * This program is free software: you can redistribute it and/or modify |
| 17 | + * it under the terms of the GNU General Public License as published by |
| 18 | + * the Free Software Foundation, either version 3 of the License, or |
| 19 | + * (at your option) any later version. |
| 20 | + * This program is distributed in the hope that it will be useful, |
| 21 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | + * GNU General Public License for more details. |
| 24 | + * You should have received a copy of the GNU General Public License |
| 25 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 26 | + * |
| 27 | + * |
| 28 | + * OAuth2-Client |
| 29 | + * |
| 30 | + * A weebtrees(https://webtrees.net) 2.1 custom module to implement an OAuth2 client |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +declare(strict_types=1); |
| 35 | + |
| 36 | +namespace Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers; |
| 37 | + |
| 38 | +use Fisharebest\Webtrees\FlashMessages; |
| 39 | +use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; |
| 40 | +use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage; |
| 41 | +use Fisharebest\Webtrees\Http\RequestHandlers\RegisterAction; |
| 42 | +use Fisharebest\Webtrees\Http\ViewResponseTrait; |
| 43 | +use Fisharebest\Webtrees\I18N; |
| 44 | +use Fisharebest\Webtrees\Services\CaptchaService; |
| 45 | +use Fisharebest\Webtrees\Services\EmailService; |
| 46 | +use Fisharebest\Webtrees\Services\RateLimitService; |
| 47 | +use Fisharebest\Webtrees\Services\UserService; |
| 48 | +use Fisharebest\Webtrees\Site; |
| 49 | +use Fisharebest\Webtrees\Tree; |
| 50 | +use Fisharebest\Webtrees\Validator; |
| 51 | +use Jefferson49\Webtrees\Helpers\DeactivatedCaptchaService; |
| 52 | +use Jefferson49\Webtrees\Helpers\Functions; |
| 53 | +use Jefferson49\Webtrees\Internationalization\MoreI18N; |
| 54 | +use Psr\Http\Message\ResponseInterface; |
| 55 | +use Psr\Http\Message\ServerRequestInterface; |
| 56 | +use Psr\Http\Server\RequestHandlerInterface; |
| 57 | + |
| 58 | +use Exception; |
| 59 | + |
| 60 | +/** |
| 61 | + * Register with an authorization provider |
| 62 | + */ |
| 63 | +class RegisterWithProviderAction implements RequestHandlerInterface |
| 64 | +{ |
| 65 | + use ViewResponseTrait; |
| 66 | + |
| 67 | + private CaptchaService $captcha_service; |
| 68 | + |
| 69 | + /** |
| 70 | + * @param CaptchaService $captcha_service |
| 71 | + */ |
| 72 | + public function __construct(CaptchaService $captcha_service) |
| 73 | + { |
| 74 | + $this->captcha_service = $captcha_service; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param ServerRequestInterface $request |
| 79 | + * |
| 80 | + * @return ResponseInterface |
| 81 | + */ |
| 82 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 83 | + { |
| 84 | + $this->checkRegistrationAllowed(); |
| 85 | + |
| 86 | + $tree = Validator::attributes($request)->treeOptional(); |
| 87 | + |
| 88 | + $password_token = Validator::queryParams($request)->string('password_token', ''); |
| 89 | + $email = Validator::queryParams($request)->string('email', ''); |
| 90 | + $real_name = Validator::queryParams($request)->string('real_name', ''); |
| 91 | + $user_name = Validator::queryParams($request)->string('user_name', ''); |
| 92 | + |
| 93 | + try { |
| 94 | + if ($this->captcha_service->isRobot($request)) { |
| 95 | + throw new Exception(MoreI18N::xlate('Please try again.')); |
| 96 | + } |
| 97 | + } |
| 98 | + catch (Exception $ex) { |
| 99 | + FlashMessages::addMessage($ex->getMessage(), 'danger'); |
| 100 | + |
| 101 | + return redirect(route(LoginPage::class)); |
| 102 | + } |
| 103 | + |
| 104 | + //Generate a request for a new webtrees user account |
| 105 | + $random_password = md5($password_token . time()); |
| 106 | + |
| 107 | + $params = [ |
| 108 | + 'comments' => I18N::translate('Automatic user registration after sign in with authorization provider'), |
| 109 | + 'email' => $email, |
| 110 | + 'password' => $random_password, |
| 111 | + 'real_name' => $real_name, |
| 112 | + 'user_name' => $user_name, |
| 113 | + ]; |
| 114 | + |
| 115 | + $request = Functions::getFromContainer(ServerRequestInterface::class); |
| 116 | + $request = $request->withAttribute('tree', $tree instanceof Tree ? $tree: null); |
| 117 | + $request = $request->withParsedBody($params); |
| 118 | + |
| 119 | + //Use a deactivated captcha service to call the request handler directly from the code |
| 120 | + $request_handler = new RegisterAction(new DeactivatedCaptchaService, new EmailService, new RateLimitService(), new UserService); |
| 121 | + |
| 122 | + return $request_handler->handle($request); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Check that visitors are allowed to register on this site. |
| 127 | + * |
| 128 | + * @return void |
| 129 | + * @throws HttpNotFoundException |
| 130 | + */ |
| 131 | + private function checkRegistrationAllowed(): void |
| 132 | + { |
| 133 | + if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') { |
| 134 | + throw new HttpNotFoundException(); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments