mail-account-admin/src/Frontend/Accounts/AccountEditData.php

139 lines
3.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace MailAccountAdmin\Frontend\Accounts;
use MailAccountAdmin\Exceptions\InputValidationError;
class AccountEditData
{
/** @var null|string */
private $username;
/** @var bool */
private $usernameCreateAlias;
/** @var bool */
private $usernameReplaceAlias;
/** @var null|string */
private $password;
/** @var bool */
private $active;
/** @var null|string */
private $homeDir;
/** @var null|string */
private $memo;
private function __construct(?string $username, bool $usernameCreateAlias, bool $usernameReplaceAlias, ?string $password, bool $active,
?string $homeDir, ?string $memo)
{
$this->username = $username;
$this->usernameCreateAlias = $usernameCreateAlias;
$this->usernameReplaceAlias = $usernameReplaceAlias;
$this->password = $password;
$this->active = $active;
$this->homeDir = $homeDir;
$this->memo = $memo;
}
public static function createFromArray($raw): self
{
return new self(
self::validateUsername(trim($raw['username'] ?? '')),
self::validateBoolOption(trim($raw['username_create_alias'] ?? '')),
self::validateBoolOption(trim($raw['username_replace_alias'] ?? '')),
self::validatePassword(trim($raw['password'] ?? ''), trim($raw['password_repeat'] ?? '')),
self::validateBoolOption(trim($raw['is_active'] ?? '')),
self::validateHomeDir(trim($raw['home_dir'] ?? '')),
trim($raw['memo'] ?? '')
);
}
// Input validation
private static function validateUsername(string $username): ?string
{
if ($username === '') {
return null;
}
if (strlen($username) > 100) {
throw new InputValidationError('Username is too long.');
}
$username = strtolower($username);
if (!preg_match('/^[a-z0-9._+-]+@[a-z0-9.-]+$/', $username) || preg_match('/^\\.|\\.\\.|\\.@|@\\.|\\.$/', $username)) {
throw new InputValidationError('Username is not valid (must be a valid mail address).');
}
return $username;
}
private static function validatePassword(string $password, string $passwordRepeat): ?string
{
if ($password === '') {
return null;
}
if ($password !== $passwordRepeat) {
throw new InputValidationError('Passwords do not match.');
}
return $password;
}
private static function validateHomeDir(string $homeDir): ?string
{
if ($homeDir === '') {
return null;
}
if (strlen($homeDir) > 100) {
throw new InputValidationError('Home directory is too long.');
}
$homeDir = trim($homeDir, '/');
if (!preg_match('!^[a-z0-9._+-]+(/[a-z0-9._+-]+)*$!i', $homeDir)) {
throw new InputValidationError('Home directory is not a valid path.');
}
return $homeDir;
}
private static function validateBoolOption(string $raw): bool
{
return $raw !== '';
}
// Getters
public function getUsername(): ?string
{
return $this->username;
}
public function getUsernameCreateAlias(): bool
{
return $this->usernameCreateAlias;
}
public function getUsernameReplaceAlias(): bool
{
return $this->usernameReplaceAlias;
}
public function getPassword(): ?string
{
return $this->password;
}
public function getActive(): bool
{
return $this->active;
}
public function getHomeDir(): ?string
{
return $this->homeDir;
}
public function getMemo(): string
{
return $this->memo;
}
}