|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of the mailserver-admin package. |
|
6
|
|
|
* (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin> |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace App\Command; |
|
12
|
|
|
|
|
13
|
|
|
use App\Entity\Alias; |
|
14
|
|
|
use App\Entity\Domain; |
|
15
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
21
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
22
|
|
|
|
|
23
|
|
|
class AliasAddCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
private ManagerRegistry $manager; |
|
26
|
|
|
|
|
27
|
|
|
private ValidatorInterface $validator; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ManagerRegistry $manager, ValidatorInterface $validator) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct(); |
|
32
|
|
|
|
|
33
|
|
|
$this->manager = $manager; |
|
34
|
|
|
$this->validator = $validator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function configure(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this |
|
40
|
|
|
->setName('alias:add') |
|
41
|
|
|
->setDescription('Add aliases.') |
|
42
|
|
|
->addArgument('from', InputArgument::REQUIRED, 'Address of the new alias.') |
|
43
|
|
|
->addArgument('to', InputArgument::REQUIRED, 'Where mails to the new alias go to.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
47
|
|
|
{ |
|
48
|
|
|
$from = \filter_var($input->getArgument('from'), \FILTER_VALIDATE_EMAIL); |
|
49
|
|
|
$to = \filter_var($input->getArgument('to'), \FILTER_VALIDATE_EMAIL); |
|
50
|
|
|
|
|
51
|
|
|
if (!$from) { |
|
52
|
|
|
$output->writeln(sprintf('<error>%s is not a valid email address.</error>', $input->getArgument('from'))); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return 1; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!$to) { |
|
58
|
|
|
$output->writeln(sprintf('<error>%s is not a valid email address.</error>', $input->getArgument('to'))); |
|
59
|
|
|
|
|
60
|
|
|
return 1; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$alias = new Alias(); |
|
64
|
|
|
$alias->setDestination($to); |
|
65
|
|
|
|
|
66
|
|
|
$fromParts = \explode('@', $from, 2); |
|
67
|
|
|
$domain = $this->getDomain($fromParts[1]); |
|
68
|
|
|
|
|
69
|
|
|
if (null === $domain) { |
|
70
|
|
|
$output->writeln(sprintf('<error>Domain %s has to be created before.</error>', $fromParts[1])); |
|
71
|
|
|
|
|
72
|
|
|
return 1; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$alias->setDomain($domain); |
|
76
|
|
|
$alias->setName(\mb_strtolower($fromParts[0])); |
|
77
|
|
|
|
|
78
|
|
|
$validationResult = $this->validator->validate($alias); |
|
79
|
|
|
|
|
80
|
|
|
if ($validationResult->count() > 0) { |
|
81
|
|
|
foreach ($validationResult as $item) { |
|
82
|
|
|
/* @var $item ConstraintViolation */ |
|
83
|
|
|
$output->writeln(sprintf('<error>%s: %s</error>', $item->getPropertyPath(), $item->getMessage())); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return 1; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->manager->getManager()->persist($alias); |
|
90
|
|
|
$this->manager->getManager()->flush(); |
|
91
|
|
|
|
|
92
|
|
|
return 0; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function getDomain(string $domain): ?Domain |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->manager->getRepository(Domain::class)->findOneBy(['name' => \mb_strtolower($domain)]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|