Skip to content

Commit ef886f8

Browse files
committed
Added page to approve registration with provider
1 parent 3b611bb commit ef886f8

File tree

4 files changed

+229
-20
lines changed

4 files changed

+229
-20
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jefferson49\Webtrees\Module\OAuth2Client;
6+
7+
use Fisharebest\Webtrees\I18N;
8+
use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
9+
use Fisharebest\Webtrees\Tree;
10+
use Jefferson49\Webtrees\Internationalization\MoreI18N;
11+
use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProviderAction;
12+
13+
14+
/**
15+
* @var string $captcha
16+
* @var bool $show_caution
17+
* @var string $title
18+
* @var Tree|null $tree
19+
* @var string $url
20+
* @var string $email
21+
* @var string $password_token
22+
* @var string $real_name
23+
* @var string $user_name
24+
* @var string $provider_name
25+
*/
26+
?>
27+
28+
<h2 class="wt-page-title">
29+
<?= $title ?>
30+
</h2>
31+
32+
<div class="row mb-3"><?= view('icons/spacer') ?></div>
33+
34+
<?php if ($show_caution) : ?>
35+
<div class="wt-register-caution">
36+
<?= MoreI18N::xlate('<p>Notice: By completing and submitting this form, you agree:</p><ul><li>to protect the privacy of living individuals listed on our site;</li><li>and in the text box below, to explain to whom you are related, or to provide us with information on someone who should be listed on our website.</li></ul>') ?>
37+
</div>
38+
<?php endif ?>
39+
40+
<div class="wt-page-options wt-page-options-login">
41+
<div class="row mb-3">
42+
<label class="col-sm-3 col-form-label wt-page-options-label">
43+
<?= I18N::translate('Request a new account with') . ' ' . $provider_name ?>
44+
</label>
45+
<div class="col-sm-9 wt-page-options-value">
46+
<form method="post" class="wt-page-options wt-page-options-register-with-provider" action="<?= e(route(RegisterWithProviderAction::class, [
47+
'tree' => $tree instanceof Tree ? $tree->name() : null,
48+
'password_token' => $password_token,
49+
'email' => $email,
50+
'real_name' => $real_name,
51+
'user_name' => $user_name,
52+
])) ?>" >
53+
54+
<?= $captcha ?>
55+
56+
<button class="btn btn-primary">
57+
<?= MoreI18N::xlate('continue') ?>
58+
</button>
59+
60+
<a href="<?= e(route(LoginPage::class, [
61+
'tree' => $tree instanceof Tree ? $tree->name() : null,
62+
'url' => $url,
63+
])) ?>" type="submit" class="btn btn-secondary">
64+
<?= MoreI18N::xlate('cancel') ?>
65+
</a>
66+
67+
<?= csrf_field() ?>
68+
</form>
69+
</div>
70+
</div>
71+
</div>

src/LoginWithAuthorizationProviderAction.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,19 @@ public function handle(ServerRequestInterface $request): ResponseInterface
316316
FlashMessages::addMessage($message, 'success');
317317
CustomModuleLog::addDebugLog($log_module, $message);
318318

319-
//Generate a request a new webtrees user account
320-
$random_password = md5($accessToken->getToken() . time());
321-
322-
$params = [
323-
'comments' => I18N::translate('Automatic user registration after sign in with authorization provider'),
324-
'email' => $email,
325-
'password' => $random_password,
326-
'realname' => $real_name,
327-
'username' => $user_name,
328-
];
329-
330-
$request = Functions::getFromContainer(ServerRequestInterface::class);
331-
$request = $request->withAttribute('tree', $tree instanceof Tree ? $tree: null);
332-
$request = $request->withParsedBody($params);
333-
334-
//Use a deactivated captcha service to call the request handler directly from the code
335-
$request_handler = new RegisterAction(new DeactivatedCaptchaService, new EmailService, new RateLimitService(), new UserService);
336-
337-
return $request_handler->handle($request);
319+
//Show register with provider page
320+
return $this->viewResponse(OAuth2Client::viewsNamespace() . '::register-with-provider-page', [
321+
'captcha' => $this->captcha_service->createCaptcha(),
322+
'show_caution' => Site::getPreference('SHOW_REGISTER_CAUTION') === '1',
323+
'title' => I18N::translate('Request a new user account with an authorization provider'),
324+
'tree' => $tree,
325+
'url' => $url,
326+
'provider_name' => $provider_name,
327+
'email' => $email,
328+
'password_token' => $accessToken->getToken(),
329+
'real_name' => $real_name,
330+
'user_name' => $user_name,
331+
]);
338332
}
339333

340334
//Login

src/OAuth2Client.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
4646
use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
4747
use Fisharebest\Webtrees\Http\RequestHandlers\Logout;
48-
use Fisharebest\Webtrees\Http\RequestHandlers\RegisterPage;
4948
use Fisharebest\Webtrees\I18N;
5049
use Fisharebest\Webtrees\Menu;
5150
use Fisharebest\Webtrees\Module\AbstractModule;
@@ -70,6 +69,7 @@
7069
use Jefferson49\Webtrees\Log\CustomModuleLogInterface;
7170
use Jefferson49\Webtrees\Module\OAuth2Client\Factories\AuthorizationProviderFactory;
7271
use Jefferson49\Webtrees\Module\OAuth2Client\LoginWithAuthorizationProviderAction;
72+
use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProviderAction;
7373
use GuzzleHttp\Client;
7474
use Illuminate\Support\Collection;
7575
use GuzzleHttp\Exception\GuzzleException;
@@ -103,6 +103,7 @@ class OAuth2Client extends AbstractModule implements
103103

104104
//Routes
105105
public const REDIRECT_ROUTE = '/OAuth2Client';
106+
public const REGISTER_PROVIDER_ROUTE = '/register-with-provider-action{/tree}';
106107

107108
//Github repository
108109
public const GITHUB_REPO = 'Jefferson49/OAuth2-Client';
@@ -201,6 +202,12 @@ public function boot(): void
201202
$router
202203
->get(LoginWithAuthorizationProviderAction::class, self::REDIRECT_ROUTE)
203204
->allows(RequestMethodInterface::METHOD_POST);
205+
206+
//Register a route for the RegisterWithProviderAction request handler
207+
$router = Registry::routeFactory()->routeMap();
208+
$router
209+
->get(RegisterWithProviderAction::class, self::REGISTER_PROVIDER_ROUTE)
210+
->allows(RequestMethodInterface::METHOD_POST);
204211
}
205212

206213
/**
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)