Skip to content

Commit

Permalink
Fix theme initialization / login token manager interaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 25, 2024
1 parent afa60eb commit 05e4c18
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
36 changes: 35 additions & 1 deletion module/VuFind/src/VuFind/Auth/LoginTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace VuFind\Auth;

use Laminas\Session\SessionManager;
use VuFind\Db\Row\User;
use VuFind\Exception\Auth as AuthException;
use VuFind\Exception\LoginToken as LoginTokenException;

Expand Down Expand Up @@ -97,6 +98,20 @@ class LoginTokenManager implements \VuFind\I18n\Translator\TranslatorAwareInterf
*/
protected $viewRenderer = null;

/**
* Has the theme been initialized yet?
*
* @var bool
*/
protected $themeInitialized = false;

/**
* User that needs to receive a warning (or null for no warning needed)
*
* @var ?User
*/
protected $userToWarn = null;

/**
* LoginToken constructor.
*
Expand Down Expand Up @@ -139,6 +154,7 @@ public function tokenLogin(string $sessionId): ?\VuFind\Db\Row\User
$user = null;
if ($cookie) {
try {
throw new LoginTokenException('test');
if ($token = $this->loginTokenTable->matchToken($cookie)) {
$this->loginTokenTable->deleteBySeries($token->series, $token->user_id);
$user = $this->userTable->getById($token->user_id);
Expand All @@ -149,13 +165,31 @@ public function tokenLogin(string $sessionId): ?\VuFind\Db\Row\User
// associated with the tokens and send a warning email to user
$user = $this->userTable->getById($cookie['user_id']);
$this->deleteUserLoginTokens($user->id);
$this->sendLoginTokenWarningEmail($user);
if ($this->themeInitialized) {
$this->sendLoginTokenWarningEmail($user);
} else {
$this->userToWarn = $user;
}
return null;
}
}
return $user;
}

/**
* Event hook -- called after the theme has initialized.
*
* @return void
*/
public function themeIsReady(): void
{
$this->themeInitialized = true;
if ($this->userToWarn) {
$this->sendLoginTokenWarningEmail($this->userToWarn);
$this->userToWarn = null;
}
}

/**
* Create a new login token
*
Expand Down
9 changes: 1 addition & 8 deletions module/VuFind/src/VuFind/Auth/LoginTokenManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,8 @@ public function __invoke(
throw new \Exception('Unexpected options sent to factory.');
}

$mainConfig = $container->get(\VuFind\Config\PluginManager::class)
->get('config');

// We need to initialize the theme so that the view renderer works:
$theme = new \VuFindTheme\Initializer($mainConfig->Site, $container);
$theme->init();

return new $requestedName(
$config = $container->get(\VuFind\Config\PluginManager::class)
$container->get(\VuFind\Config\PluginManager::class)
->get('config'),
$container->get(\VuFind\Db\Table\PluginManager::class)
->get('user'),
Expand Down
14 changes: 14 additions & 0 deletions module/VuFind/src/VuFind/Bootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ protected function initTheme(): void
$this->events->attach('dispatch', $callback, 9000);
}

/**
* Send login token warnings after the theme is initialized, if necessary.
*
* @return void
*/
protected function initLoginTokenManager(): void
{
$callback = function () {
$this->container->get(\VuFind\Auth\LoginTokenManager::class)->themeIsReady();
};
$this->events->attach('dispatch.error', $callback, 8000);
$this->events->attach('dispatch', $callback, 8000);
}

/**
* Set up custom HTTP status based on exception information.
*
Expand Down

0 comments on commit 05e4c18

Please sign in to comment.