Implement adding aliases for accounts

This commit is contained in:
Lexi / Zoe 2021-09-16 22:09:15 +02:00
parent 19b8075e3c
commit 37ada99f74
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
7 changed files with 109 additions and 3 deletions

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Frontend\Accounts;
use MailAccountAdmin\Common\FormData;
class AccountAddAliasData extends FormData
{
private string $aliasAddress;
private function __construct(string $aliasAddress)
{
$this->aliasAddress = $aliasAddress;
}
public static function createFromArray($raw): self
{
return new self(
self::validateAliasAddress(trim($raw['alias_address'] ?? '')),
);
}
public function getAliasAddress(): string
{
return $this->aliasAddress;
}
}

View file

@ -64,6 +64,9 @@ class AccountController extends BaseController
return $this->showAccount404($response, $accountId);
}
// If the "add alias" form has been submitted, add the result message to the render data array
$renderData = $this->addLastActionResultToRenderData($renderData);
return $this->view->render($response, 'account_details.html.twig', $renderData);
}
@ -210,4 +213,30 @@ class AccountController extends BaseController
// Redirect to edit form page via GET (PRG)
return $response->withHeader('Location', $redirectTarget)->withStatus(303);
}
// -- /accounts/{id}/addalias - Create a new alias for the account
public function addAliasToAccount(Request $request, Response $response, array $args): Response
{
// Parse URL arguments and form data
$accountId = (int)$args['id'];
$addAliasData = $request->getParsedBody();
try {
// Validate input
$validatedAddAliasData = AccountAddAliasData::createFromArray($addAliasData);
$this->accountHandler->addAliasToAccount($accountId, $validatedAddAliasData);
// Save success result
$successMessage = "Alias <i>{$validatedAddAliasData->getAliasAddress()}</i> was added.";
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult($successMessage));
} catch (InputValidationError $e) {
// Save error result
$this->sessionHelper->setLastActionResult(ActionResult::createErrorResult($e->getMessage(), $addAliasData));
}
// Redirect to edit form page via GET (PRG)
return $response->withHeader('Location', '/accounts/' . $accountId)->withStatus(303);
}
}

View file

@ -265,4 +265,26 @@ class AccountHandler
'deleted_alias_count' => $deleteAliasCount,
];
}
// -- /accounts/{id}/addalias - Create a new alias for the account
public function addAliasToAccount(int $accountId, AccountAddAliasData $aliasAddData): void
{
// Check if account exists
try {
$this->accountRepository->fetchAccountById($accountId);
} catch (AccountNotFoundException $e) {
throw new InputValidationError('Account with ID ' . $accountId . ' does not exist!');
}
// Check if alias address is still available
$address = $aliasAddData->getAliasAddress();
if (!$this->accountRepository->checkUsernameAvailable($address) || !$this->aliasRepository->checkAliasAvailable($address)) {
throw new InputValidationError("Alias address \"$address\" is not available.");
}
// Create alias in database
$this->aliasRepository->createNewAlias($accountId, $address);
}
}