|
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\Domain; |
|
14
|
|
|
use App\Entity\User; |
|
15
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Question\Question; |
|
23
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
24
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
25
|
|
|
|
|
26
|
|
|
class UserAddCommand extends Command |
|
27
|
|
|
{ |
|
28
|
|
|
private ManagerRegistry $manager; |
|
29
|
|
|
private ValidatorInterface $validator; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(ManagerRegistry $manager, ValidatorInterface $validator) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->manager = $manager; |
|
36
|
|
|
$this->validator = $validator; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function configure(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this |
|
42
|
|
|
->setName('user:add') |
|
43
|
|
|
->setDescription('Add users.') |
|
44
|
|
|
->addArgument('name', InputArgument::REQUIRED, 'Local-part (before @)') |
|
45
|
|
|
->addArgument('domain', InputArgument::REQUIRED, 'Domain-part (after @), has to be created already') |
|
46
|
|
|
->addOption('admin', null, InputOption::VALUE_NONE, 'Allow login to management interface') |
|
47
|
|
|
->addOption('sendonly', null, InputOption::VALUE_NONE, 'Send only accounts cannot receive mails') |
|
48
|
|
|
->addOption('quota', null, InputOption::VALUE_REQUIRED, 'Limit the disk usage of this account') |
|
49
|
|
|
->addOption('password', null, InputOption::VALUE_REQUIRED, 'Sets the account password directly') |
|
50
|
|
|
->addOption('enable', null, InputOption::VALUE_NONE, 'Enable the new created account'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
54
|
|
|
{ |
|
55
|
|
|
$user = new User(); |
|
56
|
|
|
$domain = $this->getDomain($input->getArgument('domain')); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
if (null === $domain) { |
|
59
|
|
|
$output->writeln(sprintf('<error>Domain %s was not found.</error>', $input->getArgument('domain'))); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return 1; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$user->setDomain($domain); |
|
65
|
|
|
$user->setName(\mb_strtolower($input->getArgument('name'))); |
|
|
|
|
|
|
66
|
|
|
$user->setAdmin((bool) $input->getOption('admin')); |
|
67
|
|
|
$user->setSendOnly((bool) $input->getOption('sendonly')); |
|
68
|
|
|
$user->setEnabled((bool) $input->getOption('enable')); |
|
69
|
|
|
|
|
70
|
|
|
if ($input->hasOption('quota')) { |
|
71
|
|
|
$user->setQuota((int) $input->getOption('quota')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!$input->getOption('password')) { |
|
75
|
|
|
/** @var QuestionHelper $helper */ |
|
76
|
|
|
$helper = $this->getHelper('question'); |
|
77
|
|
|
$question = new Question('Password? (hidden)'); |
|
78
|
|
|
$question->setHidden(true); |
|
79
|
|
|
|
|
80
|
|
|
$password = (string) $helper->ask($input, $output, $question); |
|
81
|
|
|
|
|
82
|
|
|
if ('' === $password) { |
|
83
|
|
|
$output->writeln('<error>Please set a valid password.</error>'); |
|
84
|
|
|
|
|
85
|
|
|
return 1; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$user->setPlainPassword($password); |
|
89
|
|
|
} else { |
|
90
|
|
|
$user->setPlainPassword($input->getOption('password')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$validationResult = $this->validator->validate($user); |
|
94
|
|
|
|
|
95
|
|
|
if ($validationResult->count() > 0) { |
|
96
|
|
|
foreach ($validationResult as $item) { |
|
97
|
|
|
/* @var $item ConstraintViolation */ |
|
98
|
|
|
$output->writeln(sprintf('<error>%s: %s</error>', $item->getPropertyPath(), $item->getMessage())); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return 1; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->manager->getManager()->persist($user); |
|
105
|
|
|
$this->manager->getManager()->flush(); |
|
106
|
|
|
|
|
107
|
|
|
return 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function getDomain(string $domain): ?Domain |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->manager->getRepository(Domain::class)->findOneBy(['name' => \mb_strtolower($domain)]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|