2021-07-29 18:15:38 +02:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace MailAccountAdmin\Frontend\Accounts;
|
|
|
|
|
|
2021-07-30 03:46:43 +02:00
|
|
|
use MailAccountAdmin\Common\UserHelper;
|
2021-07-29 18:15:38 +02:00
|
|
|
use MailAccountAdmin\Frontend\BaseController;
|
2021-08-14 23:37:23 +02:00
|
|
|
use MailAccountAdmin\Models\Account;
|
2021-07-30 03:46:43 +02:00
|
|
|
use MailAccountAdmin\Repositories\AccountRepository;
|
2021-07-30 22:30:30 +02:00
|
|
|
use MailAccountAdmin\Repositories\AliasRepository;
|
2021-07-29 18:15:38 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2021-07-30 03:46:43 +02:00
|
|
|
use Slim\Views\Twig;
|
2021-07-29 18:15:38 +02:00
|
|
|
|
|
|
|
|
class AccountController extends BaseController
|
|
|
|
|
{
|
2021-07-30 03:46:43 +02:00
|
|
|
/** @var AccountRepository */
|
|
|
|
|
private $accountRepository;
|
2021-07-30 22:30:30 +02:00
|
|
|
/** @var AliasRepository */
|
|
|
|
|
private $aliasRepository;
|
2021-07-30 03:46:43 +02:00
|
|
|
|
2021-07-30 22:30:30 +02:00
|
|
|
public function __construct(Twig $view, UserHelper $userHelper, AccountRepository $accountRepository, AliasRepository $aliasRepository)
|
2021-07-30 03:46:43 +02:00
|
|
|
{
|
|
|
|
|
parent::__construct($view, $userHelper);
|
|
|
|
|
$this->accountRepository = $accountRepository;
|
2021-07-30 22:30:30 +02:00
|
|
|
$this->aliasRepository = $aliasRepository;
|
2021-07-30 03:46:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
|
|
|
|
|
// -- /accounts - List all accounts
|
|
|
|
|
|
2021-07-29 18:15:38 +02:00
|
|
|
public function showAccounts(Request $request, Response $response): Response
|
|
|
|
|
{
|
2021-07-30 03:46:43 +02:00
|
|
|
// Parse query parameters for filters
|
|
|
|
|
$queryParams = $request->getQueryParams();
|
|
|
|
|
$filterByDomain = $queryParams['domain'] ?? null;
|
|
|
|
|
|
2021-07-29 18:15:38 +02:00
|
|
|
$renderData = [
|
2021-07-30 03:46:43 +02:00
|
|
|
'filterDomain' => $filterByDomain,
|
|
|
|
|
'accountList' => $this->accountRepository->fetchAccountList($filterByDomain),
|
2021-07-29 18:15:38 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->view->render($response, 'accounts.html.twig', $renderData);
|
|
|
|
|
}
|
2021-07-30 22:30:30 +02:00
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
|
|
|
|
|
// -- /accounts/{id} - Show account details
|
|
|
|
|
|
2021-07-30 22:30:30 +02:00
|
|
|
public function showAccountDetails(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
// Parse URL arguments
|
|
|
|
|
$accountId = (int)$args['id'];
|
|
|
|
|
|
|
|
|
|
// Get account data from database
|
2021-08-14 23:37:23 +02:00
|
|
|
$account = $this->accountRepository->fetchAccountById($accountId);
|
2021-07-30 22:30:30 +02:00
|
|
|
|
|
|
|
|
// Don't display the password hash, but at least the type of hash (used hash algorithm)
|
2021-08-14 23:37:23 +02:00
|
|
|
if ($account->getPasswordHash() === '') {
|
2021-07-30 22:30:30 +02:00
|
|
|
$passwordHashType = 'empty';
|
|
|
|
|
} else {
|
2021-08-14 23:37:23 +02:00
|
|
|
$passwordHashInfo = password_get_info($account->getPasswordHash());
|
2021-07-30 22:30:30 +02:00
|
|
|
$passwordHashType = $passwordHashInfo['algoName'] ?? 'unknown';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get list of aliases for this account
|
|
|
|
|
$aliases = $this->aliasRepository->fetchAliasesForUserId($accountId);
|
|
|
|
|
|
|
|
|
|
$renderData = [
|
|
|
|
|
'id' => $accountId,
|
2021-08-14 23:37:23 +02:00
|
|
|
'accountUsername' => $account->getUsername(),
|
|
|
|
|
'account' => $account,
|
2021-07-30 22:30:30 +02:00
|
|
|
'passwordHashType' => $passwordHashType,
|
|
|
|
|
'aliases' => $aliases,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->view->render($response, 'account_details.html.twig', $renderData);
|
|
|
|
|
}
|
2021-07-31 00:34:35 +02:00
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
|
|
|
|
|
// -- /accounts/new - Create new account
|
|
|
|
|
|
2021-07-31 00:34:35 +02:00
|
|
|
public function showAccountCreate(Request $request, Response $response): Response
|
|
|
|
|
{
|
|
|
|
|
return $this->showAccounts($request, $response);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
|
|
|
|
|
// -- /accounts/{id}/edit - Edit account
|
|
|
|
|
|
2021-07-31 00:34:35 +02:00
|
|
|
public function showAccountEdit(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
// Parse URL arguments
|
|
|
|
|
$accountId = (int)$args['id'];
|
|
|
|
|
|
|
|
|
|
// Get account data from database
|
2021-08-14 23:37:23 +02:00
|
|
|
$account = $this->accountRepository->fetchAccountById($accountId);
|
2021-07-31 00:34:35 +02:00
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
// Render page
|
|
|
|
|
return $this->renderEditPage($response, $account);
|
2021-07-31 00:34:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function editAccount(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
// TODO: just a placeholder
|
|
|
|
|
$this->view->getEnvironment()->addGlobal('error', 'Not implemented yet!');
|
|
|
|
|
return $this->showAccountEdit($request, $response, $args);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 23:37:23 +02:00
|
|
|
private function renderEditPage(Response $response, Account $account, array $extraRenderData = []): Response
|
|
|
|
|
{
|
|
|
|
|
$renderData = [
|
|
|
|
|
'id' => $account->getId(),
|
|
|
|
|
'accountUsername' => $account->getUsername(),
|
|
|
|
|
'account' => $account,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->view->render($response, 'account_edit.html.twig', array_merge($renderData, $extraRenderData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- /accounts/{id}/delete - Delete account
|
|
|
|
|
|
2021-07-31 00:34:35 +02:00
|
|
|
public function showAccountDelete(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
// Parse URL arguments
|
|
|
|
|
$accountId = (int)$args['id'];
|
|
|
|
|
|
|
|
|
|
// Get account data and list of aliases from database
|
2021-08-14 23:37:23 +02:00
|
|
|
$account = $this->accountRepository->fetchAccountById($accountId);
|
2021-07-31 00:34:35 +02:00
|
|
|
$aliases = $this->aliasRepository->fetchAliasesForUserId($accountId);
|
|
|
|
|
|
|
|
|
|
$renderData = [
|
|
|
|
|
'id' => $accountId,
|
2021-08-14 23:37:23 +02:00
|
|
|
'accountUsername' => $account->getUsername(),
|
2021-07-31 00:34:35 +02:00
|
|
|
'aliases' => $aliases,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-07-29 18:15:38 +02:00
|
|
|
}
|