Implement option to replace existing alias when renaming an account; use database transactions when editing accounts

This commit is contained in:
Lexi / Zoe 2021-08-20 22:48:15 +02:00
parent 6ace072841
commit 36b8cfe8b1
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
7 changed files with 163 additions and 15 deletions

View file

@ -73,26 +73,47 @@ class AccountHandler
throw new InputValidationError('Account with ID ' . $accountId . ' does not exist!');
}
// TODO: Use database transactions (beginTransaction/commit/rollBack in BaseRepository maybe?)
$newUsername = $editData->getUsername();
if ($newUsername === $account->getUsername()) {
// Username is unchanged
$newUsername = null;
}
// TODO: This feature is not supported yet. If the alias exists, the availability check (next) would fail anyway.
if ($editData->getUsernameReplaceAlias()) {
throw new InputValidationError('Replace alias: Not implemented yet.');
}
// This variable will be set to true if the user wants to change their username, has an existing alias with this username,
// and checked the "replace existing alias" option.
$aliasNeedsToBeReplaced = false;
// Check if new username is still available
if ($newUsername !== null) {
if (!$this->accountRepository->checkUsernameAvailable($newUsername) || !$this->aliasRepository->checkAliasAvailable($newUsername)) {
$newUsernameAvailable = true;
// Check if account with this username already exists
if (!$this->accountRepository->checkUsernameAvailable($newUsername)) {
$newUsernameAvailable = false;
}
// Check if alias with this username/address already exists
if (!$this->aliasRepository->checkAliasAvailable($newUsername)) {
$newUsernameAvailable = false;
// Alias already exists. If user wants to replace an existing alias, check if the alias belongs to this user.
if ($editData->getUsernameReplaceAlias()) {
$existingAlias = $this->aliasRepository->fetchAliasByAddress($newUsername);
if ($existingAlias->getUserId() === $accountId) {
$aliasNeedsToBeReplaced = true;
$newUsernameAvailable = true;
}
}
}
if (!$newUsernameAvailable) {
throw new InputValidationError("Username \"$newUsername\" is not available.");
}
}
// Start database transaction
$this->accountRepository->beginTransaction();
// Create alias for old username (if wanted)
if ($editData->getUsernameCreateAlias()) {
$oldUsername = $account->getUsername();
@ -119,9 +140,11 @@ class AccountHandler
);
// Remove existing alias for new username (if wanted)
// TODO: See above. This is the point where the alias should be deleted.
// if ($editData->getUsernameReplaceAlias()) {
// throw new InputValidationError('Replace alias: Not implemented yet.');
// }
if ($editData->getUsernameReplaceAlias() && $aliasNeedsToBeReplaced) {
$this->aliasRepository->removeAlias($accountId, $newUsername);
}
// Commit database transaction
$this->accountRepository->commitTransaction();
}
}