class PreviewLinkAccessCheck in Preview Link 8
Same name and namespace in other branches
- 2.x src/Access/PreviewLinkAccessCheck.php \Drupal\preview_link\Access\PreviewLinkAccessCheck
- 2.0.x src/Access/PreviewLinkAccessCheck.php \Drupal\preview_link\Access\PreviewLinkAccessCheck
Preview link access check.
Hierarchy
- class \Drupal\preview_link\Access\PreviewLinkAccessCheck implements AccessInterface
Expanded class hierarchy of PreviewLinkAccessCheck
1 string reference to 'PreviewLinkAccessCheck'
1 service uses PreviewLinkAccessCheck
File
- src/
Access/ PreviewLinkAccessCheck.php, line 13
Namespace
Drupal\preview_link\AccessView source
class PreviewLinkAccessCheck implements AccessInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* PreviewLinkAccessCheck constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* Checks access to the node add page for the node type.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
* @param string $preview_token
* The preview token.
*
* @return \Drupal\Core\Access\AccessResult
* A \Drupal\Core\Access\AccessInterface value.
*/
public function access(EntityInterface $entity = NULL, $preview_token = NULL) {
$neutral = AccessResult::neutral()
->addCacheableDependency($entity)
->addCacheContexts([
'preview_link_route',
]);
if (!$preview_token || !$entity) {
return $neutral;
}
/** @var \Drupal\preview_link\Entity\PreviewLinkInterface $preview_link */
$preview_link = $this->entityTypeManager
->getStorage('preview_link')
->getPreviewLink($entity);
// If we can't find a valid preview link then ignore this.
if (!$preview_link) {
return $neutral
->setReason('This entity does not have a preview link.');
}
// If an entity has a preview link and it doesnt match up, then explicitly
// deny access.
if ($preview_token !== $preview_link
->getToken()) {
return AccessResult::forbidden('Preview token is invalid.')
->addCacheableDependency($entity)
->addCacheContexts([
'preview_link_route',
]);
}
return AccessResult::allowed()
->addCacheableDependency($entity)
->addCacheableDependency($preview_link)
->addCacheContexts([
'preview_link_route',
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PreviewLinkAccessCheck:: |
protected | property | The entity type manager. | |
PreviewLinkAccessCheck:: |
public | function | Checks access to the node add page for the node type. | |
PreviewLinkAccessCheck:: |
public | function | PreviewLinkAccessCheck constructor. |