diff --git a/.drone.yml b/.drone.yml index 4d9dd2d..5243887 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,7 +12,7 @@ steps: environment: COMPOSER_CACHE_DIR: /tmp/cache commands: - - composer install --no-progress --no-interaction --ignore-platform-reqs + - composer install --no-progress --no-interaction - name: run unit tests image: php:7.4 diff --git a/.env.develop b/.env.develop index f6a431d..968fd4b 100644 --- a/.env.develop +++ b/.env.develop @@ -3,23 +3,21 @@ # composer: Set cache directory COMPOSER_CACHE_DIR=./.composer -# Environment variables for MariaDB container +# MariaDB container MYSQL_RANDOM_ROOT_PASSWORD=yes MYSQL_DATABASE=mailusers MYSQL_USER=mailaccountadmin MYSQL_PASSWORD=mailaccountadmin # App settings -APP_TITLE="MailAccountAdmin [dev]" -APP_ENV=development APP_DEBUG=true -APP_TIMEZONE=Europe/Berlin -# Disable Twig cache +# - Disable Twig cache TWIG_CACHE_DIR= +TWIG_STRICT=true -# Database credentials +# - Database credentials DB_HOST=db DB_DATABASE=mailusers -DB_USERNAME=mailaccountadmin +DB_USER=mailaccountadmin DB_PASSWORD=mailaccountadmin diff --git a/.gitignore b/.gitignore index 37cecc0..646c542 100644 --- a/.gitignore +++ b/.gitignore @@ -10,9 +10,5 @@ # PHP /.composer /.phpunit.cache -/.twig.cache /coverage /vendor - -# Production settings -/config/app.yml diff --git a/Dockerfile b/Dockerfile index 89effb8..695e69b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,10 +3,8 @@ FROM php:7.4-apache AS base WORKDIR /var/www RUN apt-get update && \ - apt-get install -y git unzip libyaml-dev libzip-dev && \ - docker-php-ext-install pdo pdo_mysql zip && \ - pecl install yaml && \ - docker-php-ext-enable yaml + apt-get install -y libzip-dev unzip git && \ + docker-php-ext-install pdo pdo_mysql zip RUN a2enmod rewrite && \ 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 && \ docker-php-ext-enable xdebug +ENV APP_ENV=development + # TODO: production image untested #FROM base AS production # diff --git a/Makefile b/Makefile index 8f22fbd..f0ffbb1 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ DOCKER_RUN = $(DOCKER_COMPOSE) run --rm app COMPOSER = $(DOCKER_RUN) composer 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 \ composer-install composer-install-no-dev composer-update composer-cmd \ test phpunit open-coverage @@ -99,7 +99,7 @@ open-coverage: # Create VERSION file from current git tag version: - git describe --tags | tee VERSION + git describe | tee VERSION # Clean up diff --git a/TODO.md b/TODO.md index 4697f74..2226cf1 100644 --- a/TODO.md +++ b/TODO.md @@ -4,6 +4,7 @@ ## General +- Settings from a config file - Database migrations - Documentation - App deployment @@ -11,7 +12,6 @@ - Refactor auth and session handling ## Admin user management - - Create/edit/delete admins - Change own password diff --git a/composer.json b/composer.json index aa59362..93dcd53 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,6 @@ "require": { "php": "^7.4", "ext-pdo": "*", - "ext-yaml": "*", "slim/slim": "^4.8", "slim/psr7": "^1.3", "php-di/php-di": "^6.3", diff --git a/config/app.develop.yml b/config/app.develop.yml deleted file mode 100644 index 923ceb4..0000000 --- a/config/app.develop.yml +++ /dev/null @@ -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 diff --git a/config/app.example.yml b/config/app.example.yml deleted file mode 100644 index 2683c29..0000000 --- a/config/app.example.yml +++ /dev/null @@ -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 diff --git a/public/index.php b/public/index.php index c918330..37641db 100644 --- a/public/index.php +++ b/public/index.php @@ -3,25 +3,19 @@ declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; -use MailAccountAdmin\Config\Loaders\AutoConfigLoader; use MailAccountAdmin\Dependencies; use MailAccountAdmin\Middlewares; use MailAccountAdmin\Routes; +use MailAccountAdmin\Settings; use Slim\Factory\AppFactory; -const ROOT_DIR = __DIR__ . '/..'; - session_start(); -// Load application config (from config file or environment variables) -$configLoader = new AutoConfigLoader(); -$config = $configLoader->loadConfig(); - -// Create application -$container = Dependencies::createContainer($config); +$settings = new Settings(); +$container = Dependencies::createContainer($settings); $app = AppFactory::createFromContainer($container); -Middlewares::setMiddlewares($app, $config); +Middlewares::setMiddlewares($app, $settings); Routes::setRoutes($app); $app->run(); diff --git a/public/static/style.css b/public/static/style.css index 02f1dbc..3806bf2 100644 --- a/public/static/style.css +++ b/public/static/style.css @@ -27,10 +27,6 @@ a:hover, a:focus { text-decoration: underline; } -.monospace { - font-family: monospace; -} - .gray { color: gray; } @@ -252,6 +248,7 @@ details > summary { details > p { margin: 0.75rem 1rem; + font-family: monospace; } /* -- Detail columns -- */ diff --git a/sql/init_tables.sql b/sql/init_tables.sql index ca50ffa..0383dd8 100644 --- a/sql/init_tables.sql +++ b/sql/init_tables.sql @@ -35,12 +35,11 @@ CREATE TABLE `mail_users` CREATE TABLE `mail_aliases` ( - `alias_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `user_id` int(10) unsigned NOT NULL, - `mail_address` varchar(255) NOT NULL, - `wildcard_priority` smallint(6) NOT NULL DEFAULT 0, - `created_at` datetime NOT NULL DEFAULT NOW(), - `modified_at` datetime NOT NULL DEFAULT NOW(), + `alias_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `mail_address` varchar(255) NOT NULL, + `created_at` datetime NOT NULL DEFAULT NOW(), + `modified_at` datetime NOT NULL DEFAULT NOW(), PRIMARY KEY (`alias_id`), UNIQUE KEY `mail_address` (`mail_address`), KEY `user_id` (`user_id`), diff --git a/src/Common/FormData.php b/src/Common/FormData.php index 8041257..fde1281 100644 --- a/src/Common/FormData.php +++ b/src/Common/FormData.php @@ -20,7 +20,7 @@ abstract class FormData throw new InputValidationError("$fieldName is required."); } elseif (strlen($raw) < $minLength) { 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)."); } return $raw; @@ -44,42 +44,25 @@ abstract class FormData // 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 && $address === '') { + if (!$required && $username === '') { return null; } - $address = strtolower( - self::validateString($address, 3, 100, $fieldName) + $username = strtolower( + 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)."); } - 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; } - protected static function validateAliasAddress(string $aliasAddress, bool $isWildcard): string + protected static function validateAliasAddress(string $aliasAddress): string { - $aliasAddress = self::validateMailAddress($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; + return self::validateUsername($aliasAddress, true, 'Alias address'); } protected static function validatePassword(string $password, string $passwordRepeat, bool $required = true): ?string diff --git a/src/Config/AppConfig.php b/src/Config/AppConfig.php deleted file mode 100644 index ce94e39..0000000 --- a/src/Config/AppConfig.php +++ /dev/null @@ -1,105 +0,0 @@ -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, - ]; - } -} diff --git a/src/Config/Loaders/AutoConfigLoader.php b/src/Config/Loaders/AutoConfigLoader.php deleted file mode 100644 index 6ba336c..0000000 --- a/src/Config/Loaders/AutoConfigLoader.php +++ /dev/null @@ -1,28 +0,0 @@ -configLoader = new YamlConfigLoader($yamlFilePath); - } else { - $this->configLoader = new EnvConfigLoader(); - } - } - - public function loadConfig(): AppConfig - { - return $this->configLoader->loadConfig(); - } -} diff --git a/src/Config/Loaders/ConfigLoaderInterface.php b/src/Config/Loaders/ConfigLoaderInterface.php deleted file mode 100644 index ed8ebe6..0000000 --- a/src/Config/Loaders/ConfigLoaderInterface.php +++ /dev/null @@ -1,11 +0,0 @@ - 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, - ]); - } -} diff --git a/src/Config/Loaders/YamlConfigLoader.php b/src/Config/Loaders/YamlConfigLoader.php deleted file mode 100644 index 61b5cb1..0000000 --- a/src/Config/Loaders/YamlConfigLoader.php +++ /dev/null @@ -1,48 +0,0 @@ -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, - ]); - } -} diff --git a/src/Dependencies.php b/src/Dependencies.php index 30dd467..94c0d64 100644 --- a/src/Dependencies.php +++ b/src/Dependencies.php @@ -7,7 +7,6 @@ use DI\Container; use MailAccountAdmin\Common\PasswordHelper; use MailAccountAdmin\Common\SessionHelper; use MailAccountAdmin\Common\UserHelper; -use MailAccountAdmin\Config\AppConfig; use MailAccountAdmin\Frontend\Accounts\AccountController; use MailAccountAdmin\Frontend\Accounts\AccountHandler; use MailAccountAdmin\Frontend\Domains\DomainController; @@ -24,43 +23,43 @@ use Twig\Extension\CoreExtension as TwigCoreExtension; class Dependencies { - public const CONFIG = 'config'; + public const SETTINGS = 'settings'; public const TWIG = 'view'; private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates'; public const DATABASE = 'database'; - public static function createContainer(AppConfig $config): Container + public static function createContainer(Settings $settings): Container { $container = new Container(); - // App configuration - $container->set(self::CONFIG, $config); + // App settings + $container->set(self::SETTINGS, $settings); // App information $container->set(AppInfo::class, function (ContainerInterface $c) { - /** @var AppConfig $config */ - $config = $c->get(self::CONFIG); - $versionHelper = new VersionHelper($config); + /** @var Settings $settings */ + $settings = $c->get(self::SETTINGS); + $versionHelper = new VersionHelper(); return new AppInfo( - $config->getAppTitle(), + $settings->getAppTitle(), $versionHelper->getAppVersion(), ); }); // Twig template engine $container->set(self::TWIG, function (ContainerInterface $c) { - /** @var AppConfig $config */ - $config = $c->get(self::CONFIG); + /** @var Settings $settings */ + $settings = $c->get(self::SETTINGS); // 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 /** @var TwigCoreExtension $coreExtension */ $coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class); - $coreExtension->setDateFormat($config->getDateTimeFormat()); - $coreExtension->setTimezone($config->getTimezone()); + $coreExtension->setDateFormat($settings->getDateFormat()); + $coreExtension->setTimezone($settings->getTimezone()); // Add app information to globals $appInfo = $c->get(AppInfo::class); @@ -72,9 +71,9 @@ class Dependencies // Database connection $container->set(self::DATABASE, function (ContainerInterface $c) { - /** @var AppConfig $config */ - $config = $c->get(self::CONFIG); - $dbSettings = $config->getDatabaseSettings(); + /** @var Settings $settings */ + $settings = $c->get(self::SETTINGS); + $dbSettings = $settings->getDatabaseSettings(); return new PDO( "mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}", @@ -109,7 +108,7 @@ class Dependencies }); // Helper classes - $container->set(SessionHelper::class, function () { + $container->set(SessionHelper::class, function (ContainerInterface $c) { return new SessionHelper(); }); @@ -121,7 +120,7 @@ class Dependencies ); }); - $container->set(PasswordHelper::class, function () { + $container->set(PasswordHelper::class, function (ContainerInterface $c) { return new PasswordHelper(); }); diff --git a/src/Frontend/Accounts/AccountAddAliasData.php b/src/Frontend/Accounts/AccountAddAliasData.php index 4a2469e..6e5a3c6 100644 --- a/src/Frontend/Accounts/AccountAddAliasData.php +++ b/src/Frontend/Accounts/AccountAddAliasData.php @@ -4,34 +4,20 @@ declare(strict_types=1); namespace MailAccountAdmin\Frontend\Accounts; use MailAccountAdmin\Common\FormData; -use MailAccountAdmin\Exceptions\InputValidationError; class AccountAddAliasData extends FormData { 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->isWildcard = $isWildcard; - $this->wildcardPriority = $wildcardPriority; } public static function createFromArray(array $raw): self { - $isWildcard = self::validateBoolOption(trim($raw['is_wildcard'] ?? '')); return new self( - self::validateAliasAddress(trim($raw['alias_address'] ?? ''), $isWildcard), - $isWildcard, - self::validateInteger(trim($raw['wildcard_priority'] ?? '')), + self::validateAliasAddress(trim($raw['alias_address'] ?? '')), ); } @@ -39,14 +25,4 @@ class AccountAddAliasData extends FormData { return $this->aliasAddress; } - - public function isWildcard(): bool - { - return $this->isWildcard; - } - - public function getWildcardPriority(): int - { - return $this->wildcardPriority; - } } diff --git a/src/Frontend/Accounts/AccountHandler.php b/src/Frontend/Accounts/AccountHandler.php index b9a0650..4a71fc0 100644 --- a/src/Frontend/Accounts/AccountHandler.php +++ b/src/Frontend/Accounts/AccountHandler.php @@ -278,22 +278,14 @@ class AccountHandler // Check if account exists $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 + $address = $aliasAddData->getAliasAddress(); 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 - $this->aliasRepository->createNewAlias($accountId, $address, $wildcardPriority); + $this->aliasRepository->createNewAlias($accountId, $address); } diff --git a/src/Middlewares.php b/src/Middlewares.php index f60987f..375e481 100644 --- a/src/Middlewares.php +++ b/src/Middlewares.php @@ -4,15 +4,14 @@ declare(strict_types=1); namespace MailAccountAdmin; use MailAccountAdmin\Auth\AuthMiddleware; -use MailAccountAdmin\Config\AppConfig; use Slim\App; use Slim\Views\TwigMiddleware; 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->add(new AuthMiddleware()); diff --git a/src/Models/Alias.php b/src/Models/Alias.php index e636c82..3fe7413 100644 --- a/src/Models/Alias.php +++ b/src/Models/Alias.php @@ -10,23 +10,14 @@ class Alias private int $id; private int $userId; private string $mailAddress; - private int $wildcardPriority; private DateTimeImmutable $createdAt; private DateTimeImmutable $modifiedAt; - private function __construct( - int $id, - int $userId, - string $mailAddress, - int $wildcardPriority, - DateTimeImmutable $createdAt, - DateTimeImmutable $modifiedAt - ) + private function __construct(int $id, int $userId, string $mailAddress, DateTimeImmutable $createdAt, DateTimeImmutable $modifiedAt) { $this->id = $id; $this->userId = $userId; $this->mailAddress = $mailAddress; - $this->wildcardPriority = $wildcardPriority; $this->createdAt = $createdAt; $this->modifiedAt = $modifiedAt; } @@ -37,7 +28,6 @@ class Alias (int)$data['alias_id'], (int)$data['user_id'], $data['mail_address'], - (int)$data['wildcard_priority'], new DateTimeImmutable($data['created_at']), new DateTimeImmutable($data['modified_at']), ); @@ -53,24 +43,11 @@ class Alias 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; } - public function isWildcard(): bool - { - return $this->wildcardPriority !== 0; - } - - public function getWildcardPriority(): int - { - return $this->wildcardPriority; - } - public function getCreatedAt(): DateTimeImmutable { return $this->createdAt; diff --git a/src/Repositories/AliasRepository.php b/src/Repositories/AliasRepository.php index 9a4d615..45ed640 100644 --- a/src/Repositories/AliasRepository.php +++ b/src/Repositories/AliasRepository.php @@ -11,11 +11,11 @@ class AliasRepository extends BaseRepository { 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]); $rows = $statement->fetchAll(PDO::FETCH_ASSOC); - // Create Alias models from rows + // Create Account models from rows $aliasList = []; foreach ($rows as $row) { $aliasList[] = Alias::createFromArray($row); @@ -43,20 +43,12 @@ class AliasRepository extends BaseRepository return $statement->rowCount() === 0; } - public function createNewAlias(int $userId, string $mailAddress, int $wildcardPriority = 0): void + public function createNewAlias(int $userId, string $mailAddress): void { - $query = ' - INSERT INTO mail_aliases - (user_id, mail_address, wildcard_priority) - VALUES - (:user_id, :mail_address, :wildcard_priority) - '; - - $statement = $this->pdo->prepare($query); + $statement = $this->pdo->prepare('INSERT INTO mail_aliases (user_id, mail_address) VALUES (:user_id, :mail_address)'); $statement->execute([ 'user_id' => $userId, 'mail_address' => $mailAddress, - 'wildcard_priority' => $wildcardPriority, ]); } diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000..4d1b989 --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,49 @@ + 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') ?: '', + ]; + } +} diff --git a/src/VersionHelper.php b/src/VersionHelper.php index 1223bce..4fd370a 100644 --- a/src/VersionHelper.php +++ b/src/VersionHelper.php @@ -3,18 +3,16 @@ declare(strict_types=1); namespace MailAccountAdmin; -use MailAccountAdmin\Config\AppConfig; - class VersionHelper { private string $version; - public function __construct(AppConfig $config) + public function __construct() { $version = $this->loadFromVersionFile(); if (!empty($version)) { $this->version = $version; - } elseif ($config->getAppEnvironment() === 'development') { + } elseif ($this->inDevelopmentMode()) { $this->version = '[dev version]'; } else { $this->version = '[undefined version]'; @@ -36,4 +34,9 @@ class VersionHelper } return null; } + + private function inDevelopmentMode(): bool + { + return getenv('APP_ENV') === 'development'; + } } diff --git a/templates/account_create.html.twig b/templates/account_create.html.twig index 15c8bd2..246aae7 100644 --- a/templates/account_create.html.twig +++ b/templates/account_create.html.twig @@ -24,7 +24,7 @@
{{ domainList ? domainList | join(', ') : 'No domains exist yet.' }}
+{{ domainList ? domainList | join(', ') : 'No domains exist yet.' }}
| - | Address | -Wildcard (priority) | +Address | Created at |
|---|---|---|---|---|
| - | - {% if alias.isWildcard() %} - Yes ({{ alias.getWildcardPriority() }}) - {% else %} - No - {% endif %} - | {{ alias.getCreatedAt() | date }} | ||
| - - | -||||
| Wildcard: | -- - - + | |||
- Wildcard aliases use % as a placeholder for any amount of characters (zero or more),
- e.g. %@example.com or example-%@example.com.
-
- Additionally, a wildcard priority must be specified. When a mail address is looked up that - matches multiple (wildcard) aliases, the alias with the lowest wildcard priority will be used. - The priority must not be 0 (internally, the value 0 stands for regular non-wildcard aliases). -
-