1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Command; |
4
|
|
|
|
5
|
|
|
use Cartalyst\Sentinel\Sentinel; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\Question; |
12
|
|
|
|
13
|
|
|
class CreateUserCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Sentinel |
17
|
|
|
*/ |
18
|
|
|
private $sentinel; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
* |
23
|
|
|
* @param Sentinel $sentinel |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Sentinel $sentinel) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->sentinel = $sentinel; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setName('user:create') |
39
|
|
|
->setDescription('Create new user') |
40
|
|
|
->setDefinition([ |
41
|
|
|
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
42
|
|
|
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
43
|
|
|
new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
44
|
|
|
new InputOption('admin', null, InputOption::VALUE_NONE, 'Set the user as admin') |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
$username = $input->getArgument('username'); |
54
|
|
|
$email = $input->getArgument('email'); |
55
|
|
|
$password = $input->getArgument('password'); |
56
|
|
|
$admin = $input->getOption('admin'); |
57
|
|
|
|
58
|
|
|
if ($admin) { |
59
|
|
|
$role = $this->sentinel->findRoleByName('Admin'); |
60
|
|
|
} else { |
61
|
|
|
$role = $this->sentinel->findRoleByName('User'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$user = $this->sentinel->registerAndActivate([ |
65
|
|
|
'username' => $username, |
66
|
|
|
'email' => $email, |
67
|
|
|
'password' => $password, |
68
|
|
|
'permissions' => [ |
69
|
|
|
'user.delete' => 0 |
70
|
|
|
] |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$role->users()->attach($user); |
74
|
|
|
|
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
82
|
|
|
{ |
83
|
|
|
$questions = []; |
84
|
|
|
|
85
|
|
View Code Duplication |
if (!$input->getArgument('username')) { |
|
|
|
|
86
|
|
|
$question = new Question('Please choose a username:'); |
87
|
|
|
$question->setValidator(function ($username) { |
88
|
|
|
if (empty($username)) { |
89
|
|
|
throw new \Exception('Username can not be empty'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $username; |
93
|
|
|
}); |
94
|
|
|
$questions['username'] = $question; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
if (!$input->getArgument('email')) { |
|
|
|
|
98
|
|
|
$question = new Question('Please choose an email:'); |
99
|
|
|
$question->setValidator(function ($email) { |
100
|
|
|
if (empty($email)) { |
101
|
|
|
throw new \Exception('Email can not be empty'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $email; |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
$questions['email'] = $question; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
if (!$input->getArgument('password')) { |
|
|
|
|
111
|
|
|
$question = new Question('Please choose a password:'); |
112
|
|
|
$question->setValidator(function ($password) { |
113
|
|
|
if (empty($password)) { |
114
|
|
|
throw new \Exception('Password can not be empty'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $password; |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
$question->setHidden(true); |
121
|
|
|
$questions['password'] = $question; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($questions as $name => $question) { |
125
|
|
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
126
|
|
|
$input->setArgument($name, $answer); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.