1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Delete users page controller class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @subpackage Controllers |
7
|
|
|
* @since 1.1 |
8
|
|
|
*/ |
9
|
|
|
namespace EBloodBank\Controllers; |
10
|
|
|
|
11
|
|
|
use EBloodBank as EBB; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Delete users page controller class |
15
|
|
|
* |
16
|
|
|
* @since 1.1 |
17
|
|
|
*/ |
18
|
|
|
class DeleteUsers extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \EBloodBank\Models\User[] |
22
|
|
|
* @since 1.1 |
23
|
|
|
*/ |
24
|
|
|
protected $users = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
* @since 1.1 |
29
|
|
|
*/ |
30
|
|
|
public function __invoke() |
31
|
|
|
{ |
32
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'delete')) { |
33
|
|
|
$this->viewFactory->displayView('error-403'); |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (filter_has_var(INPUT_POST, 'users')) { |
38
|
|
|
$usersIDs = filter_input(INPUT_POST, 'users', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
39
|
|
|
if (! empty($usersIDs) && is_array($usersIDs)) { |
40
|
|
|
$this->users = $this->getUserRepository()->findBy(['id' => $usersIDs]); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->doActions(); |
45
|
|
|
$this->viewFactory->displayView( |
46
|
|
|
'delete-users', |
47
|
|
|
[ |
48
|
|
|
'users' => $this->users, |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return void |
55
|
|
|
* @since 1.1 |
56
|
|
|
*/ |
57
|
|
|
protected function doActions() |
58
|
|
|
{ |
59
|
|
|
switch (filter_input(INPUT_POST, 'action')) { |
60
|
|
|
case 'delete_users': |
61
|
|
|
$this->doDeleteAction(); |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return void |
68
|
|
|
* @since 1.1 |
69
|
|
|
*/ |
70
|
|
|
protected function doDeleteAction() |
71
|
|
|
{ |
72
|
|
|
if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'delete')) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$sessionToken = $this->getSession()->getCsrfToken(); |
77
|
|
|
$actionToken = filter_input(INPUT_POST, 'token'); |
78
|
|
|
|
79
|
|
|
if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$users = $this->users; |
84
|
|
|
|
85
|
|
|
if (! $users || ! is_array($users)) { |
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$deletedUsersCount = 0; |
90
|
|
|
|
91
|
|
|
foreach ($users as $user) { |
92
|
|
|
if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $user)) { |
93
|
|
|
$this->getEntityManager()->remove($user); |
94
|
|
|
$deletedUsersCount++; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->getEntityManager()->flush(); |
99
|
|
|
|
100
|
|
|
EBB\redirect( |
|
|
|
|
101
|
|
|
EBB\addQueryArgs( |
|
|
|
|
102
|
|
|
EBB\getEditUsersURL(), |
|
|
|
|
103
|
|
|
['flag-deleted' => $deletedUsersCount] |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.