Implement random password generation (create/edit account)
This commit is contained in:
parent
48925f283f
commit
2ccee2169b
9 changed files with 127 additions and 54 deletions
|
|
@ -71,13 +71,14 @@ class AccountController extends BaseController
|
|||
try {
|
||||
// Validate input
|
||||
$validatedCreateData = AccountCreateData::createFromArray($createData);
|
||||
$newAccountId = $this->accountHandler->createNewAccount($validatedCreateData);
|
||||
$createResult = $this->accountHandler->createNewAccount($validatedCreateData);
|
||||
|
||||
// Save success result
|
||||
$newAccountName = $validatedCreateData->getUsername();
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult(
|
||||
'Account <a href="/accounts/' . $newAccountId . '">' . $newAccountName . '</a> was created.'
|
||||
));
|
||||
$successMessage = "Account <a href=\"/accounts/{$createResult['id']}\">{$createResult['username']}</a> was created.";
|
||||
if (!empty($createResult['generatedPassword'])) {
|
||||
$successMessage .= "\nThe password generated for this account is: <i>{$createResult['generatedPassword']}</i>";
|
||||
}
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult($successMessage));
|
||||
} catch (InputValidationError $e) {
|
||||
// Save error result
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createErrorResult($e->getMessage(), $createData));
|
||||
|
|
@ -116,10 +117,14 @@ class AccountController extends BaseController
|
|||
try {
|
||||
// Validate input
|
||||
$validatedEditData = AccountEditData::createFromArray($editData);
|
||||
$this->accountHandler->editAccountData($accountId, $validatedEditData);
|
||||
$editResult = $this->accountHandler->editAccountData($accountId, $validatedEditData);
|
||||
|
||||
// Save success result
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult('Account data was saved.'));
|
||||
$successMessage = "Account data was saved.";
|
||||
if (!empty($editResult['generatedPassword'])) {
|
||||
$successMessage .= "\nThe new password generated for this account is: <i>{$editResult['generatedPassword']}</i>";
|
||||
}
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult($successMessage));
|
||||
} catch (InputValidationError $e) {
|
||||
// Save error result
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createErrorResult($e->getMessage(), $editData));
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ use MailAccountAdmin\Common\FormData;
|
|||
class AccountCreateData extends FormData
|
||||
{
|
||||
private string $username;
|
||||
private string $password;
|
||||
private ?string $password;
|
||||
private bool $active;
|
||||
private ?string $homeDir;
|
||||
private string $memo;
|
||||
|
||||
private function __construct(string $username, string $password, bool $active, ?string $homeDir, string $memo)
|
||||
private function __construct(string $username, ?string $password, bool $active, ?string $homeDir, string $memo)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
|
|
@ -26,7 +26,7 @@ class AccountCreateData extends FormData
|
|||
{
|
||||
return new self(
|
||||
self::validateUsername(trim($raw['username'] ?? '')),
|
||||
self::validatePassword(trim($raw['password'] ?? ''), trim($raw['password_repeat'] ?? '')),
|
||||
self::validatePassword(trim($raw['password'] ?? ''), trim($raw['password_repeat'] ?? ''), false),
|
||||
self::validateBoolOption(trim($raw['is_active'] ?? '')),
|
||||
self::validateHomeDir(trim($raw['home_dir'] ?? ''), false),
|
||||
self::validateMemo(trim($raw['memo'] ?? '')),
|
||||
|
|
@ -38,7 +38,7 @@ class AccountCreateData extends FormData
|
|||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,17 +11,19 @@ class AccountEditData extends FormData
|
|||
private bool $usernameCreateAlias;
|
||||
private bool $usernameReplaceAlias;
|
||||
private ?string $password;
|
||||
private bool $passwordGenerateRandom;
|
||||
private bool $active;
|
||||
private ?string $homeDir;
|
||||
private string $memo;
|
||||
|
||||
private function __construct(?string $username, bool $usernameCreateAlias, bool $usernameReplaceAlias, ?string $password, bool $active,
|
||||
?string $homeDir, string $memo)
|
||||
private function __construct(?string $username, bool $usernameCreateAlias, bool $usernameReplaceAlias, ?string $password,
|
||||
bool $passwordGenerateRandom, bool $active, ?string $homeDir, string $memo)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->usernameCreateAlias = $usernameCreateAlias;
|
||||
$this->usernameReplaceAlias = $usernameReplaceAlias;
|
||||
$this->password = $password;
|
||||
$this->passwordGenerateRandom = $passwordGenerateRandom;
|
||||
$this->active = $active;
|
||||
$this->homeDir = $homeDir;
|
||||
$this->memo = $memo;
|
||||
|
|
@ -34,6 +36,7 @@ class AccountEditData extends FormData
|
|||
self::validateBoolOption(trim($raw['username_create_alias'] ?? '')),
|
||||
self::validateBoolOption(trim($raw['username_replace_alias'] ?? '')),
|
||||
self::validatePassword(trim($raw['password'] ?? ''), trim($raw['password_repeat'] ?? ''), false),
|
||||
self::validateBoolOption(trim($raw['password_generate_random'] ?? '')),
|
||||
self::validateBoolOption(trim($raw['is_active'] ?? '')),
|
||||
self::validateHomeDir(trim($raw['home_dir'] ?? ''), false),
|
||||
self::validateMemo(trim($raw['memo'] ?? '')),
|
||||
|
|
@ -60,6 +63,11 @@ class AccountEditData extends FormData
|
|||
return $this->password;
|
||||
}
|
||||
|
||||
public function getPasswordGenerateRandom(): bool
|
||||
{
|
||||
return $this->passwordGenerateRandom;
|
||||
}
|
||||
|
||||
public function getActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue