Implement account deletion
This commit is contained in:
parent
2ccee2169b
commit
930726432e
11 changed files with 130 additions and 28 deletions
|
|
@ -6,6 +6,7 @@ namespace MailAccountAdmin\Frontend\Accounts;
|
|||
use MailAccountAdmin\Common\ActionResult;
|
||||
use MailAccountAdmin\Common\SessionHelper;
|
||||
use MailAccountAdmin\Common\UserHelper;
|
||||
use MailAccountAdmin\Exceptions\AppException;
|
||||
use MailAccountAdmin\Exceptions\InputValidationError;
|
||||
use MailAccountAdmin\Frontend\BaseController;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
|
|
@ -31,7 +32,12 @@ class AccountController extends BaseController
|
|||
$queryParams = $request->getQueryParams();
|
||||
$filterByDomain = $queryParams['domain'] ?? '';
|
||||
|
||||
// Get list of all accounts
|
||||
$renderData = $this->accountHandler->listAccounts($filterByDomain);
|
||||
|
||||
// If the form has been submitted, add the result message to the render data array
|
||||
$renderData = $this->addLastActionResultToRenderData($renderData);
|
||||
|
||||
return $this->view->render($response, 'accounts.html.twig', $renderData);
|
||||
}
|
||||
|
||||
|
|
@ -55,10 +61,7 @@ class AccountController extends BaseController
|
|||
$renderData = $this->accountHandler->getPageDataForCreate();
|
||||
|
||||
// If the form has been submitted, add the result message and form input data to the render data array
|
||||
$lastActionResult = $this->sessionHelper->getLastActionResult();
|
||||
if ($lastActionResult !== null) {
|
||||
$renderData = array_merge($renderData, $lastActionResult->getRenderData());
|
||||
}
|
||||
$renderData = $this->addLastActionResultToRenderData($renderData);
|
||||
|
||||
return $this->view->render($response, 'account_create.html.twig', $renderData);
|
||||
}
|
||||
|
|
@ -100,10 +103,7 @@ class AccountController extends BaseController
|
|||
$renderData = $this->accountHandler->getAccountDataForEdit($accountId);
|
||||
|
||||
// If the form has been submitted, add the result message and form input data to the render data array
|
||||
$lastActionResult = $this->sessionHelper->getLastActionResult();
|
||||
if ($lastActionResult !== null) {
|
||||
$renderData = array_merge($renderData, $lastActionResult->getRenderData());
|
||||
}
|
||||
$renderData = $this->addLastActionResultToRenderData($renderData);
|
||||
|
||||
return $this->view->render($response, 'account_edit.html.twig', $renderData);
|
||||
}
|
||||
|
|
@ -145,13 +145,47 @@ class AccountController extends BaseController
|
|||
// Get account data and list of aliases from database
|
||||
$renderData = $this->accountHandler->getAccountDataForDelete($accountId);
|
||||
|
||||
// If the form has been submitted, add the result message to the render data array
|
||||
$renderData = $this->addLastActionResultToRenderData($renderData);
|
||||
|
||||
return $this->view->render($response, 'account_delete.html.twig', $renderData);
|
||||
}
|
||||
|
||||
public function deleteAccount(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
// TODO: just a placeholder
|
||||
$this->view->getEnvironment()->addGlobal('error', 'Not implemented yet!');
|
||||
return $this->showAccountDelete($request, $response, $args);
|
||||
// Parse URL arguments and form data
|
||||
$accountId = (int)$args['id'];
|
||||
$formData = $request->getParsedBody();
|
||||
|
||||
try {
|
||||
// Confirm action by entering the admin password
|
||||
$this->userHelper->confirmActionByAdminPassword($formData['admin_password'] ?? '');
|
||||
|
||||
// Delete account
|
||||
$deleteResult = $this->accountHandler->deleteAccount($accountId);
|
||||
|
||||
// Save success result
|
||||
$successMessage = "Account <i>{$deleteResult['username']}</i> ";
|
||||
$deletedAliasCount = $deleteResult['deleted_alias_count'];
|
||||
if ($deletedAliasCount > 0) {
|
||||
$aliasWordPlural = $deletedAliasCount > 1 ? 'aliases' : 'alias';
|
||||
$successMessage .= "and {$deletedAliasCount} {$aliasWordPlural} were deleted.";
|
||||
} else {
|
||||
$successMessage .= "was deleted.";
|
||||
}
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createSuccessResult($successMessage));
|
||||
|
||||
// Redirect to account list (where the success message will be displayed)
|
||||
$redirectTarget = '/accounts';
|
||||
} catch (AppException $e) {
|
||||
// Save error result
|
||||
$this->sessionHelper->setLastActionResult(ActionResult::createErrorResult($e->getMessage()));
|
||||
|
||||
// Stay on delete page
|
||||
$redirectTarget = '/accounts/' . $accountId . '/delete';
|
||||
}
|
||||
|
||||
// Redirect to edit form page via GET (PRG)
|
||||
return $response->withHeader('Location', $redirectTarget)->withStatus(303);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ class AccountHandler
|
|||
|
||||
// Remove existing alias for new username (if wanted)
|
||||
if ($editData->getUsernameReplaceAlias() && $aliasNeedsToBeReplaced) {
|
||||
$this->aliasRepository->removeAlias($accountId, $newUsername);
|
||||
$this->aliasRepository->deleteAlias($accountId, $newUsername);
|
||||
}
|
||||
|
||||
// Commit database transaction
|
||||
|
|
@ -238,4 +238,31 @@ class AccountHandler
|
|||
'aliases' => $aliases,
|
||||
];
|
||||
}
|
||||
|
||||
public function deleteAccount(int $accountId): array
|
||||
{
|
||||
// Check if account exists
|
||||
try {
|
||||
$account = $this->accountRepository->fetchAccountById($accountId);
|
||||
} catch (AccountNotFoundException $e) {
|
||||
throw new InputValidationError('Account with ID ' . $accountId . ' does not exist!');
|
||||
}
|
||||
|
||||
// Start database transaction
|
||||
$this->accountRepository->beginTransaction();
|
||||
|
||||
// Delete all aliases associated with this account
|
||||
$deleteAliasCount = $this->aliasRepository->deleteAllAliasesForUserId($accountId);
|
||||
|
||||
// Delete account from database
|
||||
$this->accountRepository->deleteAccountWithId($accountId);
|
||||
|
||||
// Commit database transaction
|
||||
$this->accountRepository->commitTransaction();
|
||||
|
||||
return [
|
||||
'username' => $account->getUsername(),
|
||||
'deleted_alias_count' => $deleteAliasCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue