107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MailAccountAdmin\Repositories;
|
|
|
|
use MailAccountAdmin\Exceptions\AccountNotFoundException;
|
|
use MailAccountAdmin\Models\Account;
|
|
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);
|
|
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Create Account models from rows
|
|
$accountList = [];
|
|
foreach ($rows as $row) {
|
|
$accountList[] = Account::createFromArray($row);
|
|
}
|
|
return $accountList;
|
|
}
|
|
|
|
/**
|
|
* @throws AccountNotFoundException
|
|
*/
|
|
public function fetchAccountById(int $accountId): Account
|
|
{
|
|
$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.");
|
|
}
|
|
|
|
$row = $statement->fetch(PDO::FETCH_ASSOC);
|
|
return Account::createFromArray($row);
|
|
}
|
|
|
|
public function checkUsernameAvailable(string $username): bool
|
|
{
|
|
$statement = $this->pdo->prepare('SELECT 1 FROM mail_users WHERE username = :username LIMIT 1');
|
|
$statement->execute(['username' => $username]);
|
|
return $statement->rowCount() === 0;
|
|
}
|
|
|
|
public function updateAccountWithId(int $accountId, ?string $newUsername, ?string $newPasswordHash, bool $newActive,
|
|
?string $newHomeDir, string $newMemo): void
|
|
{
|
|
$queryParams = [
|
|
'user_id' => $accountId,
|
|
'new_memo' => $newMemo,
|
|
'new_active' => $newActive ? '1' : '0',
|
|
];
|
|
|
|
$querySet = '';
|
|
if (isset($newUsername)) {
|
|
$querySet .= 'username = :new_username, ';
|
|
$queryParams['new_username'] = $newUsername;
|
|
}
|
|
if (isset($newPasswordHash)) {
|
|
$querySet .= 'password = :new_password, ';
|
|
$queryParams['new_password'] = $newPasswordHash;
|
|
}
|
|
if (isset($newHomeDir)) {
|
|
$querySet .= 'home_dir = :new_home_dir, ';
|
|
$queryParams['new_home_dir'] = $newHomeDir;
|
|
}
|
|
|
|
$query = '
|
|
UPDATE mail_users
|
|
SET
|
|
' . $querySet . '
|
|
memo = :new_memo,
|
|
is_active = :new_active,
|
|
modified_at = CURRENT_TIMESTAMP()
|
|
WHERE
|
|
user_id = :user_id
|
|
LIMIT 1
|
|
';
|
|
|
|
$statement = $this->pdo->prepare($query);
|
|
$statement->execute($queryParams);
|
|
}
|
|
}
|