Accounts: Fetch list of all accounts (optionally filtered by domain)

This commit is contained in:
Lexi / Zoe 2021-07-30 03:46:43 +02:00
parent 279f4492e8
commit 985f46c652
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
6 changed files with 145 additions and 9 deletions

View File

@ -112,6 +112,10 @@ a:hover, a:focus {
padding: 1em;
}
.gray {
color: gray;
}
button {
padding: 0.2em 1em;
}
@ -121,3 +125,27 @@ table, tr, td, th {
border-collapse: collapse;
padding: 0.25em 0.5em;
}
.inactive {
color: gray;
}
/* --- Filter options --- */
.filter_options {
border: 1px solid #999999;
padding: 1em;
margin: 1em 0;
}
.filter_options h4 {
margin: 0 0 0.5em 0;
}
/* --- Detail columns --- */
input#show_details_checkbox {
margin-bottom: 1em;
}
input#show_details_checkbox:not(:checked) ~ table .detail_column {
display: none;
}

View File

@ -9,6 +9,7 @@ use MailAccountAdmin\Frontend\Accounts\AccountController;
use MailAccountAdmin\Frontend\Domains\DomainController;
use MailAccountAdmin\Frontend\Login\LoginController;
use MailAccountAdmin\Frontend\Dashboard\DashboardController;
use MailAccountAdmin\Repositories\AccountRepository;
use MailAccountAdmin\Repositories\AdminUserRepository;
use MailAccountAdmin\Repositories\DomainRepository;
use PDO;
@ -64,6 +65,11 @@ class Dependencies
$c->get(self::DATABASE),
);
});
$container->set(AccountRepository::class, function (ContainerInterface $c) {
return new AccountRepository(
$c->get(self::DATABASE),
);
});
// Helper classes
$container->set(UserHelper::class, function (ContainerInterface $c) {
@ -97,6 +103,7 @@ class Dependencies
return new AccountController(
$c->get(self::TWIG),
$c->get(UserHelper::class),
$c->get(AccountRepository::class),
);
});

View File

@ -3,15 +3,33 @@ declare(strict_types=1);
namespace MailAccountAdmin\Frontend\Accounts;
use MailAccountAdmin\Common\UserHelper;
use MailAccountAdmin\Frontend\BaseController;
use MailAccountAdmin\Repositories\AccountRepository;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
class AccountController extends BaseController
{
/** @var AccountRepository */
private $accountRepository;
public function __construct(Twig $view, UserHelper $userHelper, AccountRepository $accountRepository)
{
parent::__construct($view, $userHelper);
$this->accountRepository = $accountRepository;
}
public function showAccounts(Request $request, Response $response): Response
{
// Parse query parameters for filters
$queryParams = $request->getQueryParams();
$filterByDomain = $queryParams['domain'] ?? null;
$renderData = [
'filterDomain' => $filterByDomain,
'accountList' => $this->accountRepository->fetchAccountList($filterByDomain),
];
return $this->view->render($response, 'accounts.html.twig', $renderData);

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Repositories;
use PDO;
class AccountRepository extends BaseRepository
{
public function fetchAccountList(string $filterByDomain = null): array
{
$queryWhere = '';
$queryParams = [];
if (!empty($filterByDomain)) {
$queryWhere = 'WHERE REGEXP_REPLACE(username, "^.*@", "") LIKE :domain';
$queryParams['domain'] = str_replace('*', '%', $filterByDomain);
}
$query = '
SELECT
mail_users.*,
REGEXP_REPLACE(username, "^.*@", "") AS domain,
COUNT(alias_id) AS alias_count
FROM mail_users
LEFT JOIN mail_aliases ON mail_users.user_id = mail_aliases.user_id
' . $queryWhere . '
GROUP BY username
ORDER BY domain, username
';
$statement = $this->pdo->prepare($query);
$statement->execute($queryParams);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
}

View File

@ -5,6 +5,48 @@
{% block content %}
<h2>Accounts</h2>
<p>List of accounts ... <b>TODO</b></p>
<p><a href="/accounts/42">Test</a></p>
<p>List of all mail accounts.</p>
<div class="filter_options">
<form action="/accounts" method="GET">
<h4>Filter:</h4>
<label for="filter_domain">Domain: </label>
<input name="domain" id="filter_domain" value="{{ filterDomain | default('') }}"/>
<button type="submit">Apply</button>
<button type="reset" onclick="location.href='/accounts'">Clear</button>
</form>
</div>
<input type="checkbox" id="show_details_checkbox"><label for="show_details_checkbox"> Show detail columns</label>
<table>
<tr>
<th style="min-width: 15em">Username</th>
<th style="min-width: 10em">Domain</th>
<th>Aliases</th>
<th>Active</th>
<th class="detail_column">Home directory</th>
<th class="detail_column" style="min-width: 10em">Memo</th>
<th>Created</th>
<th class="detail_column">Last modified</th>
</tr>
{% if accountList %}
{% for account in accountList -%}
<tr{% if account['is_active'] != 1 %} class="inactive"{% endif %}>
<td><a href="/accounts/{{ account['user_id'] }}">{{ account['username'] }}</a></td>
<td><a href="/accounts?domain={{ account['domain'] }}">{{ account['domain'] }}</a></td>
<td>{{ account['alias_count'] }}</td>
<td>{{ account['is_active'] == 1 ? 'Yes' : 'No' }}</td>
<td class="detail_column"><span class="gray">vmail/</span>{{ account['home_dir'] }}</td>
<td class="detail_column">{{ account['memo'] }}</td>
<td>{{ account['created_at'] }}</td>
<td class="detail_column">{{ account['modified_at'] }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="100" style="text-align: center">No accounts found.</td>
</tr>
{% endif %}
</table>
{% endblock %}

View File

@ -13,12 +13,18 @@
<th>Accounts</th>
<th>Aliases</th>
</tr>
{% for domain, domainCounts in domainList -%}
<tr>
<td><a href="/accounts?domain={{ domain | escape('url') }}">{{ domain }}</a></td>
<td>{{ domainCounts['accounts'] }}</td>
<td>{{ domainCounts['aliases'] }}</td>
</tr>
{% endfor %}
{% if domainList %}
{% for domain, domainCounts in domainList -%}
<tr>
<td><a href="/accounts?domain={{ domain | escape('url') }}">{{ domain }}</a></td>
<td>{{ domainCounts['accounts'] }}</td>
<td>{{ domainCounts['aliases'] }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="100" style="text-align: center">No domains found.</td>
</tr>
{% endif %}
</table>
{% endblock %}