Skip to content

Commit 7e70561

Browse files
committed
Bugfix: Also show sign in button labels in flash messages and on register page, fixes #19
1 parent 1a6c278 commit 7e70561

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

resources/views/register-page.phtml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ if ($provider_authorization) {
121121
<?php if (!$provider_authorization) : ?>
122122
<div class="row">
123123
<label class="col-sm-3 col-form-label wt-page-options-label" for="password">
124-
<?php if ($provider_authorization) : ?>
125-
<?= I18N::translate('Password (within webtrees)') ?>
126-
<?php else : ?>
127124
<?= MoreI18N::xlate('Password') ?>
128-
<?php endif ?>
129125
</label>
130126
<div class="col-sm-9 wt-page-options-value">
131127
<input class="form-control" type="password" id="password" name="password" placeholder="<?= /* I18N: placeholder text for new-password field */ I18N::plural('Use at least %s character.', 'Use at least %s characters.', 8, I18N::number(8)) ?>" pattern=".{8,}" required="required" autocomplete="new-password" data-wt-show-password-text="<?= e(MoreI18N::xlate('show')) ?>" data-wt-show-password-title="<?= e(MoreI18N::xlate('Show password')) ?>" data-wt-hide-password-text="<?= e(MoreI18N::xlate('hide')) ?>" data-wt-hide-password-title="<?= e(MoreI18N::xlate('Hide password')) ?>">

resources/views/register-with-provider-page.phtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Fisharebest\Webtrees\I18N;
88
use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
99
use Fisharebest\Webtrees\Tree;
1010
use Jefferson49\Webtrees\Internationalization\MoreI18N;
11+
use Jefferson49\Webtrees\Module\OAuth2Client\Factories\AuthorizationProviderFactory;
1112
use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProviderAction;
1213

1314

@@ -24,6 +25,11 @@ use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProvide
2425
* @var string $provider_name
2526
* @var string $comments
2627
*/
28+
29+
30+
$provider = AuthorizationProviderFactory::make($provider_name, '');
31+
$provider_label = $provider !== null ? $provider->getSignInButtonLabel() : $provider_name;
32+
2733
?>
2834

2935
<h2 class="wt-page-title">
@@ -63,7 +69,7 @@ use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProvide
6369

6470
<div class="row mb">
6571
<label class="col-sm-3 col-form-label wt-page-options-label">
66-
<?= I18N::translate('Request a new account with') . ' ' . $provider_name ?>
72+
<?= I18N::translate('Request a new account with') . ' ' . $provider_label ?>
6773
</label>
6874

6975
<div class="col-sm-9 wt-page-options-value">

src/LoginWithAuthorizationProviderAction.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
use Jefferson49\Webtrees\Helpers\Functions;
5555
use Jefferson49\Webtrees\Internationalization\MoreI18N;
5656
use Jefferson49\Webtrees\Log\CustomModuleLog;
57+
use Jefferson49\Webtrees\Module\OAuth2Client\Contracts\AuthorizationProviderInterface;
5758
use Jefferson49\Webtrees\Module\OAuth2Client\Factories\AuthorizationProviderFactory;
58-
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
5959
use Psr\Http\Message\ResponseInterface;
6060
use Psr\Http\Message\ServerRequestInterface;
6161
use Psr\Http\Server\RequestHandlerInterface;
@@ -147,7 +147,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
147147
$user->setPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER, '');
148148
$user->setPreference(OAuth2Client::USER_PREF_EMAIL_AT_PROVIDER, '');
149149

150-
$message = I18N::translate('Disconnected the user %s from provider: %s', $user->userName(), $provider_name);
150+
$message = I18N::translate('Disconnected the user %s from provider: %s', $user->userName(), $provider->getSignInButtonLabel());
151151
FlashMessages::addMessage($message, 'success');
152152
CustomModuleLog::addDebugLog($log_module, $message);
153153

@@ -180,7 +180,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
180180

181181
self::deleteSessionValuesForProviderConnection();
182182

183-
$message = I18N::translate('Timeout for connecting user %s with authorization provider %s. Please restart connecting with the authorization provider.', $user->userName(), $provider_name,);
183+
$message = I18N::translate('Timeout for connecting user %s with authorization provider %s. Please restart connecting with the authorization provider.', $user->userName(), $provider->getSignInButtonLabel(),);
184184
FlashMessages::addMessage($message, 'danger');
185185
CustomModuleLog::addDebugLog($log_module, $message);
186186

@@ -275,7 +275,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
275275
$existing_user = $this->user_service->findByEmail($email) ?? $this->user_service->findByUserName($user_name);
276276

277277
//Check if the authorizatiohn provider ID is already connected with an user
278-
$provider_id_is_connected = $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) !== null;
278+
$provider_id_is_connected = $this->findUserByAuthorizationProviderId($provider, $authorization_provider_id) !== null;
279279

280280
//Check if user has not signed in before (i.e. existing user, no provider name, no login timestamp)
281281
$existing_user_not_signed_in_yet = false;
@@ -300,7 +300,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
300300
$user->setPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER, $authorization_provider_id);
301301
$user->setPreference(OAuth2Client::USER_PREF_EMAIL_AT_PROVIDER, $email);
302302

303-
$message = I18N::translate('Sucessfully connected existing user %s with provider: %s', $user->userName(), $provider_name);
303+
$message = I18N::translate('Sucessfully connected existing user %s with provider: %s', $user->userName(), $provider->getSignInButtonLabel());
304304
FlashMessages::addMessage($message, 'success');
305305
CustomModuleLog::addDebugLog($log_module, $message);
306306

@@ -317,15 +317,15 @@ public function handle(ServerRequestInterface $request): ResponseInterface
317317

318318
// If provider does not support registration, show messages and redirect to login page
319319
if (!$provider::supportsRegistration()) {
320-
FlashMessages::addMessage(I18N::translate('It is not possible to request a webtrees account with %s.', $provider_name));
321-
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
320+
FlashMessages::addMessage(I18N::translate('It is not possible to request a webtrees account with %s.', $provider->getSignInButtonLabel()));
321+
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider->getSignInButtonLabel()));
322322
CustomModuleLog::addDebugLog($log_module, 'Provider does not support webtrees registration.');
323323
return redirect(route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null, 'url' => $url]));
324324
}
325325
// If no email was retrieved from authorization provider, show messages and redirect to login page
326326
elseif ($email === '' OR $user_name === '') {
327-
FlashMessages::addMessage(I18N::translate('Invalid user data received from %s. Email or username missing.', $provider_name), 'danger');
328-
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
327+
FlashMessages::addMessage(I18N::translate('Invalid user data received from %s. Email or username missing.', $provider->getSignInButtonLabel()), 'danger');
328+
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider->getSignInButtonLabel()));
329329
CustomModuleLog::addDebugLog($log_module, 'Invalid user account data received from authorizaton provider. Email or username missing.');
330330
return redirect(route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null, 'url' => $url]));
331331
}
@@ -336,8 +336,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface
336336
throw new HttpNotFoundException();
337337
}
338338

339-
FlashMessages::addMessage(I18N::translate('Press "continue" to request a new webtrees user acccount with %s.', $provider_name));
340-
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
339+
FlashMessages::addMessage(I18N::translate('Press "continue" to request a new webtrees user acccount with %s.', $provider->getSignInButtonLabel()));
340+
FlashMessages::addMessage(I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider->getSignInButtonLabel()));
341341
}
342342

343343
CustomModuleLog::addDebugLog($log_module, 'Forward to register with provider page');
@@ -361,7 +361,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
361361
//Login
362362
//Code from Fisharebest\Webtrees\Http\RequestHandlers\LoginAction
363363
try {
364-
$user = $this->doLogin($email, $provider_name, $authorization_provider_id, $log_module->getLogPrefix());
364+
$user = $this->doLogin($email, $provider, $authorization_provider_id, $log_module->getLogPrefix());
365365

366366
//Update email address if we have not just newly connected the user and email shall be synchronized with provider
367367
if ( $user_to_connect === 0
@@ -396,38 +396,38 @@ public function handle(ServerRequestInterface $request): ResponseInterface
396396
* Log in, if we can. Throw an exception, if we can't.
397397
* Code from Fisharebest\Webtrees\Http\RequestHandlers\LoginAction
398398
*
399-
* @param string $email Email address of user
400-
* @param string $provider_name Name of the authorization provider
401-
* @param string $authorization_provider_id User ID from the authorizationprovider
402-
* @param string $oauth_log_prefix Prefix for OAuth2 login lohd
399+
* @param string $email Email address of user
400+
* @param AuthorizationProviderInterface $provider The authorization provider
401+
* @param string $authorization_provider_id User ID from the authorizationprovider
402+
* @param string $oauth_log_prefix Prefix for OAuth2 login lohd
403403
*
404404
* @return void
405405
* @throws Exception
406406
*
407407
* @return User The logged in user
408408
*/
409-
private function doLogin(string $email, string $provider_name, string $authorization_provider_id, string $oauth_log_prefix): User
409+
private function doLogin(string $email, AuthorizationProviderInterface $provider, string $authorization_provider_id, string $oauth_log_prefix): User
410410
{
411411
if ($_COOKIE === []) {
412-
Log::addAuthenticationLog('Login failed (no session cookies): ' . $provider_name . ' ' . $authorization_provider_id);
412+
Log::addAuthenticationLog('Login failed (no session cookies): ' . $provider->getName() . ' ' . $authorization_provider_id);
413413
throw new Exception(MoreI18N::xlate('You cannot sign in because your browser does not accept cookies.'));
414414
}
415415

416416
//Try to get user by authorization provider id; otherwise try to get user by email
417-
$user = $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) ?? $this->user_service->findByEmail($email);
417+
$user = $this->findUserByAuthorizationProviderId($provider, $authorization_provider_id) ?? $this->user_service->findByEmail($email);
418418

419419
if ($user === null) {
420-
Log::addAuthenticationLog('Login failed (no such user/email): ' . $provider_name . ' ' . $authorization_provider_id);
420+
Log::addAuthenticationLog('Login failed (no such user/email): ' . $provider->getName() . ' ' . $authorization_provider_id);
421421
throw new Exception(MoreI18N::xlate('The username or password is incorrect.'));
422422
}
423423

424424
if ($user->getPreference(UserInterface::PREF_IS_EMAIL_VERIFIED) !== '1') {
425-
Log::addAuthenticationLog('Login failed (not verified by user): ' . $provider_name . ' ' . $authorization_provider_id);
425+
Log::addAuthenticationLog('Login failed (not verified by user): ' . $provider->getName() . ' ' . $authorization_provider_id);
426426
throw new Exception(MoreI18N::xlate('This account has not been verified. Please check your email for a verification message.'));
427427
}
428428

429429
if ($user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') {
430-
Log::addAuthenticationLog('Login failed (not approved by admin): ' . $provider_name . ' ' . $authorization_provider_id);
430+
Log::addAuthenticationLog('Login failed (not approved by admin): ' . $provider->getName() . ' ' . $authorization_provider_id);
431431
throw new Exception(MoreI18N::xlate('This account has not been approved. Please wait for an administrator to approve it.'));
432432
}
433433

@@ -437,26 +437,26 @@ private function doLogin(string $email, string $provider_name, string $authoriza
437437

438438
//If time stamp is different from 0 (i.e. user already logged in at least once before)
439439
if ($user->getPreference(UserInterface::PREF_TIMESTAMP_ACTIVE) !== '0') {
440-
Log::addAuthenticationLog($oauth_log_prefix . ': ' . 'Login denied. The email address or username already exists: ' . $provider_name . ' ' . $authorization_provider_id);
440+
Log::addAuthenticationLog($oauth_log_prefix . ': ' . 'Login denied. The email address or username already exists: ' . $provider->getName() . ' ' . $authorization_provider_id);
441441
throw new Exception(I18N::translate('Login denied. The email address or username already exists.') . ' ' .
442-
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
442+
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider->getSignInButtonLabel()));
443443
}
444444
}
445445
//If user has authorization provider, but provider/ID does not match
446-
elseif ( ($user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, '') !== $provider_name)
446+
elseif ( ($user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, '') !== $provider->getName())
447447
OR ($user->getPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER, '') !== $authorization_provider_id)) {
448448

449-
Log::addAuthenticationLog($oauth_log_prefix . ': ' . 'Login denied. The email address or username already exists: ' . $provider_name . ' ' . $authorization_provider_id);
449+
Log::addAuthenticationLog($oauth_log_prefix . ': ' . 'Login denied. The email address or username already exists: ' . $provider->getName() . ' ' . $authorization_provider_id);
450450
throw new Exception(I18N::translate('Login denied. The email address or username already exists.') . ' ' .
451-
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
451+
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider->getSignInButtonLabel()));
452452
}
453453

454454
Auth::login($user);
455455
Log::addAuthenticationLog('Login: ' . Auth::user()->userName() . '/' . Auth::user()->realName());
456456
Auth::user()->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, (string) time());
457457

458458
//Save authorization provider data to user preferences
459-
$user->setPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, $provider_name);
459+
$user->setPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, $provider->getName());
460460
$user->setPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER, $authorization_provider_id);
461461
$user->setPreference(OAuth2Client::USER_PREF_EMAIL_AT_PROVIDER, $email);
462462

@@ -499,18 +499,18 @@ private function resizeUserData(string $name, string $value, bool $add_flash_mes
499499
/**
500500
* Find user by authorization provider ID
501501
*
502-
* @param string $provider_name Name of the authorization provider
503-
* @param string $authorization_provider_id User ID from the authorizationprovider
502+
* @param AuthorizationProviderInterface $provider The authorization provider
503+
* @param string $authorization_provider_id User ID from the authorizationprovider
504504
*
505505
* @return User
506506
*/
507-
private function findUserByAuthorizationProviderId(string $provider_name, string $authorization_provider_id): ?User
507+
private function findUserByAuthorizationProviderId(AuthorizationProviderInterface $provider, string $authorization_provider_id): ?User
508508
{
509509
$users = Functions::getAllUsers();
510510

511511
foreach($users as $user) {
512512

513-
if ( $user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME) === $provider_name
513+
if ( $user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME) === $provider->getName()
514514
&& $user->getPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER) === $authorization_provider_id) {
515515

516516
return $user;

0 commit comments

Comments
 (0)