Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.67% |
13 / 15 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PermissionChecker | |
86.67% |
13 / 15 |
|
71.43% |
5 / 7 |
8.15 | |
0.00% |
0 / 1 |
| withAccessControl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasWriteAccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasReadAccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasGrantAccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasAccess | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAccessControl | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 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) 2014-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\accessControl; |
| 25 | |
| 26 | use common_session_SessionManager; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\oatbox\user\User; |
| 29 | use oat\tao\model\accessControl\data\DataAccessControl; |
| 30 | |
| 31 | class PermissionChecker extends ConfigurableService implements PermissionCheckerInterface |
| 32 | { |
| 33 | /** @var AccessControl */ |
| 34 | private $dataAccessControl; |
| 35 | |
| 36 | public function withAccessControl(AccessControl $dataAccessControl): self |
| 37 | { |
| 38 | $this->dataAccessControl = $dataAccessControl; |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function hasWriteAccess(string $resourceId, User $user = null): bool |
| 44 | { |
| 45 | return $this->hasAccess($resourceId, self::PERMISSION_WRITE, $user); |
| 46 | } |
| 47 | |
| 48 | public function hasReadAccess(string $resourceId, User $user = null): bool |
| 49 | { |
| 50 | return $this->hasAccess($resourceId, self::PERMISSION_READ, $user); |
| 51 | } |
| 52 | |
| 53 | public function hasGrantAccess(string $resourceId, User $user = null): bool |
| 54 | { |
| 55 | return $this->hasAccess($resourceId, self::PERMISSION_GRANT, $user); |
| 56 | } |
| 57 | |
| 58 | private function hasAccess(string $resourceId, string $access, User $user = null): bool |
| 59 | { |
| 60 | return $this->getAccessControl()->hasPrivileges( |
| 61 | $user ?? $this->getUser(), |
| 62 | [ |
| 63 | $resourceId => $access |
| 64 | ] |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | private function getUser(): User |
| 69 | { |
| 70 | return common_session_SessionManager::getSession()->getUser(); |
| 71 | } |
| 72 | |
| 73 | private function getAccessControl(): AccessControl |
| 74 | { |
| 75 | if (!$this->dataAccessControl) { |
| 76 | $this->dataAccessControl = new DataAccessControl(); |
| 77 | } |
| 78 | |
| 79 | return $this->dataAccessControl; |
| 80 | } |
| 81 | } |