1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProyectoTAU\TAU\Common; |
4
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use PDO; |
7
|
|
|
use PDOException; |
8
|
|
|
use ProyectoTAU\TAU\Module\Administration\User\Domain\User; |
9
|
|
|
use ProyectoTAU\TAU\Module\Administration\Group\Domain\Group; |
10
|
|
|
use ProyectoTAU\TAU\Module\Administration\Role\Domain\Role; |
11
|
|
|
use ProyectoTAU\TAU\Module\Administration\Module\Domain\Module; |
12
|
|
|
use ProyectoTAU\TAU\Module\Administration\User\Domain\UserId; |
|
|
|
|
13
|
|
|
use ProyectoTAU\TAU\Module\Administration\Group\Domain\GroupId; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/* |
16
|
|
|
* @see https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
// General singleton class. |
20
|
|
|
class SQLiteRepository |
21
|
|
|
{ |
22
|
|
|
// Hold the class instance. |
23
|
|
|
private static $instance = null; |
24
|
|
|
private static $db = null; |
25
|
|
|
|
26
|
|
|
// Tables |
27
|
|
|
private $userDataStore = []; |
28
|
|
|
private $groupDataStore = []; |
29
|
|
|
private $roleDataStore = []; |
30
|
|
|
private $moduleDataStore = []; |
31
|
|
|
|
32
|
|
|
// Relations |
33
|
|
|
private $user_group = []; |
|
|
|
|
34
|
|
|
private $group_role = []; |
|
|
|
|
35
|
|
|
private $role_module = []; |
|
|
|
|
36
|
|
|
|
37
|
|
|
// The constructor is private |
38
|
|
|
// to prevent initiation with outer code. |
39
|
1 |
|
private function __construct() |
40
|
|
|
{ |
41
|
|
|
// The expensive process (e.g.,db connection) goes here. |
42
|
1 |
|
$dbname = __DIR__ . '/admin.sqlite3'; |
43
|
|
|
try { |
44
|
1 |
|
self::$db = new PDO('sqlite:' . $dbname); |
45
|
1 |
|
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
46
|
|
|
//self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
47
|
|
|
} catch (PDOException $e) { |
48
|
|
|
die($e->getMessage().' ('.$dbname.')'); |
|
|
|
|
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
// The object is created from within the class itself |
53
|
|
|
// only if the class has no instance. |
54
|
40 |
|
public static function getInstance() |
55
|
|
|
{ |
56
|
40 |
|
if (self::$instance == null) |
57
|
|
|
{ |
58
|
1 |
|
self::$instance = new static(); |
59
|
|
|
} |
60
|
|
|
|
61
|
40 |
|
return self::$instance; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* @see https://www.tutorialspoint.com/sqlite/sqlite_truncate_table.htm |
66
|
|
|
*/ |
67
|
16 |
|
public function clearUser() |
68
|
|
|
{ |
69
|
16 |
|
$ps = self::$db->prepare('DELETE FROM user_group;'); |
70
|
16 |
|
$this->executeOrFail($ps); |
71
|
|
|
|
72
|
16 |
|
$ps = self::$db->prepare('DELETE FROM User;'); |
73
|
16 |
|
$this->executeOrFail($ps); |
74
|
16 |
|
} |
75
|
|
|
|
76
|
22 |
|
public function clearGroup() |
77
|
|
|
{ |
78
|
22 |
|
$ps = self::$db->query('DELETE FROM user_group;'); |
79
|
22 |
|
$this->executeOrFail($ps); |
80
|
|
|
|
81
|
22 |
|
$ps = self::$db->prepare('DELETE FROM "Group";'); |
82
|
22 |
|
$this->executeOrFail($ps); |
83
|
22 |
|
} |
84
|
|
|
|
85
|
16 |
|
public function clearRole() |
86
|
|
|
{ |
87
|
16 |
|
$ps = self::$db->query('DELETE FROM group_role;'); |
88
|
16 |
|
$this->executeOrFail($ps); |
89
|
|
|
|
90
|
16 |
|
$ps = self::$db->prepare('DELETE FROM "Group";'); |
91
|
16 |
|
$this->executeOrFail($ps); |
92
|
|
|
|
93
|
16 |
|
$ps = self::$db->prepare('DELETE FROM Role;'); |
94
|
16 |
|
$this->executeOrFail($ps); |
95
|
16 |
|
} |
96
|
|
|
|
97
|
10 |
|
public function clearModule() |
98
|
|
|
{ |
99
|
10 |
|
$ps = self::$db->query('DELETE FROM role_module;'); |
100
|
10 |
|
$this->executeOrFail($ps); |
101
|
|
|
|
102
|
10 |
|
$ps = self::$db->prepare('DELETE FROM Role;'); |
103
|
10 |
|
$this->executeOrFail($ps); |
104
|
|
|
|
105
|
10 |
|
$ps = self::$db->prepare('DELETE FROM Module;'); |
106
|
10 |
|
$this->executeOrFail($ps); |
107
|
10 |
|
} |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* User |
111
|
|
|
*/ |
112
|
|
|
|
113
|
16 |
|
public function createUser(User $user): void |
114
|
|
|
{ |
115
|
16 |
|
$ps = self::$db->prepare('INSERT INTO User (user_id, name, surname, login)'. |
116
|
16 |
|
' VALUES(:user_id, :name, :surname, :login);'); |
117
|
16 |
|
$this->executeOrFail($ps, [ |
118
|
16 |
|
':user_id' => $user->getId(), |
119
|
16 |
|
':name' => $user->getName(), |
120
|
16 |
|
':surname' => $user->getSurname(), |
121
|
16 |
|
':login' => $user->getLogin() |
122
|
|
|
]); |
123
|
|
|
|
124
|
16 |
|
$this->userDataStore[$user->getId()] = self::$db->lastInsertId(); |
125
|
16 |
|
} |
126
|
|
|
|
127
|
11 |
|
public function readUser($id): User |
128
|
|
|
{ |
129
|
11 |
|
$ps = self::$db->prepare('SELECT user_pk, user_id, name, surname, login FROM User WHERE user_id = :id;'); |
130
|
|
|
|
131
|
11 |
|
$this->executeOrFail($ps, [':id' => $id]); |
132
|
|
|
|
133
|
11 |
|
$resultSet = $ps->fetch(PDO::FETCH_ASSOC); |
134
|
11 |
|
if( $resultSet === false ) |
135
|
1 |
|
throw new InvalidArgumentException("User with id = {$id} not found"); |
136
|
|
|
|
137
|
10 |
|
$ref = new \ReflectionClass(User::class); |
138
|
10 |
|
$user = $this->castUser($ref->newInstanceWithoutConstructor()); |
139
|
|
|
|
140
|
10 |
|
$user->setId($resultSet['user_id']); |
141
|
10 |
|
$user->setName($resultSet['name']); |
142
|
10 |
|
$user->setSurname($resultSet['surname']); |
143
|
10 |
|
$user->setLogin($resultSet['login']); |
144
|
|
|
|
145
|
10 |
|
$this->userDataStore[$user->getId()] = $resultSet['user_pk']; |
146
|
|
|
|
147
|
10 |
|
return $user; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
public function updateUser($id, $name, $surname, $login): void |
151
|
|
|
{ |
152
|
1 |
|
$ps = self::$db->prepare('UPDATE User SET '. |
153
|
|
|
'name = :name,'. |
154
|
|
|
'surname = :surname,'. |
155
|
|
|
'login = :login'. |
156
|
1 |
|
' WHERE user_id = :id;'); |
157
|
|
|
|
158
|
1 |
|
$this->executeOrFail($ps, [ |
159
|
1 |
|
':id' => $id, |
160
|
1 |
|
':name' => $name, |
161
|
1 |
|
':surname' => $surname, |
162
|
1 |
|
':login' => $login |
163
|
|
|
]); |
164
|
1 |
|
} |
165
|
|
|
|
166
|
1 |
|
public function deleteUser($id): void |
167
|
|
|
{ |
168
|
1 |
|
$ps = self::$db->prepare('DELETE FROM User WHERE user_pk = :user_pk;'); |
169
|
|
|
|
170
|
1 |
|
$user_pk = $this->userDataStore[$id]; |
171
|
|
|
|
172
|
1 |
|
$this->executeOrFail($ps, [':user_pk' => $user_pk]); |
173
|
|
|
|
174
|
1 |
|
unset($this->userDataStore[$id]); |
175
|
1 |
|
} |
176
|
|
|
|
177
|
3 |
|
public function getGroupsFromUser(User $user): array // TODO id instead of User |
178
|
|
|
{ |
179
|
3 |
|
$ps = self::$db->prepare('SELECT group_pk, g.group_id, name, description FROM "Group" g'. |
180
|
|
|
' INNER JOIN user_group rel ON g.group_pk = rel.group_fk'. |
181
|
3 |
|
' WHERE rel.user_fk = :user_fk;'); |
182
|
|
|
|
183
|
3 |
|
$id = $user->getid(); |
184
|
3 |
|
$user_fk = $this->userDataStore[$id]; |
185
|
|
|
|
186
|
3 |
|
$resultSet = $this->queryOrFail($ps, [':user_fk' => $user_fk]); |
187
|
|
|
|
188
|
3 |
|
$groups = []; |
189
|
3 |
|
foreach ($resultSet as $entry) { |
190
|
2 |
|
$ref = new \ReflectionClass(Group::class); |
191
|
2 |
|
$group = $this->castGroup($ref->newInstanceWithoutConstructor()); |
192
|
|
|
|
193
|
2 |
|
$group->setPropertiesBag(['id', 'name', 'desc']); |
194
|
2 |
|
$group->setSettersBag($group->getPropertiesBag()); |
195
|
|
|
|
196
|
2 |
|
$group->setId($entry['group_id']); |
197
|
2 |
|
$group->setName($entry['name']); |
198
|
2 |
|
$group->setDesc($entry['description']); |
199
|
|
|
|
200
|
2 |
|
$groups[$entry['group_id']] = $group; |
201
|
|
|
} |
202
|
|
|
|
203
|
3 |
|
$available = array_diff_assoc($this->getAllGroups(), $groups); |
204
|
|
|
$r = [ |
205
|
3 |
|
'belongsto' => $groups, |
206
|
3 |
|
'available' => $available |
207
|
|
|
]; |
208
|
|
|
|
209
|
3 |
|
return $r; |
210
|
|
|
} |
211
|
|
|
|
212
|
15 |
|
private function castUser($o): User |
213
|
|
|
{ |
214
|
15 |
|
return $o; |
215
|
|
|
} |
216
|
|
|
|
217
|
20 |
|
private function castGroup($o): Group |
218
|
|
|
{ |
219
|
20 |
|
return $o; |
220
|
|
|
} |
221
|
|
|
|
222
|
24 |
|
private function queryOrFail($ps, $params = []): array |
223
|
|
|
{ |
224
|
24 |
|
$this->executeOrFail($ps, $params); |
225
|
24 |
|
$resultSet = $ps->fetchAll(PDO::FETCH_ASSOC); |
226
|
|
|
|
227
|
24 |
|
return $resultSet; |
228
|
|
|
} |
229
|
|
|
|
230
|
40 |
|
private function executeOrFail($ps, $params = [], $a = 0): void |
|
|
|
|
231
|
|
|
{ |
232
|
40 |
|
$a = 1; |
233
|
40 |
|
if($a) { |
234
|
40 |
|
$sql = $ps->queryString; |
235
|
40 |
|
$b = str_replace(array_keys($params), array_values($params), $sql); |
236
|
40 |
|
echo "\n$b\n"; |
237
|
|
|
} |
238
|
|
|
|
239
|
40 |
|
$result = $ps->execute($params); |
240
|
|
|
|
241
|
40 |
|
if( $result === false ) { |
242
|
|
|
$a = 1; |
243
|
|
|
if($a) { |
244
|
|
|
$sql = $ps->queryString; |
245
|
|
|
$b = str_replace(array_keys($params), array_values($params), $sql); |
246
|
|
|
echo "\n$b\n"; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
throw new \Exception($ps->errorInfo(), $ps->errorCode()); |
250
|
|
|
} |
251
|
40 |
|
} |
252
|
|
|
|
253
|
6 |
|
private function getAllGroups(): array |
254
|
|
|
{ |
255
|
6 |
|
$ps = self::$db->prepare('SELECT group_pk, group_id, group_id, name, description FROM "Group";'); |
256
|
|
|
|
257
|
6 |
|
$resultSet = $this->queryOrFail($ps); |
258
|
|
|
|
259
|
6 |
|
$groups = []; |
260
|
6 |
|
foreach ($resultSet as $entry) { |
261
|
6 |
|
$ref = new \ReflectionClass(Group::class); |
262
|
6 |
|
$group = $this->castGroup($ref->newInstanceWithoutConstructor()); |
263
|
|
|
|
264
|
6 |
|
$group->setPropertiesBag(['id', 'name', 'desc']); |
265
|
6 |
|
$group->setSettersBag($group->getPropertiesBag()); |
266
|
|
|
|
267
|
6 |
|
$group->setId($entry['group_id']); |
268
|
6 |
|
$group->setName($entry['name']); |
269
|
6 |
|
$group->setDesc($entry['description']); |
270
|
|
|
|
271
|
6 |
|
$this->groupDataStore[$entry['group_id']] = $entry['group_pk']; |
272
|
|
|
|
273
|
6 |
|
$groups[$entry['group_id']] = $group; |
274
|
|
|
} |
275
|
|
|
|
276
|
6 |
|
return $groups; |
277
|
|
|
} |
278
|
|
|
|
279
|
22 |
|
public function createGroup(Group $group) |
280
|
|
|
{ |
281
|
22 |
|
$ps = self::$db->prepare('INSERT INTO "Group" (group_id, name, description)'. |
282
|
22 |
|
' VALUES(:group_id, :name, :description);'); |
283
|
22 |
|
$this->executeOrFail($ps, [ |
284
|
22 |
|
':group_id' => $group->getId(), |
285
|
22 |
|
':name' => $group->getName(), |
286
|
22 |
|
':description' => $group->getDesc() |
287
|
|
|
]); |
288
|
|
|
|
289
|
22 |
|
$this->groupDataStore[$group->getId()] = self::$db->lastInsertId(); |
290
|
22 |
|
} |
291
|
|
|
|
292
|
19 |
|
public function readGroup($id) |
293
|
|
|
{ |
294
|
19 |
|
$ps = self::$db->prepare('SELECT group_pk, group_id, name, description FROM "Group" WHERE group_id = :id;'); |
295
|
|
|
|
296
|
19 |
|
$this->executeOrFail($ps, [':id' => $id]); |
297
|
|
|
|
298
|
19 |
|
$resultSet = $ps->fetch(PDO::FETCH_ASSOC); |
299
|
19 |
|
if( $resultSet === false ) |
300
|
1 |
|
throw new InvalidArgumentException("Group with id = {$id} not found"); |
301
|
|
|
|
302
|
18 |
|
$ref = new \ReflectionClass(Group::class); |
303
|
18 |
|
$group = $this->castGroup($ref->newInstanceWithoutConstructor()); |
304
|
|
|
|
305
|
18 |
|
$group->setPropertiesBag(['id', 'name', 'desc']); |
306
|
18 |
|
$group->setSettersBag($group->getPropertiesBag()); |
307
|
|
|
|
308
|
18 |
|
$group->setId($resultSet['group_id']); |
309
|
18 |
|
$group->setName($resultSet['name']); |
310
|
18 |
|
$group->setDesc($resultSet['description']); |
311
|
|
|
|
312
|
18 |
|
$this->groupDataStore[$group->getId()] = $resultSet['group_pk']; |
313
|
|
|
|
314
|
18 |
|
return $group; |
315
|
|
|
} |
316
|
|
|
|
317
|
1 |
|
public function updateGroup($id, $name, $desc) |
318
|
|
|
{ |
319
|
1 |
|
$ps = self::$db->prepare('UPDATE "Group" SET '. |
320
|
|
|
'name = :name,'. |
321
|
|
|
'description = :description'. |
322
|
1 |
|
' WHERE group_id = :id;'); |
323
|
|
|
|
324
|
1 |
|
$this->executeOrFail($ps, [ |
325
|
1 |
|
':id' => $id, |
326
|
1 |
|
':name' => $name, |
327
|
1 |
|
':description' => $desc |
328
|
|
|
]); |
329
|
1 |
|
} |
330
|
|
|
|
331
|
1 |
|
public function deleteGroup($id) |
332
|
|
|
{ |
333
|
1 |
|
$ps = self::$db->prepare('DELETE FROM "Group" WHERE group_pk = :group_pk;'); |
334
|
|
|
|
335
|
1 |
|
$group_pk = $this->groupDataStore[$id]; |
336
|
|
|
|
337
|
1 |
|
$this->executeOrFail($ps, [':group_pk' => $group_pk]); |
338
|
|
|
|
339
|
1 |
|
unset($this->groupDataStore[$id]); |
340
|
1 |
|
} |
341
|
|
|
|
342
|
9 |
|
public function getUsersFromGroup(Group $group) |
343
|
|
|
{ |
344
|
9 |
|
$ps = self::$db->prepare('SELECT user_pk, u.user_id, name, surname, login FROM User u'. |
345
|
|
|
' INNER JOIN user_group rel ON u.user_pk = rel.user_fk'. |
346
|
9 |
|
' WHERE rel.group_fk = :group_fk;'); |
347
|
|
|
|
348
|
9 |
|
$id = $group->getId(); |
349
|
9 |
|
$group_fk = $this->groupDataStore[$id]; |
350
|
|
|
|
351
|
9 |
|
$resultSet = $this->queryOrFail($ps, [':group_fk' => $group_fk]); |
352
|
|
|
|
353
|
9 |
|
$users = []; |
354
|
9 |
|
foreach ($resultSet as $entry) { |
355
|
6 |
|
$ref = new \ReflectionClass(User::class); |
356
|
6 |
|
$user = $this->castUser($ref->newInstanceWithoutConstructor()); |
357
|
|
|
|
358
|
6 |
|
$user->setId($entry['user_id']); |
359
|
6 |
|
$user->setName($entry['name']); |
360
|
6 |
|
$user->setSurname($entry['surname']); |
361
|
6 |
|
$user->setLogin($entry['login']); |
362
|
|
|
|
363
|
6 |
|
$users[$entry['user_id']] = $user; |
364
|
|
|
} |
365
|
|
|
|
366
|
9 |
|
$available = array_diff_assoc($this->getAllUsers(), $users); |
367
|
|
|
$r = [ |
368
|
9 |
|
'members' => $users, |
369
|
9 |
|
'available' => $available |
370
|
|
|
]; |
371
|
|
|
|
372
|
9 |
|
return $r; |
373
|
|
|
} |
374
|
|
|
|
375
|
9 |
|
private function getAllUsers(): array |
376
|
|
|
{ |
377
|
9 |
|
$ps = self::$db->prepare('SELECT user_pk, user_id, name, surname, login FROM User;'); |
378
|
|
|
|
379
|
9 |
|
$resultSet = $this->queryOrFail($ps); |
380
|
|
|
|
381
|
9 |
|
$users = []; |
382
|
9 |
|
foreach ($resultSet as $entry) { |
383
|
9 |
|
$ref = new \ReflectionClass(User::class); |
384
|
9 |
|
$user = $this->castUser($ref->newInstanceWithoutConstructor()); |
385
|
|
|
|
386
|
9 |
|
$user->setId($entry['user_id']); |
387
|
9 |
|
$user->setName($entry['name']); |
388
|
9 |
|
$user->setSurname($entry['surname']); |
389
|
9 |
|
$user->setLogin($entry['login']); |
390
|
|
|
|
391
|
9 |
|
$this->userDataStore[$entry['user_id']] = $entry['user_pk']; |
392
|
|
|
|
393
|
9 |
|
$users[$entry['user_id']] = $user; |
394
|
|
|
} |
395
|
|
|
|
396
|
9 |
|
return $users; |
397
|
|
|
} |
398
|
|
|
|
399
|
8 |
|
public function addUserToGroup(User $user, Group $group) |
400
|
|
|
{ |
401
|
|
|
/*$ps = self::$db->prepare('INSERT INTO user_group (user_fk, group_fk, user_id, group_id)'. |
402
|
|
|
' VALUES (:user_fk, :group_fk, :user_id, :group_id);');*/ |
403
|
8 |
|
$ps = self::$db->prepare('INSERT INTO user_group (user_fk, group_fk)'. |
404
|
8 |
|
' VALUES (:user_fk, :group_fk);'); |
405
|
|
|
|
406
|
8 |
|
$this->executeOrFail($ps, [ |
407
|
8 |
|
':user_fk' => $this->userDataStore[$user->getId()], |
408
|
8 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
409
|
|
|
//':user_id' => $user->getId(), |
410
|
|
|
//':group_id' => $group->getId() |
411
|
|
|
]); |
412
|
8 |
|
} |
413
|
|
|
|
414
|
1 |
|
public function removeUserFromGroup(User $user, Group $group) |
415
|
|
|
{ |
416
|
1 |
|
$ps = self::$db->prepare('DELETE FROM user_group WHERE user_fk = :user_fk'. |
417
|
1 |
|
' AND group_fk = :group_fk;'); |
418
|
|
|
|
419
|
1 |
|
$this->executeOrFail($ps, [ |
420
|
1 |
|
':user_fk' => $this->userDataStore[$user->getId()], |
421
|
1 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
422
|
|
|
//':user_id' => $user->getId(), |
423
|
|
|
//':group_id' => $group->getId() |
424
|
|
|
]); |
425
|
1 |
|
} |
426
|
|
|
|
427
|
16 |
|
public function createRole(Role $role) |
428
|
|
|
{ |
429
|
16 |
|
$ps = self::$db->prepare('INSERT INTO Role (role_id, name, description)'. |
430
|
16 |
|
' VALUES(:role_id, :name, :description);'); |
431
|
|
|
|
432
|
16 |
|
$this->executeOrFail($ps, [ |
433
|
16 |
|
':role_id' => $role->getId(), |
434
|
16 |
|
':name' => $role->getName(), |
435
|
16 |
|
':description' => $role->getDesc() |
436
|
|
|
]); |
437
|
|
|
|
438
|
16 |
|
$this->roleDataStore[$role->getId()] = self::$db->lastInsertId(); |
439
|
16 |
|
} |
440
|
|
|
|
441
|
14 |
|
public function readRole($id) |
442
|
|
|
{ |
443
|
14 |
|
$ps = self::$db->prepare('SELECT role_pk, role_id, name, description FROM Role WHERE role_id = :id;'); |
444
|
|
|
|
445
|
14 |
|
$this->executeOrFail($ps, [':id' => $id]); |
446
|
|
|
|
447
|
14 |
|
$resultSet = $ps->fetch(PDO::FETCH_ASSOC); |
448
|
14 |
|
if( $resultSet === false ) |
449
|
1 |
|
throw new InvalidArgumentException("Role with id = {$id} not found"); |
450
|
|
|
|
451
|
13 |
|
$ref = new \ReflectionClass(Role::class); |
452
|
13 |
|
$role = $this->castRole($ref->newInstanceWithoutConstructor()); |
453
|
|
|
|
454
|
13 |
|
$role->setPropertiesBag(['id', 'name', 'desc']); |
455
|
13 |
|
$role->setSettersBag($role->getPropertiesBag()); |
456
|
|
|
|
457
|
13 |
|
$role->setId($resultSet['role_id']); |
458
|
13 |
|
$role->setName($resultSet['name']); |
459
|
13 |
|
$role->setDesc($resultSet['description']); |
460
|
|
|
|
461
|
13 |
|
$this->roleDataStore[$role->getId()] = $resultSet['role_pk']; |
462
|
|
|
|
463
|
13 |
|
return $role; |
464
|
|
|
} |
465
|
|
|
|
466
|
15 |
|
private function castRole($o): Role |
467
|
|
|
{ |
468
|
15 |
|
return $o; |
469
|
|
|
} |
470
|
|
|
|
471
|
3 |
|
public function addGroupToRole(Group $group, Role $role) |
472
|
|
|
{ |
473
|
3 |
|
$ps = self::$db->prepare('INSERT INTO group_role (group_fk, role_fk, group_id, role_id)'. |
474
|
3 |
|
' VALUES (:group_fk, :role_fk, :group_id, :role_id);'); |
475
|
|
|
|
476
|
3 |
|
$this->executeOrFail($ps, [ |
477
|
3 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
478
|
3 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
479
|
3 |
|
':group_id' => $group->getId(), |
480
|
3 |
|
':role_id' => $role->getId() |
481
|
|
|
]); |
482
|
3 |
|
} |
483
|
|
|
|
484
|
3 |
|
public function getGroupsFromRole(Role $role) |
485
|
|
|
{ |
486
|
3 |
|
$ps = self::$db->prepare('SELECT group_pk, g.group_id, name, description FROM "Group" g'. |
487
|
|
|
' INNER JOIN group_role rel ON g.group_pk = rel.group_fk'. |
488
|
3 |
|
' WHERE rel.role_fk = :role_fk;'); |
489
|
|
|
|
490
|
3 |
|
$id = $role->getId(); |
491
|
3 |
|
$role_fk = $this->roleDataStore[$id]; |
492
|
|
|
|
493
|
3 |
|
$resultSet = $this->queryOrFail($ps, [':role_fk' => $role_fk]); |
494
|
|
|
|
495
|
3 |
|
$groups = []; |
496
|
3 |
|
foreach ($resultSet as $entry) { |
497
|
2 |
|
$ref = new \ReflectionClass(Group::class); |
498
|
2 |
|
$group = $this->castGroup($ref->newInstanceWithoutConstructor()); |
499
|
|
|
|
500
|
2 |
|
$group->setPropertiesBag(['id', 'name', 'desc']); |
501
|
2 |
|
$group->setSettersBag($group->getPropertiesBag()); |
502
|
|
|
|
503
|
2 |
|
$group->setId($entry['group_id']); |
504
|
2 |
|
$group->setName($entry['name']); |
505
|
2 |
|
$group->setDesc($entry['description']); |
506
|
|
|
|
507
|
2 |
|
$groups[$entry['group_id']] = $group; |
508
|
|
|
} |
509
|
|
|
|
510
|
3 |
|
$available = array_diff_assoc($this->getAllGroups(), $groups); |
511
|
|
|
$r = [ |
512
|
3 |
|
'grantedby' => $groups, |
513
|
3 |
|
'available' => $available |
514
|
|
|
]; |
515
|
|
|
|
516
|
3 |
|
return $r; |
517
|
|
|
} |
518
|
|
|
|
519
|
1 |
|
public function removeGroupFromRole(Group $group, Role $role) |
520
|
|
|
{ |
521
|
1 |
|
$ps = self::$db->prepare('DELETE FROM group_role WHERE group_fk = :group_fk'. |
522
|
1 |
|
' AND role_fk = :role_fk;'); |
523
|
|
|
|
524
|
1 |
|
$this->executeOrFail($ps, [ |
525
|
1 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
526
|
1 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
527
|
|
|
//':group_id' => $group->getId(), |
528
|
|
|
//':role_id' => $role->getId() |
529
|
|
|
]); |
530
|
1 |
|
} |
531
|
|
|
|
532
|
1 |
|
public function addGroupToUser(Group $group, User $user) |
533
|
|
|
{ |
534
|
1 |
|
$ps = self::$db->prepare('INSERT INTO user_group (user_fk, group_fk, group_id, user_id)'. |
535
|
1 |
|
' VALUES (:user_fk, :group_fk, :user_id, :group_id);'); |
536
|
|
|
|
537
|
1 |
|
$this->executeOrFail($ps, [ |
538
|
1 |
|
':user_fk' => $this->userDataStore[$user->getId()], |
539
|
1 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
540
|
1 |
|
':user_id' => $user->getId(), |
541
|
1 |
|
':group_id' => $group->getId() |
542
|
|
|
]); |
543
|
1 |
|
} |
544
|
|
|
|
545
|
1 |
|
public function removeGroupFromUser(Group $group, User $user) |
546
|
|
|
{ |
547
|
1 |
|
$ps = self::$db->prepare('DELETE FROM user_group WHERE user_fk = :user_fk'. |
548
|
1 |
|
' AND group_fk = :group_fk;'); |
549
|
|
|
|
550
|
1 |
|
$this->executeOrFail($ps, [ |
551
|
1 |
|
':user_fk' => $this->userDataStore[$user->getId()], |
552
|
1 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
553
|
|
|
//':role_id' => $role->getId() |
554
|
|
|
//':group_id' => $group->getId(), |
555
|
|
|
]); |
556
|
1 |
|
} |
557
|
|
|
|
558
|
1 |
|
public function updateRole($id, $name, $desc) |
559
|
|
|
{ |
560
|
1 |
|
$ps = self::$db->prepare('UPDATE Role SET '. |
561
|
|
|
'name = :name,'. |
562
|
|
|
'description = :description'. |
563
|
1 |
|
' WHERE role_id = :id;'); |
564
|
|
|
|
565
|
1 |
|
$this->executeOrFail($ps, [ |
566
|
1 |
|
':id' => $id, |
567
|
1 |
|
':name' => $name, |
568
|
1 |
|
':description' => $desc |
569
|
|
|
]); |
570
|
1 |
|
} |
571
|
|
|
|
572
|
1 |
|
public function deleteRole($id) |
573
|
|
|
{ |
574
|
1 |
|
$ps = self::$db->prepare('DELETE FROM Role WHERE role_pk = :role_pk;'); |
575
|
|
|
|
576
|
1 |
|
$role_pk = $this->roleDataStore[$id]; |
577
|
|
|
|
578
|
1 |
|
$this->executeOrFail($ps, [':role_pk' => $role_pk]); |
579
|
|
|
|
580
|
1 |
|
unset($this->roleDataStore[$id]); |
581
|
1 |
|
} |
582
|
|
|
|
583
|
3 |
|
public function addRoleToGroup(Role $role, Group $group) |
584
|
|
|
{ |
585
|
3 |
|
$ps = self::$db->prepare('INSERT INTO group_role (group_fk, role_fk, group_id, role_id)'. |
586
|
3 |
|
' VALUES (:group_fk, :role_fk, :group_id, :role_id);'); |
587
|
|
|
|
588
|
3 |
|
$this->executeOrFail($ps, [ |
589
|
3 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
590
|
3 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
591
|
3 |
|
':group_id' => $group->getId(), |
592
|
3 |
|
':role_id' => $role->getId() |
593
|
|
|
]); |
594
|
3 |
|
} |
595
|
|
|
|
596
|
3 |
|
public function getRolesFromGroup(Group $group) |
597
|
|
|
{ |
598
|
3 |
|
$ps = self::$db->prepare('SELECT role_pk, r.role_id, name, description FROM Role r'. |
599
|
|
|
' INNER JOIN group_role rel ON r.role_pk = rel.role_fk'. |
600
|
3 |
|
' WHERE rel.group_fk = :group_fk;'); |
601
|
|
|
|
602
|
3 |
|
$id = $group->getId(); |
603
|
3 |
|
$group_fk = $this->groupDataStore[$id]; |
604
|
|
|
|
605
|
3 |
|
$resultSet = $this->queryOrFail($ps, [':group_fk' => $group_fk]); |
606
|
|
|
|
607
|
3 |
|
$roles = []; |
608
|
3 |
|
foreach ($resultSet as $entry) { |
609
|
2 |
|
$ref = new \ReflectionClass(Role::class); |
610
|
2 |
|
$role = $this->castRole($ref->newInstanceWithoutConstructor()); |
611
|
|
|
|
612
|
2 |
|
$role->setPropertiesBag(['id', 'name', 'desc']); |
613
|
2 |
|
$role->setSettersBag($role->getPropertiesBag()); |
614
|
|
|
|
615
|
2 |
|
$role->setId($entry['role_id']); |
616
|
2 |
|
$role->setName($entry['name']); |
617
|
2 |
|
$role->setDesc($entry['description']); |
618
|
|
|
|
619
|
2 |
|
$roles[$entry['role_id']] = $role; |
620
|
|
|
} |
621
|
|
|
|
622
|
3 |
|
$available = array_diff_assoc($this->getAllRoles(), $roles); |
623
|
|
|
$r = [ |
624
|
3 |
|
'plays' => $roles, |
625
|
3 |
|
'available' => $available |
626
|
|
|
]; |
627
|
|
|
|
628
|
3 |
|
return $r; |
629
|
|
|
} |
630
|
|
|
|
631
|
6 |
|
private function getAllRoles() |
632
|
|
|
{ |
633
|
6 |
|
$ps = self::$db->prepare('SELECT role_pk, role_id, role_id, name, description FROM Role;'); |
634
|
|
|
|
635
|
6 |
|
$resultSet = $this->queryOrFail($ps); |
636
|
|
|
|
637
|
6 |
|
$roles = []; |
638
|
6 |
|
foreach ($resultSet as $entry) { |
639
|
6 |
|
$ref = new \ReflectionClass(Role::class); |
640
|
6 |
|
$role = $this->castRole($ref->newInstanceWithoutConstructor()); |
641
|
|
|
|
642
|
6 |
|
$role->setPropertiesBag(['id', 'name', 'desc']); |
643
|
6 |
|
$role->setSettersBag($role->getPropertiesBag()); |
644
|
|
|
|
645
|
6 |
|
$role->setId($entry['role_id']); |
646
|
6 |
|
$role->setName($entry['name']); |
647
|
6 |
|
$role->setDesc($entry['description']); |
648
|
|
|
|
649
|
6 |
|
$this->roleDataStore[$entry['role_id']] = $entry['role_pk']; |
650
|
|
|
|
651
|
6 |
|
$roles[$entry['role_id']] = $role; |
652
|
|
|
} |
653
|
|
|
|
654
|
6 |
|
return $roles; |
655
|
|
|
} |
656
|
|
|
|
657
|
1 |
|
public function removeRoleFromGroup(Role $role, Group $group) |
658
|
|
|
{ |
659
|
1 |
|
$ps = self::$db->prepare('DELETE FROM group_role WHERE role_fk = :role_fk'. |
660
|
1 |
|
' AND group_fk = :group_fk;'); |
661
|
|
|
|
662
|
1 |
|
$this->executeOrFail($ps, [ |
663
|
1 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
664
|
1 |
|
':group_fk' => $this->groupDataStore[$group->getId()], |
665
|
|
|
//':role_id' => $role->getId(), |
666
|
|
|
//':group_id' => $group->getId() |
667
|
|
|
]); |
668
|
1 |
|
} |
669
|
|
|
|
670
|
6 |
|
public function addModuleToRole(Module $module, Role $role) |
671
|
|
|
{ |
672
|
6 |
|
$ps = self::$db->prepare('INSERT INTO role_module (role_fk, module_fk, role_id, module_id)'. |
673
|
6 |
|
' VALUES (:role_fk, :module_fk, :role_id, :module_id);'); |
674
|
|
|
|
675
|
6 |
|
$this->executeOrFail($ps, [ |
676
|
6 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
677
|
6 |
|
':module_fk' => $this->moduleDataStore[$module->getId()], |
678
|
6 |
|
':role_id' => $role->getId(), |
679
|
6 |
|
':module_id' => $module->getId() |
680
|
|
|
]); |
681
|
6 |
|
} |
682
|
|
|
|
683
|
2 |
|
public function removeModuleFromRole(Module $module, Role $role) |
684
|
|
|
{ |
685
|
2 |
|
$ps = self::$db->prepare('DELETE FROM role_module WHERE role_fk = :role_fk'. |
686
|
2 |
|
' AND module_fk = :module_fk;'); |
687
|
|
|
|
688
|
2 |
|
$this->executeOrFail($ps, [ |
689
|
2 |
|
':module_fk' => $this->moduleDataStore[$module->getId()], |
690
|
2 |
|
':role_fk' => $this->roleDataStore[$role->getId()], |
691
|
|
|
//':module_id' => $module->getId(), |
692
|
|
|
//':role_id' => $role->getId() |
693
|
|
|
]); |
694
|
2 |
|
} |
695
|
|
|
|
696
|
3 |
|
public function getModulesFromRole(Role $role) |
697
|
|
|
{ |
698
|
3 |
|
$ps = self::$db->prepare('SELECT module_pk, m.module_id, name, description FROM Module m'. |
699
|
|
|
' INNER JOIN role_module rel ON m.module_pk = rel.module_fk'. |
700
|
3 |
|
' WHERE rel.role_fk = :role_fk;'); |
701
|
|
|
|
702
|
3 |
|
$id = $role->getId(); |
703
|
3 |
|
$role_fk = $this->roleDataStore[$id]; |
704
|
|
|
|
705
|
3 |
|
$resultSet = $this->queryOrFail($ps, [':role_fk' => $role_fk]); |
706
|
|
|
|
707
|
3 |
|
$modules = []; |
708
|
3 |
|
foreach ($resultSet as $entry) { |
709
|
2 |
|
$ref = new \ReflectionClass(Module::class); |
710
|
2 |
|
$module = $this->castModule($ref->newInstanceWithoutConstructor()); |
711
|
|
|
|
712
|
2 |
|
$module->setPropertiesBag(['id', 'name', 'desc']); |
713
|
2 |
|
$module->setSettersBag($module->getPropertiesBag()); |
714
|
|
|
|
715
|
2 |
|
$module->setId($entry['module_id']); |
716
|
2 |
|
$module->setName($entry['name']); |
717
|
2 |
|
$module->setDesc($entry['description']); |
718
|
|
|
|
719
|
2 |
|
$modules[$entry['module_id']] = $module; |
720
|
|
|
} |
721
|
|
|
|
722
|
3 |
|
$available = array_diff_assoc($this->getAllModules(), $modules); |
723
|
|
|
$r = [ |
724
|
3 |
|
'canrun' => $modules, |
725
|
3 |
|
'available' => $available |
726
|
|
|
]; |
727
|
|
|
|
728
|
3 |
|
return $r; |
729
|
|
|
} |
730
|
|
|
|
731
|
9 |
|
private function castModule($o): Module |
732
|
|
|
{ |
733
|
9 |
|
return $o; |
734
|
|
|
} |
735
|
|
|
|
736
|
3 |
|
private function getAllModules() |
737
|
|
|
{ |
738
|
3 |
|
$ps = self::$db->prepare('SELECT module_pk, module_id, module_id, name, description FROM Module;'); |
739
|
|
|
|
740
|
3 |
|
$resultSet = $this->queryOrFail($ps); |
741
|
|
|
|
742
|
3 |
|
$modules = []; |
743
|
3 |
|
foreach ($resultSet as $entry) { |
744
|
3 |
|
$ref = new \ReflectionClass(Module::class); |
745
|
3 |
|
$module = $this->castModule($ref->newInstanceWithoutConstructor()); |
746
|
|
|
|
747
|
3 |
|
$module->setPropertiesBag(['id', 'name', 'desc']); |
748
|
3 |
|
$module->setSettersBag($module->getPropertiesBag()); |
749
|
|
|
|
750
|
3 |
|
$module->setId($entry['module_id']); |
751
|
3 |
|
$module->setName($entry['name']); |
752
|
3 |
|
$module->setDesc($entry['description']); |
753
|
|
|
|
754
|
3 |
|
$this->moduleDataStore[$entry['module_id']] = $entry['module_pk']; |
755
|
|
|
|
756
|
3 |
|
$modules[$entry['module_id']] = $module; |
757
|
|
|
} |
758
|
|
|
|
759
|
3 |
|
return $modules; |
760
|
|
|
} |
761
|
|
|
|
762
|
10 |
|
public function createModule(Module $module) |
763
|
|
|
{ |
764
|
10 |
|
$ps = self::$db->prepare('INSERT INTO Module (module_id, name, description)'. |
765
|
10 |
|
' VALUES(:module_id, :name, :description);'); |
766
|
10 |
|
$this->executeOrFail($ps, [ |
767
|
10 |
|
':module_id' => $module->getId(), |
768
|
10 |
|
':name' => $module->getName(), |
769
|
10 |
|
':description' => $module->getDesc() |
770
|
|
|
]); |
771
|
|
|
|
772
|
10 |
|
$this->moduleDataStore[$module->getId()] = self::$db->lastInsertId(); |
773
|
10 |
|
} |
774
|
|
|
|
775
|
9 |
|
public function readModule($id) |
776
|
|
|
{ |
777
|
9 |
|
$ps = self::$db->prepare('SELECT module_pk, module_id, name, description FROM Module WHERE module_id = :id;'); |
778
|
|
|
|
779
|
9 |
|
$this->executeOrFail($ps, [':id' => $id]); |
780
|
|
|
|
781
|
9 |
|
$resultSet = $ps->fetch(PDO::FETCH_ASSOC); |
782
|
9 |
|
if( $resultSet === false ) |
783
|
1 |
|
throw new InvalidArgumentException("Module with id = {$id} not found"); |
784
|
|
|
|
785
|
8 |
|
$ref = new \ReflectionClass(Module::class); |
786
|
8 |
|
$module = $this->castModule($ref->newInstanceWithoutConstructor()); |
787
|
|
|
|
788
|
8 |
|
$module->setPropertiesBag(['id', 'name', 'desc']); |
789
|
8 |
|
$module->setSettersBag($module->getPropertiesBag()); |
790
|
|
|
|
791
|
8 |
|
$module->setId($resultSet['module_id']); |
792
|
8 |
|
$module->setName($resultSet['name']); |
793
|
8 |
|
$module->setDesc($resultSet['description']); |
794
|
|
|
|
795
|
8 |
|
$this->moduleDataStore[$module->getId()] = $resultSet['module_pk']; |
796
|
|
|
|
797
|
8 |
|
return $module; |
798
|
|
|
} |
799
|
|
|
|
800
|
1 |
|
public function updateModule($id, $name, $desc) |
801
|
|
|
{ |
802
|
1 |
|
$ps = self::$db->prepare('UPDATE Module SET '. |
803
|
|
|
'name = :name,'. |
804
|
|
|
'description = :description'. |
805
|
1 |
|
' WHERE module_id = :id;'); |
806
|
|
|
|
807
|
1 |
|
$this->executeOrFail($ps, [ |
808
|
1 |
|
':id' => $id, |
809
|
1 |
|
':name' => $name, |
810
|
1 |
|
':description' => $desc |
811
|
|
|
]); |
812
|
1 |
|
} |
813
|
|
|
|
814
|
1 |
|
public function deleteModule($id) |
815
|
|
|
{ |
816
|
1 |
|
$ps = self::$db->prepare('DELETE FROM Module WHERE module_pk = :module_pk;'); |
817
|
|
|
|
818
|
1 |
|
$module_pk = $this->moduleDataStore[$id]; |
819
|
|
|
|
820
|
1 |
|
$this->executeOrFail($ps, [':module_pk' => $module_pk]); |
821
|
|
|
|
822
|
1 |
|
unset($this->moduleDataStore[$id]); |
823
|
1 |
|
} |
824
|
|
|
|
825
|
3 |
|
public function addRoleToModule(Role $role, Module $module) |
826
|
|
|
{ |
827
|
3 |
|
$this->addModuleToRole($module, $role); |
828
|
3 |
|
} |
829
|
|
|
|
830
|
1 |
|
public function removeRoleFromModule(Role $role, Module $module) |
831
|
|
|
{ |
832
|
1 |
|
$this->removeModuleFromRole($module, $role); |
833
|
1 |
|
} |
834
|
|
|
|
835
|
3 |
|
public function getRolesFromModule(Module $module) |
836
|
|
|
{ |
837
|
3 |
|
$ps = self::$db->prepare('SELECT role_pk, r.role_id, name, description FROM Role r'. |
838
|
|
|
' INNER JOIN role_module rel ON r.role_pk = rel.role_fk'. |
839
|
3 |
|
' WHERE rel.module_fk = :module_fk;'); |
840
|
|
|
|
841
|
3 |
|
$id = $module->getId(); |
842
|
3 |
|
$module_fk = $this->moduleDataStore[$id]; |
843
|
|
|
|
844
|
3 |
|
$resultSet = $this->queryOrFail($ps, [':module_fk' => $module_fk]); |
845
|
|
|
|
846
|
3 |
|
$roles = []; |
847
|
3 |
|
foreach ($resultSet as $entry) { |
848
|
2 |
|
$ref = new \ReflectionClass(Role::class); |
849
|
2 |
|
$role = $this->castRole($ref->newInstanceWithoutConstructor()); |
850
|
|
|
|
851
|
2 |
|
$role->setPropertiesBag(['id', 'name', 'desc']); |
852
|
2 |
|
$role->setSettersBag($role->getPropertiesBag()); |
853
|
|
|
|
854
|
2 |
|
$role->setId($entry['role_id']); |
855
|
2 |
|
$role->setName($entry['name']); |
856
|
2 |
|
$role->setDesc($entry['description']); |
857
|
|
|
|
858
|
2 |
|
$roles[$entry['role_id']] = $role; |
859
|
|
|
} |
860
|
|
|
|
861
|
3 |
|
$available = array_diff_assoc($this->getAllRoles(), $roles); |
862
|
|
|
$r = [ |
863
|
3 |
|
'authorizedby' => $roles, |
864
|
3 |
|
'available' => $available |
865
|
|
|
]; |
866
|
|
|
|
867
|
3 |
|
return $r; |
868
|
|
|
} |
869
|
|
|
} |
870
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths