Same filename and directory in other branches
- 8.9.x core/modules/content_moderation/src/Access/LatestRevisionCheck.php
- 9 core/modules/content_moderation/src/Access/LatestRevisionCheck.php
Namespace
Drupal\content_moderation\Access
File
core/modules/content_moderation/src/Access/LatestRevisionCheck.php
View source
<?php
namespace Drupal\content_moderation\Access;
use Drupal\Core\Access\AccessException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\Routing\Route;
class LatestRevisionCheck implements AccessInterface {
protected $moderationInfo;
public function __construct(ModerationInformationInterface $moderation_information) {
$this->moderationInfo = $moderation_information;
}
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
$entity = $this
->loadEntity($route, $route_match);
if ($this->moderationInfo
->hasPendingRevision($entity)) {
$access_result = AccessResult::allowedIfHasPermissions($account, [
'view latest version',
'view any unpublished content',
]);
if (!$access_result
->isAllowed()) {
$owner_access = AccessResult::allowedIfHasPermissions($account, [
'view latest version',
'view own unpublished content',
]);
$owner_access = $owner_access
->andIf(AccessResult::allowedIf($entity instanceof EntityOwnerInterface && $entity
->getOwnerId() == $account
->id()));
$access_result = $access_result
->orIf($owner_access);
}
return $access_result
->addCacheableDependency($entity);
}
return AccessResult::forbidden('No pending revision for moderated entity.')
->addCacheableDependency($entity);
}
protected function loadEntity(Route $route, RouteMatchInterface $route_match) {
$entity_type = $route
->getOption('_content_moderation_entity_type');
if ($entity = $route_match
->getParameter($entity_type)) {
if ($entity instanceof EntityInterface) {
return $entity;
}
}
throw new AccessException(sprintf('%s is not a valid entity route. The LatestRevisionCheck access checker may only be used with a route that has a single entity parameter.', $route_match
->getRouteName()));
}
}
Classes