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