service = $service; $this->cacheServiceId = $cacheServiceId; $this->ttl = $ttl; $this->validatePermissionsCacheKeyFactory = $validatePermissionsCacheKeyFactory; $this->getAllChildrenCacheKeyFactory = $getAllChildrenCacheKeyFactory; } /** * @inheritDoc * * @throws common_exception_Error * @throws common_cache_NotFoundException */ public function getAllChildren(core_kernel_classes_Class $resource): array { $user = $this->getUser(); $cacheKey = $this->getAllChildrenCacheKeyFactory->create($resource, $user); $cache = $this->getCache(); if ($cache && $cache->has($cacheKey)) { return $cache->get($cacheKey)->getInstances(); } $accessibleInstances = $this->getService()->getAllChildren($resource); $this->addToCache($cacheKey, new SecureResourceServiceAllChildrenCacheCollection($accessibleInstances)); return $accessibleInstances; } /** * @inheritDoc * * @throws common_exception_Error * @throws common_cache_NotFoundException */ public function validatePermissions(iterable $resources, array $permissionsToCheck): void { foreach ($resources as $resource) { $this->validatePermission($resource, $permissionsToCheck); } } /** * @inheritDoc * * @throws common_exception_Error * @throws common_cache_NotFoundException */ public function validatePermission($resource, array $permissionsToCheck): void { $user = $this->getUser(); $resourceUri = $resource instanceof core_kernel_classes_Resource ? $resource->getUri() : $resource; $cacheKey = $this->validatePermissionsCacheKeyFactory->create($resourceUri, $user); $cache = $this->getCache(); if ($cache && $cache->has($cacheKey)) { $hasAccess = $cache->get($cacheKey); if (!$hasAccess) { throw new ResourceAccessDeniedException($resourceUri); } return; } try { $this->getService()->validatePermission($resource, $permissionsToCheck); } catch (ResourceAccessDeniedException $e) { $this->addToCache($cacheKey, false); throw $e; } $this->addToCache($cacheKey, true); } /** * @return SecureResourceService */ public function getService(): SecureResourceServiceInterface { $this->service->setServiceLocator($this->getServiceLocator()); return $this->service; } private function addToCache(string $cacheKey, $data) { $cache = $this->getCache(); if ($cache) { $cache->put( $data, $cacheKey, $this->ttl ); } } private function getCache(): ?common_cache_Cache { $isCacheEnabled = !empty(trim($this->cacheServiceId)); if (!$isCacheEnabled) { return null; } $this->cache = $this->getServiceLocator()->get($this->cacheServiceId); return $this->cache; } /** * @return User * * @throws common_exception_Error */ private function getUser(): User { if ($this->user === null) { $this->user = $this ->getServiceLocator() ->get(SessionService::SERVICE_ID) ->getCurrentUser(); } return $this->user; } }