Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
PermissionHelper | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
filterByPermission | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getCurrentUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | declare(strict_types=1); |
22 | |
23 | namespace oat\generis\model\data\permission; |
24 | |
25 | use oat\oatbox\service\ConfigurableService; |
26 | use oat\oatbox\session\SessionService; |
27 | use oat\oatbox\user\User; |
28 | |
29 | class PermissionHelper extends ConfigurableService |
30 | { |
31 | /** |
32 | * Filters resources by the provided right if it is supported |
33 | * @return array identifiers of resources that have permission for the provided right |
34 | */ |
35 | public function filterByPermission(array $resourceIds, string $right): array |
36 | { |
37 | $provider = $this->getServiceLocator()->get(PermissionInterface::SERVICE_ID); |
38 | if (!in_array($right, $provider->getSupportedRights())) { |
39 | return $resourceIds; |
40 | } |
41 | $permissions = $provider->getPermissions($this->getCurrentUser(), $resourceIds); |
42 | |
43 | return array_filter($resourceIds, function ($id) use ($right, $permissions) { |
44 | return isset($permissions[$id]) && in_array($right, $permissions[$id]); |
45 | }); |
46 | } |
47 | |
48 | private function getCurrentUser(): User |
49 | { |
50 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
51 | return $this->getServiceLocator()->get(SessionService::SERVICE_ID)->getCurrentUser(); |
52 | } |
53 | } |