1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Slides\Connector\Auth\Helpers\ConsoleHelper; |
7
|
|
|
use Slides\Connector\Auth\AuthService; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ManageUsers |
11
|
|
|
* |
12
|
|
|
* @package Slides\Connector\Auth\Commands |
13
|
|
|
*/ |
14
|
|
|
class ManageUsers extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'manage:users {action} {ids}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Manage user(s): delete, restore'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The actions. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $actions = [ |
36
|
|
|
'delete', |
37
|
|
|
'restore' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* User IDs. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $ids; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var AuthService |
49
|
|
|
*/ |
50
|
|
|
protected $authService; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* SyncUsers constructor. |
54
|
|
|
* |
55
|
|
|
* @param AuthService $authService |
56
|
|
|
*/ |
57
|
|
|
public function __construct(AuthService $authService) |
58
|
|
|
{ |
59
|
|
|
$this->authService = $authService; |
60
|
|
|
|
61
|
|
|
parent::__construct(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Execute the console command. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function handle() |
70
|
|
|
{ |
71
|
|
|
$this->ids = ConsoleHelper::stringToArray($ids = $this->argument('ids')); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->line('Passed user ids: <info>' . $ids . '</info>'); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->runActionHandler($this->argument('action')); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Run an action handler. |
80
|
|
|
* |
81
|
|
|
* @param string $action |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
protected function runActionHandler(string $action) |
86
|
|
|
{ |
87
|
|
|
if(!in_array($action, $this->actions)) { |
88
|
|
|
throw new \InvalidArgumentException("Unknown action `{$action}` passed"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$actionHandler = 'handle' . studly_case($action); |
92
|
|
|
|
93
|
|
|
if(!method_exists($this, $actionHandler)) { |
94
|
|
|
throw new \InvalidArgumentException("Cannot find action handler `{$actionHandler}`"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->line("Handling the <info>{$action}</info> action"); |
98
|
|
|
|
99
|
|
|
return $this->{$actionHandler}(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Delete users. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function handleDelete() |
108
|
|
|
{ |
109
|
|
|
$users = $this->retrieveUsers(['id' => $this->ids]); |
110
|
|
|
|
111
|
|
|
if ($users->isEmpty()) { |
112
|
|
|
$this->info('No users found.'); |
113
|
|
|
return; |
114
|
|
|
} else { |
115
|
|
|
if (!$this->confirm('Do you want to delete ' . $users->count() . ' user(s)?')) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($users as $user) { |
121
|
|
|
try { |
122
|
|
|
$this->authService->handle('delete', ['user' => $user]); |
123
|
|
|
|
124
|
|
|
$this->info('User #' . $user->id . ' (' . $user->email . ') successfully deleted.'); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
$this->error('User #' . $user->id . ' (' . $user->email . ') cannot be deleted. ' . $e->getMessage()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Restore users. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function handleRestore() |
137
|
|
|
{ |
138
|
|
|
$users = $this->retrieveUsers(['id' => $this->ids], true); |
139
|
|
|
|
140
|
|
|
if($users->isEmpty()) { |
141
|
|
|
$this->info('No users found.'); |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
else { |
145
|
|
|
if(!$this->confirm('Do you want to restore ' . $users->count() . ' user(s)?')) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ($users as $user) { |
151
|
|
|
try { |
152
|
|
|
$this->authService->handle('restore', ['user' => $user]); |
153
|
|
|
|
154
|
|
|
$this->info('User #' . $user->id . ' (' . $user->email . ') successfully restored.'); |
155
|
|
|
} |
156
|
|
|
catch(\Exception $e) { |
157
|
|
|
$this->error('User #' . $user->id . ' (' . $user->email . ') cannot be restored. ' . $e->getMessage()); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retrieve users. |
164
|
|
|
* |
165
|
|
|
* @param array $conditions |
166
|
|
|
* @param bool $onlyTrashed |
167
|
|
|
* |
168
|
|
|
* @return \Illuminate\Support\Collection |
169
|
|
|
*/ |
170
|
|
|
protected function retrieveUsers(array $conditions, bool $onlyTrashed = false) |
171
|
|
|
{ |
172
|
|
|
$query = \Illuminate\Support\Facades\Auth::getProvider()->createModel() |
173
|
|
|
->newQuery(); |
174
|
|
|
|
175
|
|
|
foreach ($conditions as $column => $value) { |
176
|
|
|
$query->where($column, $value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if($onlyTrashed) { |
180
|
|
|
$query->whereNotNull('deleted_at'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $query->get(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|