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
|
|
|
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
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public function getUserClass() |
44
|
|
|
{ |
45
|
|
|
if (is_null($userClass = config('auth.providers.'.config('sleeping_owl.auth_provider', 'users').'.model'))) { |
46
|
|
|
throw new \Exception('User class not specified in config/auth.php providers.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $userClass; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getUsers() |
53
|
|
|
{ |
54
|
|
|
$userClass = $this->getUserClass(); |
55
|
|
|
|
56
|
|
|
$headers = ['id', 'name', 'email']; |
57
|
|
|
$users = $userClass::get($headers); |
58
|
|
|
|
59
|
|
|
$this->table($headers, $users); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function createNewUser() |
63
|
|
|
{ |
64
|
|
|
$userClass = $this->getUserClass(); |
65
|
|
|
|
66
|
|
|
if (is_null($email = $this->ask('Email'))) { |
67
|
|
|
$this->error('You should specify email.'); |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (! is_null($userClass::where('email', $email)->first())) { |
73
|
|
|
$this->error("User with same email [{$email}] exists."); |
74
|
|
|
|
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (is_null($password = $this->secret('Password'))) { |
79
|
|
|
$this->error('You should specify password.'); |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$passwordConfirm = $this->secret('Password Confirm'); |
85
|
|
|
|
86
|
|
|
if ($password !== $passwordConfirm) { |
87
|
|
|
$this->error('Password confirm failed.'); |
88
|
|
|
|
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$name = $this->ask('User Name'); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$user = $userClass::create([ |
96
|
|
|
'email' => $email, |
97
|
|
|
'password' => bcrypt($password), |
98
|
|
|
'name' => $name, |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->info("User [{$user->id}] created."); |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
$this->error('Something went wrong. User not created'); |
104
|
|
|
|
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function deleteUser() |
110
|
|
|
{ |
111
|
|
|
$userClass = $this->getUserClass(); |
112
|
|
|
|
113
|
|
|
$this->getUsers(); |
114
|
|
|
$id = $this->ask('Select user id to delete'); |
115
|
|
|
|
116
|
|
|
if (is_null($user = $userClass::find($id))) { |
117
|
|
|
$this->error("User with id [{$id}] not found."); |
118
|
|
|
|
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$confirm = $this->confirm("Are you sure want to delete user with id [{$id}]?", false); |
123
|
|
|
if (! $confirm) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$user->delete(); |
128
|
|
|
$this->info("User with id [{$id}] was deleted."); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Change administrator's password. |
133
|
|
|
*/ |
134
|
|
|
protected function changePassword() |
135
|
|
|
{ |
136
|
|
|
$userClass = $this->getUserClass(); |
137
|
|
|
|
138
|
|
|
$this->getUsers(); |
139
|
|
|
$id = $this->ask('Select user id to change their password'); |
140
|
|
|
|
141
|
|
|
if (is_null($user = $userClass::find($id))) { |
142
|
|
|
$this->error("User with id [{$id}] not found."); |
143
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$password = $this->secret('New password'); |
148
|
|
|
if (is_null($password)) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$passwordConfirm = $this->secret('Password Confirm'); |
153
|
|
|
if (is_null($passwordConfirm)) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($password !== $passwordConfirm) { |
158
|
|
|
$this->error('Password confirm failed.'); |
159
|
|
|
|
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$user->password = bcrypt($password); |
164
|
|
|
$user->save(); |
165
|
|
|
|
166
|
|
|
$this->info('Password was changed.'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the console command options. |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
protected function getOptions() |
174
|
|
|
{ |
175
|
|
|
return [ |
176
|
|
|
['create', 'c', InputOption::VALUE_NONE, 'Create new user.'], |
177
|
|
|
['delete', 'd', InputOption::VALUE_NONE, 'Delete user.'], |
178
|
|
|
['password', 'p', InputOption::VALUE_NONE, 'Change user password.'], |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|