Compare commits

..

No commits in common. "main" and "v0.1.0" have entirely different histories.
main ... v0.1.0

29 changed files with 122 additions and 470 deletions

View File

@ -12,7 +12,7 @@ steps:
environment: environment:
COMPOSER_CACHE_DIR: /tmp/cache COMPOSER_CACHE_DIR: /tmp/cache
commands: commands:
- composer install --no-progress --no-interaction --ignore-platform-reqs - composer install --no-progress --no-interaction
- name: run unit tests - name: run unit tests
image: php:7.4 image: php:7.4

View File

@ -3,23 +3,21 @@
# composer: Set cache directory # composer: Set cache directory
COMPOSER_CACHE_DIR=./.composer COMPOSER_CACHE_DIR=./.composer
# Environment variables for MariaDB container # MariaDB container
MYSQL_RANDOM_ROOT_PASSWORD=yes MYSQL_RANDOM_ROOT_PASSWORD=yes
MYSQL_DATABASE=mailusers MYSQL_DATABASE=mailusers
MYSQL_USER=mailaccountadmin MYSQL_USER=mailaccountadmin
MYSQL_PASSWORD=mailaccountadmin MYSQL_PASSWORD=mailaccountadmin
# App settings # App settings
APP_TITLE="MailAccountAdmin [dev]"
APP_ENV=development
APP_DEBUG=true APP_DEBUG=true
APP_TIMEZONE=Europe/Berlin
# Disable Twig cache # - Disable Twig cache
TWIG_CACHE_DIR= TWIG_CACHE_DIR=
TWIG_STRICT=true
# Database credentials # - Database credentials
DB_HOST=db DB_HOST=db
DB_DATABASE=mailusers DB_DATABASE=mailusers
DB_USERNAME=mailaccountadmin DB_USER=mailaccountadmin
DB_PASSWORD=mailaccountadmin DB_PASSWORD=mailaccountadmin

4
.gitignore vendored
View File

@ -10,9 +10,5 @@
# PHP # PHP
/.composer /.composer
/.phpunit.cache /.phpunit.cache
/.twig.cache
/coverage /coverage
/vendor /vendor
# Production settings
/config/app.yml

View File

@ -3,10 +3,8 @@ FROM php:7.4-apache AS base
WORKDIR /var/www WORKDIR /var/www
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y git unzip libyaml-dev libzip-dev && \ apt-get install -y libzip-dev unzip git && \
docker-php-ext-install pdo pdo_mysql zip && \ docker-php-ext-install pdo pdo_mysql zip
pecl install yaml && \
docker-php-ext-enable yaml
RUN a2enmod rewrite && \ RUN a2enmod rewrite && \
sed -ri -e 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/*.conf sed -ri -e 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/*.conf
@ -20,6 +18,8 @@ RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \
pecl install xdebug && \ pecl install xdebug && \
docker-php-ext-enable xdebug docker-php-ext-enable xdebug
ENV APP_ENV=development
# TODO: production image untested # TODO: production image untested
#FROM base AS production #FROM base AS production
# #

View File

@ -9,7 +9,7 @@ DOCKER_RUN = $(DOCKER_COMPOSE) run --rm app
COMPOSER = $(DOCKER_RUN) composer COMPOSER = $(DOCKER_RUN) composer
PHPUNIT = $(DOCKER_RUN) vendor/bin/phpunit PHPUNIT = $(DOCKER_RUN) vendor/bin/phpunit
.PHONY: all clean version \ .PHONY: all clean VERSION \
docker-up docker-up-detached docker-down docker-restart docker-build docker-rebuild docker-purge docker-logs docker-run \ docker-up docker-up-detached docker-down docker-restart docker-build docker-rebuild docker-purge docker-logs docker-run \
composer-install composer-install-no-dev composer-update composer-cmd \ composer-install composer-install-no-dev composer-update composer-cmd \
test phpunit open-coverage test phpunit open-coverage
@ -99,7 +99,7 @@ open-coverage:
# Create VERSION file from current git tag # Create VERSION file from current git tag
version: version:
git describe --tags | tee VERSION git describe | tee VERSION
# Clean up # Clean up

View File

@ -4,6 +4,7 @@
## General ## General
- Settings from a config file
- Database migrations - Database migrations
- Documentation - Documentation
- App deployment - App deployment
@ -11,7 +12,6 @@
- Refactor auth and session handling - Refactor auth and session handling
## Admin user management ## Admin user management
- Create/edit/delete admins - Create/edit/delete admins
- Change own password - Change own password

View File

@ -14,7 +14,6 @@
"require": { "require": {
"php": "^7.4", "php": "^7.4",
"ext-pdo": "*", "ext-pdo": "*",
"ext-yaml": "*",
"slim/slim": "^4.8", "slim/slim": "^4.8",
"slim/psr7": "^1.3", "slim/psr7": "^1.3",
"php-di/php-di": "^6.3", "php-di/php-di": "^6.3",

View File

@ -1,19 +0,0 @@
# Config file for local development (same settings as .env.develop)
# -- App settings
appTitle: "MailAccountAdmin [dev]"
environment: development
debug: true
timezone: Europe/Berlin
# -- Twig settings
twig:
cacheDir:
# -- Database settings
database:
host: db
port: 3306
name: mailusers
username: mailaccountadmin
password: mailaccountadmin

View File

@ -1,23 +0,0 @@
# This is an example config file for MailAccountAdmin.
# Copy this file to /config/app.yml and change as needed.
# Settings that are commented out represent the default values.
# -- App settings
# appTitle: MailAccountAdmin
# environment: production
# debug: false
# timezone: UTC
# dateTimeFormat: r
# -- Twig settings
twig:
# Cache directory for Twig templates (default: unset, which means no cache is used)
cacheDir: .twig.cache
# -- Database settings
database:
host: localhost
port: 3306
name: mailusers
username: mailaccountadmin
password: very_secret_password

View File

@ -3,25 +3,19 @@ declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
use MailAccountAdmin\Config\Loaders\AutoConfigLoader;
use MailAccountAdmin\Dependencies; use MailAccountAdmin\Dependencies;
use MailAccountAdmin\Middlewares; use MailAccountAdmin\Middlewares;
use MailAccountAdmin\Routes; use MailAccountAdmin\Routes;
use MailAccountAdmin\Settings;
use Slim\Factory\AppFactory; use Slim\Factory\AppFactory;
const ROOT_DIR = __DIR__ . '/..';
session_start(); session_start();
// Load application config (from config file or environment variables) $settings = new Settings();
$configLoader = new AutoConfigLoader(); $container = Dependencies::createContainer($settings);
$config = $configLoader->loadConfig();
// Create application
$container = Dependencies::createContainer($config);
$app = AppFactory::createFromContainer($container); $app = AppFactory::createFromContainer($container);
Middlewares::setMiddlewares($app, $config); Middlewares::setMiddlewares($app, $settings);
Routes::setRoutes($app); Routes::setRoutes($app);
$app->run(); $app->run();

View File

@ -27,10 +27,6 @@ a:hover, a:focus {
text-decoration: underline; text-decoration: underline;
} }
.monospace {
font-family: monospace;
}
.gray { .gray {
color: gray; color: gray;
} }
@ -252,6 +248,7 @@ details > summary {
details > p { details > p {
margin: 0.75rem 1rem; margin: 0.75rem 1rem;
font-family: monospace;
} }
/* -- Detail columns -- */ /* -- Detail columns -- */

View File

@ -35,12 +35,11 @@ CREATE TABLE `mail_users`
CREATE TABLE `mail_aliases` CREATE TABLE `mail_aliases`
( (
`alias_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `alias_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL, `user_id` int(10) unsigned NOT NULL,
`mail_address` varchar(255) NOT NULL, `mail_address` varchar(255) NOT NULL,
`wildcard_priority` smallint(6) NOT NULL DEFAULT 0, `created_at` datetime NOT NULL DEFAULT NOW(),
`created_at` datetime NOT NULL DEFAULT NOW(), `modified_at` datetime NOT NULL DEFAULT NOW(),
`modified_at` datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (`alias_id`), PRIMARY KEY (`alias_id`),
UNIQUE KEY `mail_address` (`mail_address`), UNIQUE KEY `mail_address` (`mail_address`),
KEY `user_id` (`user_id`), KEY `user_id` (`user_id`),

View File

@ -20,7 +20,7 @@ abstract class FormData
throw new InputValidationError("$fieldName is required."); throw new InputValidationError("$fieldName is required.");
} elseif (strlen($raw) < $minLength) { } elseif (strlen($raw) < $minLength) {
throw new InputValidationError("$fieldName is too short (minimum $minLength characters)."); throw new InputValidationError("$fieldName is too short (minimum $minLength characters).");
} elseif (strlen($raw) > $maxLength) { } elseif (strlen($raw) > 100) {
throw new InputValidationError("$fieldName is too long (maximum $maxLength characters)."); throw new InputValidationError("$fieldName is too long (maximum $maxLength characters).");
} }
return $raw; return $raw;
@ -44,42 +44,25 @@ abstract class FormData
// Input validation - Application specific validators // Input validation - Application specific validators
private static function validateMailAddress(string $address, bool $required = true, string $fieldName = 'Mail address'): ?string protected static function validateUsername(string $username, bool $required = true, string $fieldName = 'Username'): ?string
{ {
// Note: This validator allows '%' as wildcard character inside mail addresses. if (!$required && $username === '') {
if (!$required && $address === '') {
return null; return null;
} }
$address = strtolower( $username = strtolower(
self::validateString($address, 3, 100, $fieldName) self::validateString($username, 3, 100, $fieldName)
); );
if (!preg_match('/^[a-z0-9%._+-]+@[a-z0-9%.-]+$/', $address) || preg_match('/^\\.|\\.\\.|\\.@|@\\.|\\.$/', $address)) { if (!preg_match('/^[a-z0-9._+-]+@[a-z0-9.-]+$/', $username) || preg_match('/^\\.|\\.\\.|\\.@|@\\.|\\.$/', $username)) {
throw new InputValidationError("$fieldName is not valid (must be a valid mail address)."); throw new InputValidationError("$fieldName is not valid (must be a valid mail address).");
} }
return $address;
}
protected static function validateUsername(string $username, bool $required = true): ?string
{
$username = self::validateMailAddress($username, $required, 'Username');
if ($username !== null && strpos($username, '%') !== false) {
throw new InputValidationError('Username must not contain the wildcard character "%" (use a wildcard alias instead).');
}
return $username; return $username;
} }
protected static function validateAliasAddress(string $aliasAddress, bool $isWildcard): string protected static function validateAliasAddress(string $aliasAddress): string
{ {
$aliasAddress = self::validateMailAddress($aliasAddress, true, 'Alias address'); return self::validateUsername($aliasAddress, true, 'Alias address');
// Check if the address contains a wildcard character
if (!$isWildcard && strpos($aliasAddress, '%') !== false) {
throw new InputValidationError('Non-wildcard alias address must not contain "%" character.');
}
return $aliasAddress;
} }
protected static function validatePassword(string $password, string $passwordRepeat, bool $required = true): ?string protected static function validatePassword(string $password, string $passwordRepeat, bool $required = true): ?string

View File

@ -1,105 +0,0 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config;
class AppConfig
{
// App settings
protected string $appTitle = 'MailAccountAdmin';
protected string $environment = 'production';
protected bool $debug = false;
protected string $timezone;
protected string $dateTimeFormat = 'r';
// Twig settings
protected ?string $twigCacheDir = null;
// Database settings
protected string $databaseHost = 'localhost';
protected int $databasePort = 3306;
protected string $databaseName = '';
protected string $databaseUsername = '';
protected string $databasePassword = '';
protected function __construct()
{
// Set default timezone from php.ini
$this->timezone = ini_get('date.timezone') ?: 'UTC';
}
public static function createFromArray(array $configArray): self
{
$config = new self();
foreach ($configArray as $key => $value) {
assert(property_exists($config, $key));
if ($value !== null) {
$config->$key = $value;
}
}
return $config;
}
public function getAppTitle(): string
{
return $this->appTitle;
}
public function getAppEnvironment(): string
{
return $this->environment;
}
public function isDebugMode(): bool
{
return $this->debug;
}
public function getTimezone(): string
{
return $this->timezone;
}
public function getDateTimeFormat(): string
{
// Specify datetime format (https://www.php.net/manual/en/datetime.format.php)
// Default value "r": RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200)
return $this->dateTimeFormat;
}
public function getTwigCacheDir(): ?string
{
if (empty($this->twigCacheDir)) {
return null;
} elseif (substr($this->twigCacheDir, 0, 1) === '/') {
// Absolute path
return $this->twigCacheDir;
} else {
// Relative path
return ROOT_DIR . '/' . $this->twigCacheDir;
}
}
public function getTwigSettings(): array
{
return [
'cache' => $this->getTwigCacheDir() ?: false,
'debug' => $this->isDebugMode(),
'strict_variables' => $this->isDebugMode(),
];
}
public function getDatabaseSettings(): array
{
return [
'host' => $this->databaseHost,
'port' => $this->databasePort,
'dbname' => $this->databaseName,
'username' => $this->databaseUsername,
'password' => $this->databasePassword,
];
}
}

View File

@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config\Loaders;
use MailAccountAdmin\Config\AppConfig;
class AutoConfigLoader implements ConfigLoaderInterface
{
private ConfigLoaderInterface $configLoader;
public function __construct()
{
$yamlFilePath = ROOT_DIR . '/config/app.yml';
// Check if yml config file exists
if (file_exists($yamlFilePath)) {
$this->configLoader = new YamlConfigLoader($yamlFilePath);
} else {
$this->configLoader = new EnvConfigLoader();
}
}
public function loadConfig(): AppConfig
{
return $this->configLoader->loadConfig();
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config\Loaders;
use MailAccountAdmin\Config\AppConfig;
interface ConfigLoaderInterface
{
public function loadConfig(): AppConfig;
}

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config\Loaders;
use MailAccountAdmin\Config\AppConfig;
class EnvConfigLoader implements ConfigLoaderInterface
{
public function loadConfig(): AppConfig
{
return AppConfig::createFromArray([
// App settings
'appTitle' => getenv('APP_TITLE') ?: null,
'environment' => getenv('APP_ENV') ?: null,
'debug' => getenv('APP_DEBUG') === 'true' ? true : null,
'timezone' => getenv('APP_TIMEZONE') ?: null,
'dateTimeFormat' => getenv('APP_DATE_TIME_FORMAT') ?: null,
// Twig settings
'twigCacheDir' => getenv('TWIG_CACHE_DIR') ?: null,
// Database settings
'databaseHost' => getenv('DB_HOST') ?: null,
'databasePort' => (int)getenv('DB_PORT') ?: null,
'databaseName' => getenv('DB_DATABASE') ?: null,
'databaseUsername' => getenv('DB_USERNAME') ?: null,
'databasePassword' => getenv('DB_PASSWORD') ?: null,
]);
}
}

View File

@ -1,48 +0,0 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config\Loaders;
use MailAccountAdmin\Config\AppConfig;
class YamlConfigLoader implements ConfigLoaderInterface
{
private string $filePath;
public function __construct(string $filePath)
{
assert(file_exists($filePath));
$this->filePath = $filePath;
}
public function loadConfig(): AppConfig
{
// Parse yml config file
$parsedConfig = yaml_parse_file($this->filePath);
// Check datatypes
assert(is_array($parsedConfig));
assert(!isset($parsedConfig['twig']) || is_array($parsedConfig['twig']));
assert(!isset($parsedConfig['database']) || is_array($parsedConfig['database']));
return AppConfig::createFromArray([
// App settings
'appTitle' => $parsedConfig['appTitle'] ?? null,
'environment' => $parsedConfig['environment'] ?? null,
'debug' => isset($parsedConfig['debug']) ? (bool)$parsedConfig['debug'] : null,
'timezone' => $parsedConfig['timezone'] ?? null,
'dateTimeFormat' => $parsedConfig['dateTimeFormat'] ?? null,
// Twig settings
'twigCacheDir' => $parsedConfig['twig']['cacheDir'] ?? null,
// Database settings
'databaseHost' => $parsedConfig['database']['host'] ?? null,
'databasePort' => isset($parsedConfig['database']['port']) ? (int)$parsedConfig['database']['port'] : null,
'databaseName' => $parsedConfig['database']['name'] ?? null,
'databaseUsername' => $parsedConfig['database']['username'] ?? null,
'databasePassword' => $parsedConfig['database']['password'] ?? null,
]);
}
}

View File

@ -7,7 +7,6 @@ use DI\Container;
use MailAccountAdmin\Common\PasswordHelper; use MailAccountAdmin\Common\PasswordHelper;
use MailAccountAdmin\Common\SessionHelper; use MailAccountAdmin\Common\SessionHelper;
use MailAccountAdmin\Common\UserHelper; use MailAccountAdmin\Common\UserHelper;
use MailAccountAdmin\Config\AppConfig;
use MailAccountAdmin\Frontend\Accounts\AccountController; use MailAccountAdmin\Frontend\Accounts\AccountController;
use MailAccountAdmin\Frontend\Accounts\AccountHandler; use MailAccountAdmin\Frontend\Accounts\AccountHandler;
use MailAccountAdmin\Frontend\Domains\DomainController; use MailAccountAdmin\Frontend\Domains\DomainController;
@ -24,43 +23,43 @@ use Twig\Extension\CoreExtension as TwigCoreExtension;
class Dependencies class Dependencies
{ {
public const CONFIG = 'config'; public const SETTINGS = 'settings';
public const TWIG = 'view'; public const TWIG = 'view';
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates'; private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
public const DATABASE = 'database'; public const DATABASE = 'database';
public static function createContainer(AppConfig $config): Container public static function createContainer(Settings $settings): Container
{ {
$container = new Container(); $container = new Container();
// App configuration // App settings
$container->set(self::CONFIG, $config); $container->set(self::SETTINGS, $settings);
// App information // App information
$container->set(AppInfo::class, function (ContainerInterface $c) { $container->set(AppInfo::class, function (ContainerInterface $c) {
/** @var AppConfig $config */ /** @var Settings $settings */
$config = $c->get(self::CONFIG); $settings = $c->get(self::SETTINGS);
$versionHelper = new VersionHelper($config); $versionHelper = new VersionHelper();
return new AppInfo( return new AppInfo(
$config->getAppTitle(), $settings->getAppTitle(),
$versionHelper->getAppVersion(), $versionHelper->getAppVersion(),
); );
}); });
// Twig template engine // Twig template engine
$container->set(self::TWIG, function (ContainerInterface $c) { $container->set(self::TWIG, function (ContainerInterface $c) {
/** @var AppConfig $config */ /** @var Settings $settings */
$config = $c->get(self::CONFIG); $settings = $c->get(self::SETTINGS);
// Create Twig view // Create Twig view
$twig = Twig::create(self::TWIG_TEMPLATE_DIR, $config->getTwigSettings()); $twig = Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings());
// Set default date format // Set default date format
/** @var TwigCoreExtension $coreExtension */ /** @var TwigCoreExtension $coreExtension */
$coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class); $coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class);
$coreExtension->setDateFormat($config->getDateTimeFormat()); $coreExtension->setDateFormat($settings->getDateFormat());
$coreExtension->setTimezone($config->getTimezone()); $coreExtension->setTimezone($settings->getTimezone());
// Add app information to globals // Add app information to globals
$appInfo = $c->get(AppInfo::class); $appInfo = $c->get(AppInfo::class);
@ -72,9 +71,9 @@ class Dependencies
// Database connection // Database connection
$container->set(self::DATABASE, function (ContainerInterface $c) { $container->set(self::DATABASE, function (ContainerInterface $c) {
/** @var AppConfig $config */ /** @var Settings $settings */
$config = $c->get(self::CONFIG); $settings = $c->get(self::SETTINGS);
$dbSettings = $config->getDatabaseSettings(); $dbSettings = $settings->getDatabaseSettings();
return new PDO( return new PDO(
"mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}", "mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}",
@ -109,7 +108,7 @@ class Dependencies
}); });
// Helper classes // Helper classes
$container->set(SessionHelper::class, function () { $container->set(SessionHelper::class, function (ContainerInterface $c) {
return new SessionHelper(); return new SessionHelper();
}); });
@ -121,7 +120,7 @@ class Dependencies
); );
}); });
$container->set(PasswordHelper::class, function () { $container->set(PasswordHelper::class, function (ContainerInterface $c) {
return new PasswordHelper(); return new PasswordHelper();
}); });

View File

@ -4,34 +4,20 @@ declare(strict_types=1);
namespace MailAccountAdmin\Frontend\Accounts; namespace MailAccountAdmin\Frontend\Accounts;
use MailAccountAdmin\Common\FormData; use MailAccountAdmin\Common\FormData;
use MailAccountAdmin\Exceptions\InputValidationError;
class AccountAddAliasData extends FormData class AccountAddAliasData extends FormData
{ {
private string $aliasAddress; private string $aliasAddress;
private bool $isWildcard;
private int $wildcardPriority;
private function __construct(string $aliasAddress, bool $isWildcard, int $wildcardPriority) private function __construct(string $aliasAddress)
{ {
if ($isWildcard && $wildcardPriority === 0) {
throw new InputValidationError('Wildcard alias must have a wildcard priority other than 0.');
} elseif (!$isWildcard) {
$wildcardPriority = 0;
}
$this->aliasAddress = $aliasAddress; $this->aliasAddress = $aliasAddress;
$this->isWildcard = $isWildcard;
$this->wildcardPriority = $wildcardPriority;
} }
public static function createFromArray(array $raw): self public static function createFromArray(array $raw): self
{ {
$isWildcard = self::validateBoolOption(trim($raw['is_wildcard'] ?? ''));
return new self( return new self(
self::validateAliasAddress(trim($raw['alias_address'] ?? ''), $isWildcard), self::validateAliasAddress(trim($raw['alias_address'] ?? '')),
$isWildcard,
self::validateInteger(trim($raw['wildcard_priority'] ?? '')),
); );
} }
@ -39,14 +25,4 @@ class AccountAddAliasData extends FormData
{ {
return $this->aliasAddress; return $this->aliasAddress;
} }
public function isWildcard(): bool
{
return $this->isWildcard;
}
public function getWildcardPriority(): int
{
return $this->wildcardPriority;
}
} }

View File

@ -278,22 +278,14 @@ class AccountHandler
// Check if account exists // Check if account exists
$this->ensureAccountExists($accountId); $this->ensureAccountExists($accountId);
$unescapedAddress = $address = $aliasAddData->getAliasAddress();
$wildcardPriority = 0;
if ($aliasAddData->isWildcard()) {
// If it's a wildcard alias, escape underscores
$address = str_replace('_', '\\_', $address);
$wildcardPriority = $aliasAddData->getWildcardPriority();
}
// Check if alias address is still available // Check if alias address is still available
$address = $aliasAddData->getAliasAddress();
if (!$this->accountRepository->checkUsernameAvailable($address) || !$this->aliasRepository->checkAliasAvailable($address)) { if (!$this->accountRepository->checkUsernameAvailable($address) || !$this->aliasRepository->checkAliasAvailable($address)) {
throw new InputValidationError("Alias address \"$unescapedAddress\" is not available."); throw new InputValidationError("Alias address \"$address\" is not available.");
} }
// Create alias in database // Create alias in database
$this->aliasRepository->createNewAlias($accountId, $address, $wildcardPriority); $this->aliasRepository->createNewAlias($accountId, $address);
} }

View File

@ -4,15 +4,14 @@ declare(strict_types=1);
namespace MailAccountAdmin; namespace MailAccountAdmin;
use MailAccountAdmin\Auth\AuthMiddleware; use MailAccountAdmin\Auth\AuthMiddleware;
use MailAccountAdmin\Config\AppConfig;
use Slim\App; use Slim\App;
use Slim\Views\TwigMiddleware; use Slim\Views\TwigMiddleware;
class Middlewares class Middlewares
{ {
public static function setMiddlewares(App $app, AppConfig $config): void public static function setMiddlewares(App $app, Settings $settings): void
{ {
$displayErrorDetails = $config->isDebugMode(); $displayErrorDetails = $settings->isDebugMode();
$app->addErrorMiddleware($displayErrorDetails, true, true); $app->addErrorMiddleware($displayErrorDetails, true, true);
$app->add(new AuthMiddleware()); $app->add(new AuthMiddleware());

View File

@ -10,23 +10,14 @@ class Alias
private int $id; private int $id;
private int $userId; private int $userId;
private string $mailAddress; private string $mailAddress;
private int $wildcardPriority;
private DateTimeImmutable $createdAt; private DateTimeImmutable $createdAt;
private DateTimeImmutable $modifiedAt; private DateTimeImmutable $modifiedAt;
private function __construct( private function __construct(int $id, int $userId, string $mailAddress, DateTimeImmutable $createdAt, DateTimeImmutable $modifiedAt)
int $id,
int $userId,
string $mailAddress,
int $wildcardPriority,
DateTimeImmutable $createdAt,
DateTimeImmutable $modifiedAt
)
{ {
$this->id = $id; $this->id = $id;
$this->userId = $userId; $this->userId = $userId;
$this->mailAddress = $mailAddress; $this->mailAddress = $mailAddress;
$this->wildcardPriority = $wildcardPriority;
$this->createdAt = $createdAt; $this->createdAt = $createdAt;
$this->modifiedAt = $modifiedAt; $this->modifiedAt = $modifiedAt;
} }
@ -37,7 +28,6 @@ class Alias
(int)$data['alias_id'], (int)$data['alias_id'],
(int)$data['user_id'], (int)$data['user_id'],
$data['mail_address'], $data['mail_address'],
(int)$data['wildcard_priority'],
new DateTimeImmutable($data['created_at']), new DateTimeImmutable($data['created_at']),
new DateTimeImmutable($data['modified_at']), new DateTimeImmutable($data['modified_at']),
); );
@ -53,24 +43,11 @@ class Alias
return $this->userId; return $this->userId;
} }
public function getMailAddress(bool $unescape = true): string public function getMailAddress(): string
{ {
if ($this->isWildcard() && $unescape) {
return str_replace('\\_', '_', $this->mailAddress);
}
return $this->mailAddress; return $this->mailAddress;
} }
public function isWildcard(): bool
{
return $this->wildcardPriority !== 0;
}
public function getWildcardPriority(): int
{
return $this->wildcardPriority;
}
public function getCreatedAt(): DateTimeImmutable public function getCreatedAt(): DateTimeImmutable
{ {
return $this->createdAt; return $this->createdAt;

View File

@ -11,11 +11,11 @@ class AliasRepository extends BaseRepository
{ {
public function fetchAliasesForUserId(int $userId): array public function fetchAliasesForUserId(int $userId): array
{ {
$statement = $this->pdo->prepare('SELECT * FROM mail_aliases WHERE user_id = :user_id ORDER BY wildcard_priority, mail_address'); $statement = $this->pdo->prepare('SELECT * FROM mail_aliases WHERE user_id = :user_id ORDER BY mail_address');
$statement->execute(['user_id' => $userId]); $statement->execute(['user_id' => $userId]);
$rows = $statement->fetchAll(PDO::FETCH_ASSOC); $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
// Create Alias models from rows // Create Account models from rows
$aliasList = []; $aliasList = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$aliasList[] = Alias::createFromArray($row); $aliasList[] = Alias::createFromArray($row);
@ -43,20 +43,12 @@ class AliasRepository extends BaseRepository
return $statement->rowCount() === 0; return $statement->rowCount() === 0;
} }
public function createNewAlias(int $userId, string $mailAddress, int $wildcardPriority = 0): void public function createNewAlias(int $userId, string $mailAddress): void
{ {
$query = ' $statement = $this->pdo->prepare('INSERT INTO mail_aliases (user_id, mail_address) VALUES (:user_id, :mail_address)');
INSERT INTO mail_aliases
(user_id, mail_address, wildcard_priority)
VALUES
(:user_id, :mail_address, :wildcard_priority)
';
$statement = $this->pdo->prepare($query);
$statement->execute([ $statement->execute([
'user_id' => $userId, 'user_id' => $userId,
'mail_address' => $mailAddress, 'mail_address' => $mailAddress,
'wildcard_priority' => $wildcardPriority,
]); ]);
} }

49
src/Settings.php Normal file
View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin;
class Settings
{
public function getAppTitle(): string
{
return getenv('APP_TITLE') ?: 'MailAccountAdmin';
}
public function isDebugMode(): bool
{
return getenv('APP_DEBUG') === 'true';
}
public function getTimezone(): string
{
return 'Europe/Berlin';
}
public function getDateFormat(): string
{
// Default date format (https://www.php.net/manual/en/datetime.format.php)
// RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200)
return 'r';
}
public function getTwigSettings(): array
{
return [
'cache' => getenv('TWIG_CACHE_DIR') ?: false,
'debug' => $this->isDebugMode(),
'strict_variables' => getenv('TWIG_STRICT') === 'true',
];
}
public function getDatabaseSettings(): array
{
return [
'host' => getenv('DB_HOST') ?: 'localhost',
'port' => getenv('DB_PORT') ?: 3306,
'dbname' => getenv('DB_DATABASE') ?: '',
'username' => getenv('DB_USER') ?: '',
'password' => getenv('DB_PASSWORD') ?: '',
];
}
}

View File

@ -3,18 +3,16 @@ declare(strict_types=1);
namespace MailAccountAdmin; namespace MailAccountAdmin;
use MailAccountAdmin\Config\AppConfig;
class VersionHelper class VersionHelper
{ {
private string $version; private string $version;
public function __construct(AppConfig $config) public function __construct()
{ {
$version = $this->loadFromVersionFile(); $version = $this->loadFromVersionFile();
if (!empty($version)) { if (!empty($version)) {
$this->version = $version; $this->version = $version;
} elseif ($config->getAppEnvironment() === 'development') { } elseif ($this->inDevelopmentMode()) {
$this->version = '[dev version]'; $this->version = '[dev version]';
} else { } else {
$this->version = '[undefined version]'; $this->version = '[undefined version]';
@ -36,4 +34,9 @@ class VersionHelper
} }
return null; return null;
} }
private function inDevelopmentMode(): bool
{
return getenv('APP_ENV') === 'development';
}
} }

View File

@ -24,7 +24,7 @@
</p> </p>
<details> <details>
<summary>Show list of known domains</summary> <summary>Show list of known domains</summary>
<p class="monospace">{{ domainList ? domainList | join(', ') : 'No domains exist yet.' }}</p> <p>{{ domainList ? domainList | join(', ') : 'No domains exist yet.' }}</p>
</details> </details>
<table> <table>
<tr> <tr>

View File

@ -71,21 +71,13 @@
<table class="bordered_table"> <table class="bordered_table">
<tr> <tr>
<th></th> <th></th>
<th style="min-width: 16em">Address</th> <th>Address</th>
<th>Wildcard (priority)</th>
<th>Created at</th> <th>Created at</th>
</tr> </tr>
{% for alias in aliases %} {% for alias in aliases %}
<tr> <tr>
<td><input type="checkbox" id="delete_selected_{{ alias.getId() }}" name="selected_aliases[]" value="{{ alias.getId() }}"/></td> <td><input type="checkbox" id="delete_selected_{{ alias.getId() }}" name="selected_aliases[]" value="{{ alias.getId() }}"/></td>
<td><label for="delete_selected_{{ alias.getId() }}">{{ alias.getMailAddress() }}</label></td> <td><label for="delete_selected_{{ alias.getId() }}">{{ alias.getMailAddress() }}</label></td>
<td>
{% if alias.isWildcard() %}
Yes ({{ alias.getWildcardPriority() }})
{% else %}
<span class="gray">No</span>
{% endif %}
</td>
<td>{{ alias.getCreatedAt() | date }}</td> <td>{{ alias.getCreatedAt() | date }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
@ -103,18 +95,7 @@
<tr> <tr>
<td><label for="add_alias_address">New alias:</label></td> <td><label for="add_alias_address">New alias:</label></td>
<td> <td>
<input id="add_alias_address" name="alias_address" value="{{ formData['alias_address'] | default('') }}" {{ success is defined or error is defined ? 'autofocus': '' }}/> <input id="add_alias_address" name="alias_address" value="{{ formData['new_alias'] | default('') }}" {{ success is defined or error is defined ? 'autofocus': '' }}/>
</td>
</tr>
<tr>
<td style="vertical-align: top">Wildcard:</td>
<td style="vertical-align: top">
<label>
<input type="checkbox" name="is_wildcard" {{ formData is defined and formData['is_wildcard'] | default() ? 'checked' : '' }}/>
Create wildcard alias,
</label>
<label for="add_alias_wildcard_priority">priority:</label>
<input type="number" id="add_alias_wildcard_priority" name="wildcard_priority" value="{{ formData['wildcard_priority'] | default('1') }}" style="width: 6em"/>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -123,18 +104,5 @@
</td> </td>
</tr> </tr>
</table> </table>
<details>
<summary>How to define wildcard aliases?</summary>
<p>
Wildcard aliases use <code>%</code> as a placeholder for any amount of characters (zero or more),
e.g. <code>%@example.com</code> or <code>example-%@example.com</code>.
</p>
<p>
Additionally, a <b>wildcard priority</b> must be specified. When a mail address is looked up that
matches multiple (wildcard) aliases, the alias with the <b>lowest</b> wildcard priority will be used.
The priority must not be 0 (internally, the value 0 stands for regular non-wildcard aliases).
</p>
</details>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -12,11 +12,7 @@
<header> <header>
<h1>{{ app_info.getTitle() }}</h1> <h1>{{ app_info.getTitle() }}</h1>
<div class="header_appversion"> <div class="header_appversion">
{% if logged_in | default() %} <a href="{{ app_info.getRepositoryUrl() }}">{{ app_info.getVersion() }}</a>
<a href="{{ app_info.getRepositoryUrl() }}">{{ app_info.getVersion() }}</a>
{% else %}
{{ app_info.getVersion() }}
{% endif %}
</div> </div>
<div class="header_spacer"></div> <div class="header_spacer"></div>
<div class="header_userstatus"> <div class="header_userstatus">