public function PreviewLinkCanonicalRerouteAccessCheck::access in Preview Link 2.0.x
Same name and namespace in other branches
- 2.x src/Access/PreviewLinkCanonicalRerouteAccessCheck.php \Drupal\preview_link\Access\PreviewLinkCanonicalRerouteAccessCheck::access()
Checks if an activated preview link token is associated with this entity.
Parameters
\Symfony\Component\HttpFoundation\Request|null $request: The request.
Return value
\Drupal\Core\Access\AccessResult A \Drupal\Core\Access\AccessInterface value.
Throws
\Drupal\preview_link\Exception\PreviewLinkRerouteException When a claimed token grants access to entity for this route match.
File
- src/
Access/ PreviewLinkCanonicalRerouteAccessCheck.php, line 75
Class
- PreviewLinkCanonicalRerouteAccessCheck
- Reroutes users from a canonical route to preview link route.
Namespace
Drupal\preview_link\AccessCode
public function access(Request $request = NULL) : AccessResultInterface {
$cacheability = (new CacheableMetadata())
->addCacheContexts([
'session',
'route',
]);
// Dont use argument resolved route match or route, get the real route match
// from the master request.
$routeMatch = $this->routeMatch
->getMasterRouteMatch();
$route = $routeMatch
->getRouteObject();
$entityParameterName = $route ? $route
->getRequirement('_access_preview_link_canonical_rerouter') : NULL;
if (!isset($entityParameterName)) {
// If the requirement doesnt exist then the master request isn't the
// canonical route, its probably simulated from something like menu or
// breadcrumb.
return AccessResult::allowed()
->addCacheableDependency($cacheability);
}
$cacheability = (new CacheableMetadata())
->addCacheContexts([
'session',
'route',
]);
if (!$request) {
return AccessResult::allowed()
->addCacheableDependency($cacheability);
}
$entity = $routeMatch
->getParameter($entityParameterName);
if (!$entity instanceof EntityInterface) {
// Entity was not upcast for preview link reroute access check.
return AccessResult::allowed()
->addCacheableDependency($cacheability);
}
$collection = $this->privateTempStoreFactory
->get('preview_link');
$claimedTokens = $collection
->get('keys') ?? [];
if (!$claimedTokens) {
// Session has no claimed tokens.
return AccessResult::allowed()
->addCacheableDependency($cacheability);
}
if (!$this->previewLinkHost
->isToken($entity, $claimedTokens)) {
// This session doesnt have an activated preview link tokens matching this
// entity.
return AccessResult::allowed()
->addCacheableDependency($cacheability);
}
// Check if any keys in this session unlock this entity.
$previewLinks = $this->previewLinkHost
->getPreviewLinks($entity);
// Get the first token that matches this entity.
foreach ($previewLinks as $previewLink) {
if (in_array($previewLink
->getToken(), $claimedTokens, TRUE)) {
throw new PreviewLinkRerouteException('', 0, NULL, $entity, $previewLink);
}
}
throw new \LogicException('Shouldnt get here unless there are implementation differences between isToken and getPreviewLinks.');
}