Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
24 / 27 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ResultAccessChecker | |
88.89% |
24 / 27 |
|
33.33% |
1 / 3 |
9.11 | |
0.00% |
0 / 1 |
| hasReadAccess | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
| hasReadPermissionForClass | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
4.07 | |||
| getPermissionHelper | |
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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\search; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use oat\generis\model\data\permission\PermissionHelper; |
| 27 | use oat\generis\model\data\permission\PermissionInterface; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\tao\model\TaoOntology; |
| 31 | |
| 32 | class ResultAccessChecker extends ConfigurableService |
| 33 | { |
| 34 | use OntologyAwareTrait; |
| 35 | |
| 36 | public function hasReadAccess(array $content): bool |
| 37 | { |
| 38 | $resource = $this->getResource($content['id']); |
| 39 | |
| 40 | $topLevelClass = $this->getClass(TaoOntology::CLASS_URI_OBJECT); |
| 41 | |
| 42 | $permissionHelper = $this->getPermissionHelper(); |
| 43 | |
| 44 | foreach ($resource->getTypes() as $type) { |
| 45 | $accessibleResources = $permissionHelper->filterByPermission( |
| 46 | [$type->getUri()], |
| 47 | PermissionInterface::RIGHT_READ |
| 48 | ); |
| 49 | |
| 50 | if (empty($accessibleResources)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | $class = $this->getClass($type->getUri()); |
| 55 | |
| 56 | if (!$this->hasReadPermissionForClass($class, $permissionHelper, $topLevelClass)) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | private function hasReadPermissionForClass( |
| 65 | core_kernel_classes_Class $class, |
| 66 | PermissionHelper $permissionHelper, |
| 67 | core_kernel_classes_Class $topLevelClass |
| 68 | ): bool { |
| 69 | $parentClasses = $class->getParentClasses(true); |
| 70 | |
| 71 | foreach ($parentClasses as $parentClass) { |
| 72 | $accessibleResource = $permissionHelper |
| 73 | ->filterByPermission( |
| 74 | [$parentClass->getUri()], |
| 75 | PermissionInterface::RIGHT_READ |
| 76 | ); |
| 77 | |
| 78 | if (empty($accessibleResource)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if ($parentClass->getUri() === $topLevelClass->getUri()) { |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | private function getPermissionHelper(): PermissionHelper |
| 90 | { |
| 91 | return $this->getServiceLocator()->get(PermissionHelper::class); |
| 92 | } |
| 93 | } |