From 4e8d87900836d761ef0ef959eb1cfeef1bebf348 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Fri, 30 Jul 2021 22:30:30 +0200 Subject: [PATCH] Accounts: Add account detail page --- public/static/style.css | 18 ++++- src/Dependencies.php | 7 ++ src/Exceptions/AccountNotFoundException.php | 8 ++ src/Frontend/Accounts/AccountController.php | 38 +++++++++- src/Repositories/AccountRepository.php | 13 ++++ src/Repositories/AliasRepository.php | 16 ++++ src/Routes.php | 5 +- templates/account_details.html.twig | 82 +++++++++++++++++++++ 8 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 src/Exceptions/AccountNotFoundException.php create mode 100644 src/Repositories/AliasRepository.php create mode 100644 templates/account_details.html.twig diff --git a/public/static/style.css b/public/static/style.css index ab04854..32af1ae 100644 --- a/public/static/style.css +++ b/public/static/style.css @@ -116,18 +116,32 @@ a:hover, a:focus { color: gray; } +.green { + color: green; +} + +.red { + color: red; +} + button { padding: 0.2em 1em; } +.inactive { + color: gray; +} + +/* --- Tables --- */ + table, tr, td, th { border: 1px solid #999999; border-collapse: collapse; padding: 0.25em 0.5em; } -.inactive { - color: gray; +.vertical_table_headers th { + text-align: left; } /* --- Filter options --- */ diff --git a/src/Dependencies.php b/src/Dependencies.php index f7f9742..50c82e1 100644 --- a/src/Dependencies.php +++ b/src/Dependencies.php @@ -11,6 +11,7 @@ use MailAccountAdmin\Frontend\Login\LoginController; use MailAccountAdmin\Frontend\Dashboard\DashboardController; use MailAccountAdmin\Repositories\AccountRepository; use MailAccountAdmin\Repositories\AdminUserRepository; +use MailAccountAdmin\Repositories\AliasRepository; use MailAccountAdmin\Repositories\DomainRepository; use PDO; use Psr\Container\ContainerInterface; @@ -70,6 +71,11 @@ class Dependencies $c->get(self::DATABASE), ); }); + $container->set(AliasRepository::class, function (ContainerInterface $c) { + return new AliasRepository( + $c->get(self::DATABASE), + ); + }); // Helper classes $container->set(UserHelper::class, function (ContainerInterface $c) { @@ -104,6 +110,7 @@ class Dependencies $c->get(self::TWIG), $c->get(UserHelper::class), $c->get(AccountRepository::class), + $c->get(AliasRepository::class), ); }); diff --git a/src/Exceptions/AccountNotFoundException.php b/src/Exceptions/AccountNotFoundException.php new file mode 100644 index 0000000..4c9833b --- /dev/null +++ b/src/Exceptions/AccountNotFoundException.php @@ -0,0 +1,8 @@ +accountRepository = $accountRepository; + $this->aliasRepository = $aliasRepository; } public function showAccounts(Request $request, Response $response): Response @@ -34,4 +38,36 @@ class AccountController extends BaseController return $this->view->render($response, 'accounts.html.twig', $renderData); } + + public function showAccountDetails(Request $request, Response $response, array $args): Response + { + // Parse URL arguments + $accountId = (int)$args['id']; + + // Get account data from database + $accountData = $this->accountRepository->fetchAccountById($accountId); + $accountUsername = $accountData['username']; + $accountData['domain'] = explode('@', $accountUsername, 2)[1]; + + // Don't display the password hash, but at least the type of hash (used hash algorithm) + if ($accountData['password'] === '') { + $passwordHashType = 'empty'; + } else { + $passwordHashInfo = password_get_info($accountData['password']); + $passwordHashType = $passwordHashInfo['algoName'] ?? 'unknown'; + } + + // Get list of aliases for this account + $aliases = $this->aliasRepository->fetchAliasesForUserId($accountId); + + $renderData = [ + 'id' => $accountId, + 'accountUsername' => $accountUsername, + 'accountData' => $accountData, + 'passwordHashType' => $passwordHashType, + 'aliases' => $aliases, + ]; + + return $this->view->render($response, 'account_details.html.twig', $renderData); + } } diff --git a/src/Repositories/AccountRepository.php b/src/Repositories/AccountRepository.php index a97e3cd..a506447 100644 --- a/src/Repositories/AccountRepository.php +++ b/src/Repositories/AccountRepository.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace MailAccountAdmin\Repositories; +use MailAccountAdmin\Exceptions\AccountNotFoundException; use PDO; class AccountRepository extends BaseRepository @@ -32,4 +33,16 @@ class AccountRepository extends BaseRepository $statement->execute($queryParams); return $statement->fetchAll(PDO::FETCH_ASSOC); } + + public function fetchAccountById(int $accountId): array + { + $statement = $this->pdo->prepare('SELECT * FROM mail_users WHERE user_id = :user_id LIMIT 1'); + $statement->execute(['user_id' => $accountId]); + + if ($statement->rowCount() < 1) { + throw new AccountNotFoundException("Account with user ID '$accountId' was not found."); + } + + return $statement->fetch(PDO::FETCH_ASSOC); + } } diff --git a/src/Repositories/AliasRepository.php b/src/Repositories/AliasRepository.php new file mode 100644 index 0000000..ac212c2 --- /dev/null +++ b/src/Repositories/AliasRepository.php @@ -0,0 +1,16 @@ +pdo->prepare('SELECT * FROM mail_aliases WHERE user_id = :user_id ORDER BY mail_address'); + $statement->execute(['user_id' => $userId]); + return $statement->fetchAll(PDO::FETCH_ASSOC); + } +} diff --git a/src/Routes.php b/src/Routes.php index 03e0b4a..57a0b87 100644 --- a/src/Routes.php +++ b/src/Routes.php @@ -23,10 +23,11 @@ class Routes // Domains $app->get('/domains', DomainController::class . ':showDomains'); - $app->get('/domains/{foo}', DomainController::class . ':showDomains'); // Accounts $app->get('/accounts', AccountController::class . ':showAccounts'); - $app->get('/accounts/{foo}', AccountController::class . ':showAccounts'); + $app->get('/accounts/{id:[1-9][0-9]*}', AccountController::class . ':showAccountDetails'); + $app->get('/accounts/{id:[1-9][0-9]*}/edit', AccountController::class . ':showAccountDetails'); + $app->get('/accounts/{id:[1-9][0-9]*}/delete', AccountController::class . ':showAccountDetails'); } } diff --git a/templates/account_details.html.twig b/templates/account_details.html.twig new file mode 100644 index 0000000..1dba4dd --- /dev/null +++ b/templates/account_details.html.twig @@ -0,0 +1,82 @@ +{% extends "base.html.twig" %} + +{% block title %}Accounts{% endblock %} + +{% block content %} +

Account: {{ accountUsername }}

+ +

+ Actions: + Edit | + Delete | + Send mail +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User ID{{ accountData['user_id'] }}
Username{{ accountData['username'] }}
Domain{{ accountData['domain'] }}
Password + {% if passwordHashType == 'empty' %} + [No password set] + {% elseif passwordHashType == 'unknown' %} + [Not hashed / unknown hash algorithm] + {% else %} + [{{ passwordHashType }} hash] + {% endif %} +
Status{{ accountData['is_active'] == '1' ? 'Active' : 'Inactive' }}
Home directory/srv/vmail/{{ accountData['home_dir'] }}
Admin memo{{ accountData['memo'] | nl2br }}
Created at{{ accountData['created_at'] }}
Last modified at{{ accountData['modified_at'] }}
+ +

Aliases

+ + {% if aliases %} + + + + + + + {% for alias in aliases %} + + + + {# TODO #} + + {% endfor %} +
AddressCreated atActions
{{ alias['mail_address'] }}{{ alias['created_at'] }}Delete
+ {% else %} +

No aliases.

+ {% endif %} +{% endblock %}