Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.84% |
45 / 49 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| SecureResourceCachedService | |
91.84% |
45 / 49 |
|
75.00% |
6 / 8 |
19.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getAllChildren | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| validatePermissions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| validatePermission | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
6.35 | |||
| getService | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addToCache | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| getCache | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| getUser | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2013-2020 (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\resources; |
| 25 | |
| 26 | use common_cache_Cache; |
| 27 | use common_cache_NotFoundException; |
| 28 | use common_exception_Error; |
| 29 | use core_kernel_classes_Class; |
| 30 | use core_kernel_classes_Resource; |
| 31 | use oat\oatbox\session\SessionService; |
| 32 | use oat\oatbox\user\User; |
| 33 | use oat\tao\model\service\InjectionAwareService; |
| 34 | |
| 35 | class SecureResourceCachedService extends InjectionAwareService implements SecureResourceServiceInterface |
| 36 | { |
| 37 | /** @var User */ |
| 38 | private $user; |
| 39 | /** @var SecureResourceService */ |
| 40 | private $service; |
| 41 | /** @var string */ |
| 42 | private $cacheServiceId; |
| 43 | /** @var int|null */ |
| 44 | private $ttl; |
| 45 | /** @var common_cache_Cache */ |
| 46 | private $cache; |
| 47 | /** @var ValidatePermissionsCacheKeyFactory */ |
| 48 | private $validatePermissionsCacheKeyFactory; |
| 49 | /** @var GetAllChildrenCacheKeyFactory */ |
| 50 | private $getAllChildrenCacheKeyFactory; |
| 51 | |
| 52 | /** |
| 53 | * @noinspection MagicMethodsValidityInspection |
| 54 | * @noinspection PhpMissingParentConstructorInspection |
| 55 | * |
| 56 | * @param SecureResourceService $service |
| 57 | * @param ValidatePermissionsCacheKeyFactory $validatePermissionsCacheKeyFactory |
| 58 | * @param GetAllChildrenCacheKeyFactory $getAllChildrenCacheKeyFactory |
| 59 | * @param string $cacheServiceId |
| 60 | * @param int $ttl |
| 61 | */ |
| 62 | public function __construct( |
| 63 | SecureResourceService $service, |
| 64 | ValidatePermissionsCacheKeyFactory $validatePermissionsCacheKeyFactory, |
| 65 | GetAllChildrenCacheKeyFactory $getAllChildrenCacheKeyFactory, |
| 66 | string $cacheServiceId, |
| 67 | ?int $ttl |
| 68 | ) { |
| 69 | $this->service = $service; |
| 70 | $this->cacheServiceId = $cacheServiceId; |
| 71 | $this->ttl = $ttl; |
| 72 | $this->validatePermissionsCacheKeyFactory = $validatePermissionsCacheKeyFactory; |
| 73 | $this->getAllChildrenCacheKeyFactory = $getAllChildrenCacheKeyFactory; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @inheritDoc |
| 78 | * |
| 79 | * @throws common_exception_Error |
| 80 | * @throws common_cache_NotFoundException |
| 81 | */ |
| 82 | public function getAllChildren(core_kernel_classes_Class $resource): array |
| 83 | { |
| 84 | $user = $this->getUser(); |
| 85 | |
| 86 | $cacheKey = $this->getAllChildrenCacheKeyFactory->create($resource, $user); |
| 87 | |
| 88 | $cache = $this->getCache(); |
| 89 | if ($cache && $cache->has($cacheKey)) { |
| 90 | return $cache->get($cacheKey)->getInstances(); |
| 91 | } |
| 92 | |
| 93 | $accessibleInstances = $this->getService()->getAllChildren($resource); |
| 94 | |
| 95 | $this->addToCache($cacheKey, new SecureResourceServiceAllChildrenCacheCollection($accessibleInstances)); |
| 96 | |
| 97 | return $accessibleInstances; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @inheritDoc |
| 102 | * |
| 103 | * @throws common_exception_Error |
| 104 | * @throws common_cache_NotFoundException |
| 105 | */ |
| 106 | public function validatePermissions(iterable $resources, array $permissionsToCheck): void |
| 107 | { |
| 108 | foreach ($resources as $resource) { |
| 109 | $this->validatePermission($resource, $permissionsToCheck); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @inheritDoc |
| 115 | * |
| 116 | * @throws common_exception_Error |
| 117 | * @throws common_cache_NotFoundException |
| 118 | */ |
| 119 | public function validatePermission($resource, array $permissionsToCheck): void |
| 120 | { |
| 121 | $user = $this->getUser(); |
| 122 | |
| 123 | $resourceUri = $resource instanceof core_kernel_classes_Resource ? $resource->getUri() : $resource; |
| 124 | |
| 125 | $cacheKey = $this->validatePermissionsCacheKeyFactory->create($resourceUri, $user); |
| 126 | |
| 127 | $cache = $this->getCache(); |
| 128 | if ($cache && $cache->has($cacheKey)) { |
| 129 | $hasAccess = $cache->get($cacheKey); |
| 130 | |
| 131 | if (!$hasAccess) { |
| 132 | throw new ResourceAccessDeniedException($resourceUri); |
| 133 | } |
| 134 | |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | try { |
| 139 | $this->getService()->validatePermission($resource, $permissionsToCheck); |
| 140 | } catch (ResourceAccessDeniedException $e) { |
| 141 | $this->addToCache($cacheKey, false); |
| 142 | |
| 143 | throw $e; |
| 144 | } |
| 145 | |
| 146 | $this->addToCache($cacheKey, true); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return SecureResourceService |
| 151 | */ |
| 152 | public function getService(): SecureResourceServiceInterface |
| 153 | { |
| 154 | $this->service->setServiceLocator($this->getServiceLocator()); |
| 155 | |
| 156 | return $this->service; |
| 157 | } |
| 158 | |
| 159 | private function addToCache(string $cacheKey, $data) |
| 160 | { |
| 161 | $cache = $this->getCache(); |
| 162 | |
| 163 | if ($cache) { |
| 164 | $cache->put( |
| 165 | $data, |
| 166 | $cacheKey, |
| 167 | $this->ttl |
| 168 | ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private function getCache(): ?common_cache_Cache |
| 173 | { |
| 174 | $isCacheEnabled = !empty(trim($this->cacheServiceId)); |
| 175 | |
| 176 | if (!$isCacheEnabled) { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | $this->cache = $this->getServiceLocator()->get($this->cacheServiceId); |
| 181 | |
| 182 | return $this->cache; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return User |
| 187 | * |
| 188 | * @throws common_exception_Error |
| 189 | */ |
| 190 | private function getUser(): User |
| 191 | { |
| 192 | if ($this->user === null) { |
| 193 | $this->user = $this |
| 194 | ->getServiceLocator() |
| 195 | ->get(SessionService::SERVICE_ID) |
| 196 | ->getCurrentUser(); |
| 197 | } |
| 198 | |
| 199 | return $this->user; |
| 200 | } |
| 201 | } |