accountRepository = $accountRepository; $this->aliasRepository = $aliasRepository; $this->passwordHelper = $passwordHelper; } // -- /accounts - List all accounts public function listAccounts(string $filterByDomain): array { $accountList = $this->accountRepository->fetchAccountList($filterByDomain); return [ 'filterDomain' => $filterByDomain, 'accountList' => $accountList, ]; } // -- /accounts/{id} - Show account details public function getAccountDetails(int $accountId): array { // Get account data from database $account = $this->accountRepository->fetchAccountById($accountId); // Don't display the password hash, but at least the type of hash (used hash algorithm) $passwordHashType = $this->passwordHelper->getPasswordHashType($account->getPasswordHash()); // Get list of aliases for this account $aliases = $this->aliasRepository->fetchAliasesForUserId($accountId); return [ 'id' => $accountId, 'accountUsername' => $account->getUsername(), 'account' => $account, 'passwordHashType' => $passwordHashType, 'aliases' => $aliases, ]; } // -- /accounts/{id}/edit - Edit account data public function editAccountData(int $accountId, AccountEditData $editData): void { // Check if account exists try { $account = $this->accountRepository->fetchAccountById($accountId); } catch (AccountNotFoundException $e) { throw new InputValidationError('Account with ID ' . $accountId . ' does not exist!'); } $newUsername = $editData->getUsername(); if ($newUsername === $account->getUsername()) { // Username is unchanged $newUsername = null; } // 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) { $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(); if (!$this->aliasRepository->checkAliasAvailable($oldUsername)) { throw new InputValidationError("Alias \"$oldUsername\" cannot be created: Alias already exists."); } $this->aliasRepository->createNewAlias($accountId, $oldUsername); } // Hash new password $newPasswordHash = null; if ($editData->getPassword() !== null) { $newPasswordHash = $this->passwordHelper->hashPassword($editData->getPassword()); } // Update account in database $this->accountRepository->updateAccountWithId( $accountId, $newUsername, $newPasswordHash, $editData->getActive(), $editData->getHomeDir(), $editData->getMemo() ); // Remove existing alias for new username (if wanted) if ($editData->getUsernameReplaceAlias() && $aliasNeedsToBeReplaced) { $this->aliasRepository->removeAlias($accountId, $newUsername); } // Commit database transaction $this->accountRepository->commitTransaction(); } }