1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\User\Console; |
6
|
|
|
|
7
|
|
|
use App\User\User; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Yiisoft\Rbac\Manager; |
14
|
|
|
use Yiisoft\Rbac\StorageInterface; |
15
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
16
|
|
|
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy; |
17
|
|
|
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter; |
18
|
|
|
|
19
|
|
|
class CreateCommand extends Command |
20
|
|
|
{ |
21
|
|
|
private CycleDependencyProxy $promise; |
22
|
|
|
private Manager $manager; |
23
|
|
|
private StorageInterface $storage; |
24
|
|
|
|
25
|
|
|
protected static $defaultName = 'user/create'; |
26
|
|
|
|
27
|
|
|
public function __construct(CycleDependencyProxy $promise, Manager $manager, StorageInterface $storage) |
28
|
|
|
{ |
29
|
|
|
$this->promise = $promise; |
30
|
|
|
$this->manager = $manager; |
31
|
|
|
$this->storage = $storage; |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function configure(): void |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setDescription('Creates a user') |
39
|
|
|
->setHelp('This command allows you to create a user') |
40
|
|
|
->addArgument('login', InputArgument::REQUIRED, 'Login') |
41
|
|
|
->addArgument('password', InputArgument::REQUIRED, 'Password') |
42
|
|
|
->addArgument('isAdmin', InputArgument::OPTIONAL, 'Create user as admin'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
46
|
|
|
{ |
47
|
|
|
$io = new SymfonyStyle($input, $output); |
48
|
|
|
|
49
|
|
|
$login = $input->getArgument('login'); |
50
|
|
|
$password = $input->getArgument('password'); |
51
|
|
|
$isAdmin = (bool) $input->getArgument('isAdmin'); |
52
|
|
|
|
53
|
|
|
$user = new User($login, $password); |
|
|
|
|
54
|
|
|
try { |
55
|
|
|
(new EntityWriter($this->promise->getORM()))->write([$user]); |
56
|
|
|
|
57
|
|
|
if ($isAdmin) { |
58
|
|
|
$this->manager->assign($this->storage->getRoleByName('admin'), $user->getId()); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$io->success('User created'); |
62
|
|
|
} catch (\Throwable $t) { |
63
|
|
|
$io->error($t->getMessage()); |
64
|
|
|
return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR; |
65
|
|
|
} |
66
|
|
|
return ExitCode::OK; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|