1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
|
8
|
|
|
class UserManagerCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The console command name. |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name = 'sleepingowl:user'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $description = 'Manage your users.'; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function fire() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
if ($this->option('create')) { |
25
|
|
|
return $this->createNewUser(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($this->option('delete')) { |
29
|
|
|
return $this->deleteUser(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->option('password')) { |
33
|
|
|
return $this->changePassword(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->getUsers(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function handle() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
if ($this->option('create')) { |
42
|
|
|
return $this->createNewUser(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->option('delete')) { |
46
|
|
|
return $this->deleteUser(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($this->option('password')) { |
50
|
|
|
return $this->changePassword(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->getUsers(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
public function getUserClass() |
61
|
|
|
{ |
62
|
|
|
if (is_null($userClass = config('auth.providers.'.config('sleeping_owl.auth_provider', 'users').'.model'))) { |
63
|
|
|
throw new \Exception('User class not specified in config/auth.php providers.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $userClass; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getUsers() |
70
|
|
|
{ |
71
|
|
|
$userClass = $this->getUserClass(); |
72
|
|
|
|
73
|
|
|
$headers = ['id', 'name', 'email']; |
74
|
|
|
$users = $userClass::get($headers); |
75
|
|
|
|
76
|
|
|
$this->table($headers, $users); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function createNewUser() |
80
|
|
|
{ |
81
|
|
|
$userClass = $this->getUserClass(); |
82
|
|
|
|
83
|
|
|
if (is_null($email = $this->ask('Email'))) { |
84
|
|
|
$this->error('You should specify email.'); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (! is_null($userClass::where('email', $email)->first())) { |
90
|
|
|
$this->error("User with same email [{$email}] exists."); |
91
|
|
|
|
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (is_null($password = $this->secret('Password'))) { |
96
|
|
|
$this->error('You should specify password.'); |
97
|
|
|
|
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$passwordConfirm = $this->secret('Password Confirm'); |
102
|
|
|
|
103
|
|
|
if ($password !== $passwordConfirm) { |
104
|
|
|
$this->error('Password confirm failed.'); |
105
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$name = $this->ask('User Name'); |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
$user = $userClass::create([ |
113
|
|
|
'email' => $email, |
114
|
|
|
'password' => bcrypt($password), |
115
|
|
|
'name' => $name, |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->info("User [{$user->id}] created."); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
\Log::error('unable to create new user!', [ |
121
|
|
|
'exception' => $e, |
122
|
|
|
]); |
123
|
|
|
$this->error('Something went wrong. User not created'); |
124
|
|
|
|
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function deleteUser() |
130
|
|
|
{ |
131
|
|
|
$userClass = $this->getUserClass(); |
132
|
|
|
|
133
|
|
|
$this->getUsers(); |
134
|
|
|
$id = $this->ask('Select user id to delete'); |
135
|
|
|
|
136
|
|
|
if (is_null($user = $userClass::find($id))) { |
137
|
|
|
$this->error("User with id [{$id}] not found."); |
138
|
|
|
|
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$confirm = $this->confirm("Are you sure want to delete user with id [{$id}]?", false); |
143
|
|
|
if (! $confirm) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$user->delete(); |
148
|
|
|
$this->info("User with id [{$id}] was deleted."); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Change administrator's password. |
153
|
|
|
*/ |
154
|
|
|
protected function changePassword() |
155
|
|
|
{ |
156
|
|
|
$userClass = $this->getUserClass(); |
157
|
|
|
|
158
|
|
|
$this->getUsers(); |
159
|
|
|
$id = $this->ask('Select user id to change their password'); |
160
|
|
|
|
161
|
|
|
if (is_null($user = $userClass::find($id))) { |
162
|
|
|
$this->error("User with id [{$id}] not found."); |
163
|
|
|
|
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$password = $this->secret('New password'); |
168
|
|
|
if (is_null($password)) { |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$passwordConfirm = $this->secret('Password Confirm'); |
173
|
|
|
if (is_null($passwordConfirm)) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($password !== $passwordConfirm) { |
178
|
|
|
$this->error('Password confirm failed.'); |
179
|
|
|
|
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$user->password = bcrypt($password); |
184
|
|
|
$user->save(); |
185
|
|
|
|
186
|
|
|
$this->info('Password was changed.'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the console command options. |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function getOptions() |
194
|
|
|
{ |
195
|
|
|
return [ |
196
|
|
|
['create', 'c', InputOption::VALUE_NONE, 'Create new user.'], |
197
|
|
|
['delete', 'd', InputOption::VALUE_NONE, 'Delete user.'], |
198
|
|
|
['password', 'p', InputOption::VALUE_NONE, 'Change user password.'], |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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.