Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.94% |
31 / 33 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
SavePermissionsStrategy | |
93.94% |
31 / 33 |
|
33.33% |
1 / 3 |
13.04 | |
0.00% |
0 / 1 |
normalizeRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPermissionsToAdd | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
7.01 | |||
getPermissionsToRemove | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
5.01 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace oat\taoDacSimple\model; |
25 | |
26 | class SavePermissionsStrategy extends PermissionsStrategyAbstract |
27 | { |
28 | public function normalizeRequest(array $currentPrivileges, array $privilegesToSet): array |
29 | { |
30 | return $this->getDeltaPermissions($currentPrivileges, $privilegesToSet); |
31 | } |
32 | |
33 | public function getPermissionsToAdd(array $currentPrivileges, array $addRemove): array |
34 | { |
35 | if (empty($addRemove['add'])) { |
36 | return []; |
37 | } |
38 | |
39 | $permissionsToAdd = $addRemove['add']; |
40 | |
41 | foreach ($permissionsToAdd as $userId => &$permissionToAdd) { |
42 | $permissionsCount = count($permissionToAdd); |
43 | |
44 | if ($permissionsCount < 3) { |
45 | $mandatoryFields = []; |
46 | |
47 | // check attempt to add only one permission which depend on other (w on r, g on w and r) and add |
48 | // dependent permissions if necessary |
49 | |
50 | if (in_array(PermissionProvider::PERMISSION_GRANT, $permissionToAdd, true)) { |
51 | $mandatoryFields = [PermissionProvider::PERMISSION_WRITE, PermissionProvider::PERMISSION_READ]; |
52 | } elseif (in_array(PermissionProvider::PERMISSION_WRITE, $permissionToAdd, true)) { |
53 | $mandatoryFields = [PermissionProvider::PERMISSION_READ]; |
54 | } |
55 | |
56 | $permissionToAdd = array_values(array_diff( |
57 | array_unique(array_merge($permissionToAdd, $mandatoryFields)), |
58 | $currentPrivileges[$userId] ?? [] |
59 | )); |
60 | |
61 | if (empty($permissionToAdd)) { |
62 | unset($permissionsToAdd[$userId]); |
63 | } |
64 | } |
65 | } |
66 | |
67 | return $permissionsToAdd; |
68 | } |
69 | |
70 | public function getPermissionsToRemove(array $currentPrivileges, array $addRemove): array |
71 | { |
72 | if (empty($addRemove['remove'])) { |
73 | return []; |
74 | } |
75 | |
76 | $permissionsToRemove = $addRemove['remove']; |
77 | |
78 | foreach ($permissionsToRemove as $userId => &$permissionToRemove) { |
79 | $mandatoryFields = []; |
80 | |
81 | if (in_array(PermissionProvider::PERMISSION_READ, $permissionToRemove, true)) { |
82 | $mandatoryFields = [PermissionProvider::PERMISSION_WRITE, PermissionProvider::PERMISSION_GRANT]; |
83 | } elseif (in_array(PermissionProvider::PERMISSION_WRITE, $permissionToRemove, true)) { |
84 | $mandatoryFields = [PermissionProvider::PERMISSION_GRANT]; |
85 | } |
86 | |
87 | $permissionToRemove = array_values(array_intersect( |
88 | array_unique(array_merge($permissionToRemove, $mandatoryFields)), |
89 | $currentPrivileges[$userId] ?? [] |
90 | )); |
91 | } |
92 | |
93 | return $permissionsToRemove; |
94 | } |
95 | } |