Implement random password generation (create/edit account)

This commit is contained in:
Lexi / Zoe 2021-09-16 19:55:38 +02:00
parent 48925f283f
commit 2ccee2169b
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
9 changed files with 127 additions and 54 deletions

View file

@ -72,7 +72,7 @@ class AccountHandler
];
}
public function createNewAccount(AccountCreateData $createData): int
public function createNewAccount(AccountCreateData $createData): array
{
// Check if new username is still available
$username = $createData->getUsername();
@ -80,8 +80,18 @@ class AccountHandler
throw new InputValidationError("Username \"$username\" is not available.");
}
// Array returned by the function on success
$returnData = [
'username' => $username,
];
// Hash new password
$passwordHash = $this->passwordHelper->hashPassword($createData->getPassword());
$password = $createData->getPassword();
if ($password === null) {
$password = $this->passwordHelper->generateRandomPassword();
$returnData['generatedPassword'] = $password;
}
$passwordHash = $this->passwordHelper->hashPassword($password);
// Construct home directory from username if necessary
if ($createData->getHomeDir() !== null) {
@ -92,13 +102,15 @@ class AccountHandler
}
// Create account in database
return $this->accountRepository->insertAccount(
$returnData['id'] = $this->accountRepository->insertAccount(
$username,
$passwordHash,
$createData->getActive(),
$homeDir,
$createData->getMemo()
);
return $returnData;
}
@ -116,8 +128,11 @@ class AccountHandler
];
}
public function editAccountData(int $accountId, AccountEditData $editData): void
public function editAccountData(int $accountId, AccountEditData $editData): array
{
// Array returned by the function on success
$returnData = [];
// Check if account exists
try {
$account = $this->accountRepository->fetchAccountById($accountId);
@ -175,12 +190,16 @@ class AccountHandler
$this->aliasRepository->createNewAlias($accountId, $oldUsername);
}
// Hash new password
$newPasswordHash = null;
if ($editData->getPassword() !== null) {
$newPasswordHash = $this->passwordHelper->hashPassword($editData->getPassword());
// Generate new password (if wanted)
$newPassword = $editData->getPassword();
if ($editData->getPasswordGenerateRandom()) {
$newPassword = $this->passwordHelper->generateRandomPassword();
$returnData['generatedPassword'] = $newPassword;
}
// Hash new password
$newPasswordHash = $newPassword !== null ? $this->passwordHelper->hashPassword($newPassword) : null;
// Update account in database
$this->accountRepository->updateAccountWithId(
$accountId,
@ -198,6 +217,8 @@ class AccountHandler
// Commit database transaction
$this->accountRepository->commitTransaction();
return $returnData;
}