Skip to content

Commit

Permalink
Catch exceptions while rolling out users
Browse files Browse the repository at this point in the history
  • Loading branch information
tehplague committed Jan 23, 2020
1 parent 4f4c4a8 commit 7ed38d3
Showing 1 changed file with 67 additions and 60 deletions.
127 changes: 67 additions & 60 deletions src/Services/PasswordRolloutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,81 +104,88 @@ private function doRollout(array $users, bool $demoteUnknownUsers = false): void

/** @var Platform $platform */
foreach ($platforms as $platformName => $platform) {
$this->logger->info(
'Working on platform {platform} with type {type} on {hostname}',
[
'platform' => $platformName,
'type' => $platform->getType(),
'hostname' => $platform->getHostname()
]
);

$databaseConnection = DriverManager::getConnection(
[
'dbname' => $platform->getDatabase(),
'user' => $platform->getUsername() ?? $defaultUsername,
'password' => $platform->getPassword() ?? $defaultPassword,
'host' => $platform->getHostname(),
'driver' => 'pdo_mysql'
]
);

$updater = $this->platformRegistry->getUpdaterForPlatform($platform);
if ($updater instanceof DatabaseUpdaterInterface) {
$updater->setDatabaseConnection($databaseConnection);
}

$activeAdmins = [];

/** @var User $user */
foreach ($users as $user) {
try {
$this->logger->info(
'Rolling out user {username} to {hostname}',
'Working on platform {platform} with type {type} on {hostname}',
[
'username' => $user->getUsername(),
'platform' => $platformName,
'type' => $platform->getType(),
'hostname' => $platform->getHostname()
]
);

$hashAlgorithm = $updater->getHashAlgorithm();
$hashedPasswords = $user->getHashedPasswords();
if (!isset($hashedPasswords[$hashAlgorithm])) {
$this->logger->error(
'User {username} does not have a password hash for the {algorithm} algorithm. Skipping...',
$databaseConnection = DriverManager::getConnection(
[
'dbname' => $platform->getDatabase(),
'user' => $platform->getUsername() ?? $defaultUsername,
'password' => $platform->getPassword() ?? $defaultPassword,
'host' => $platform->getHostname(),
'driver' => 'pdo_mysql'
]
);

$updater = $this->platformRegistry->getUpdaterForPlatform($platform);
if ($updater instanceof DatabaseUpdaterInterface) {
$updater->setDatabaseConnection($databaseConnection);
}

$activeAdmins = [];

/** @var User $user */
foreach ($users as $user) {
$this->logger->info(
'Rolling out user {username} to {hostname}',
[
'username' => $user->getUsername(),
'algorithm' => $hashAlgorithm
'hostname' => $platform->getHostname()
]
);
continue;
}

if (!$this->dryRun) {
$updater->updateAccountByUsername(
$user->getUsername(),
$hashedPasswords[$hashAlgorithm],
true,
$user->getFirstname(),
$user->getLastname(),
$user->getEmail(),
$user->isActive()
);
$hashAlgorithm = $updater->getHashAlgorithm();
$hashedPasswords = $user->getHashedPasswords();
if (!isset($hashedPasswords[$hashAlgorithm])) {
$this->logger->error(
'User {username} does not have a password hash for the {algorithm} algorithm. Skipping...',
[
'username' => $user->getUsername(),
'algorithm' => $hashAlgorithm
]
);
continue;
}

if (!$this->dryRun) {
$updater->updateAccountByUsername(
$user->getUsername(),
$hashedPasswords[$hashAlgorithm],
true,
$user->getFirstname(),
$user->getLastname(),
$user->getEmail(),
$user->isActive()
);
}

$activeAdmins[] = $user->getUsername();
}

$activeAdmins[] = $user->getUsername();
}

if ($demoteUnknownUsers && $platform->getManageAdminUsers()) {
$this->logger->info(
'Demoting all other admin users on {hostname}',
[
'hostname' => $platform->getHostname()
]
);
if ($demoteUnknownUsers && $platform->getManageAdminUsers()) {
$this->logger->info(
'Demoting all other admin users on {hostname}',
[
'hostname' => $platform->getHostname()
]
);

if (!$this->dryRun) {
$updater->demoteUnknownUsers($activeAdmins);
if (!$this->dryRun) {
$updater->demoteUnknownUsers($activeAdmins);
}
}
} catch (\Exception $ex) {
$this->logger->alert('Exception while rolling out users to {hostname}: {message}', [
'hostname' => $platform->getHostname(),
'message' => $ex->getMessage()
]);
}
}
}
Expand Down

0 comments on commit 7ed38d3

Please sign in to comment.