Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
26 / 28 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DeleteRoleService | |
92.86% |
26 / 28 |
|
80.00% |
4 / 5 |
13.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withForbiddenRoles | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
9 | |||
| deleteDuplicatedFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isForbidden | |
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\accessControl\Service; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\GenerisRdf; |
| 27 | use oat\generis\model\OntologyRdfs; |
| 28 | use oat\tao\model\exceptions\UserErrorException; |
| 29 | use tao_models_classes_RoleService; |
| 30 | |
| 31 | class DeleteRoleService |
| 32 | { |
| 33 | /** @var tao_models_classes_RoleService */ |
| 34 | private $roleService; |
| 35 | |
| 36 | /** @var InternalRoleSpecification */ |
| 37 | private $internalRoleSpecification; |
| 38 | |
| 39 | /** @var string[] */ |
| 40 | private $forbiddenRoles = []; |
| 41 | |
| 42 | public function __construct( |
| 43 | InternalRoleSpecification $internalRoleSpecification, |
| 44 | tao_models_classes_RoleService $roleService |
| 45 | ) { |
| 46 | $this->internalRoleSpecification = $internalRoleSpecification; |
| 47 | $this->roleService = $roleService; |
| 48 | } |
| 49 | |
| 50 | public function withForbiddenRoles(array $forbiddenRoles): self |
| 51 | { |
| 52 | $this->forbiddenRoles = $forbiddenRoles; |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | public function delete(core_kernel_classes_Resource $role): void |
| 58 | { |
| 59 | $isWritable = $role->isWritable(); |
| 60 | |
| 61 | if ($isWritable && $this->internalRoleSpecification->isSatisfiedBy($role)) { |
| 62 | throw new UserErrorException(__('Unable to delete the selected resource')); |
| 63 | } |
| 64 | |
| 65 | if (!$isWritable && $this->deleteDuplicatedFields($role)) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (!$isWritable || $this->isForbidden($role)) { |
| 70 | throw new UserErrorException(__('Unable to delete the selected resource')); |
| 71 | } |
| 72 | |
| 73 | $users = $role->getClass(GenerisRdf::CLASS_GENERIS_USER)->searchInstances( |
| 74 | [ |
| 75 | GenerisRdf::PROPERTY_USER_ROLES => $role->getUri() |
| 76 | ], |
| 77 | [ |
| 78 | 'recursive' => true, |
| 79 | 'like' => false |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | if (!empty($users)) { |
| 84 | throw new UserErrorException( |
| 85 | __('This role is still given to one or more users. Please remove the role to these users first.') |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | if (!$this->roleService->removeRole($role)) { |
| 90 | throw new UserErrorException(__('Unable to delete the selected resource')); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private function deleteDuplicatedFields(core_kernel_classes_Resource $role): bool |
| 95 | { |
| 96 | return $role->removePropertyValues($role->getProperty(OntologyRdfs::RDFS_LABEL)); |
| 97 | } |
| 98 | |
| 99 | private function isForbidden(core_kernel_classes_Resource $role): bool |
| 100 | { |
| 101 | return in_array($role->getUri(), $this->forbiddenRoles, true); |
| 102 | } |
| 103 | } |