annotate core/modules/content_moderation/src/Access/LatestRevisionCheck.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\content_moderation\Access;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessException;
Chris@0 6 use Drupal\Core\Access\AccessResult;
Chris@0 7 use Drupal\Core\Entity\EntityInterface;
Chris@0 8 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10 use Drupal\content_moderation\ModerationInformationInterface;
Chris@0 11 use Drupal\Core\Session\AccountInterface;
Chris@0 12 use Drupal\user\EntityOwnerInterface;
Chris@0 13 use Symfony\Component\Routing\Route;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Access check for the entity moderation tab.
Chris@0 17 */
Chris@0 18 class LatestRevisionCheck implements AccessInterface {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * The moderation information service.
Chris@0 22 *
Chris@0 23 * @var \Drupal\content_moderation\ModerationInformationInterface
Chris@0 24 */
Chris@0 25 protected $moderationInfo;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Constructs a new LatestRevisionCheck.
Chris@0 29 *
Chris@0 30 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
Chris@0 31 * The moderation information service.
Chris@0 32 */
Chris@0 33 public function __construct(ModerationInformationInterface $moderation_information) {
Chris@0 34 $this->moderationInfo = $moderation_information;
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Checks that there is a pending revision available.
Chris@0 39 *
Chris@0 40 * This checker assumes the presence of an '_entity_access' requirement key
Chris@0 41 * in the same form as used by EntityAccessCheck.
Chris@0 42 *
Chris@0 43 * @param \Symfony\Component\Routing\Route $route
Chris@0 44 * The route to check against.
Chris@0 45 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 46 * The parametrized route.
Chris@0 47 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 48 * The current user account.
Chris@0 49 *
Chris@0 50 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 51 * The access result.
Chris@0 52 *
Chris@0 53 * @see \Drupal\Core\Entity\EntityAccessCheck
Chris@0 54 */
Chris@0 55 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
Chris@0 56 // This tab should not show up unless there's a reason to show it.
Chris@0 57 $entity = $this->loadEntity($route, $route_match);
Chris@0 58 if ($this->moderationInfo->hasPendingRevision($entity)) {
Chris@0 59 // Check the global permissions first.
Chris@0 60 $access_result = AccessResult::allowedIfHasPermissions($account, ['view latest version', 'view any unpublished content']);
Chris@0 61 if (!$access_result->isAllowed()) {
Chris@0 62 // Check entity owner access.
Chris@0 63 $owner_access = AccessResult::allowedIfHasPermissions($account, ['view latest version', 'view own unpublished content']);
Chris@0 64 $owner_access = $owner_access->andIf((AccessResult::allowedIf($entity instanceof EntityOwnerInterface && ($entity->getOwnerId() == $account->id()))));
Chris@0 65 $access_result = $access_result->orIf($owner_access);
Chris@0 66 }
Chris@0 67
Chris@0 68 return $access_result->addCacheableDependency($entity);
Chris@0 69 }
Chris@0 70
Chris@17 71 return AccessResult::forbidden('No pending revision for moderated entity.')->addCacheableDependency($entity);
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Returns the default revision of the entity this route is for.
Chris@0 76 *
Chris@0 77 * @param \Symfony\Component\Routing\Route $route
Chris@0 78 * The route to check against.
Chris@0 79 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 80 * The parametrized route.
Chris@0 81 *
Chris@0 82 * @return \Drupal\Core\Entity\ContentEntityInterface
Chris@0 83 * returns the Entity in question.
Chris@0 84 *
Chris@0 85 * @throws \Drupal\Core\Access\AccessException
Chris@0 86 * An AccessException is thrown if the entity couldn't be loaded.
Chris@0 87 */
Chris@0 88 protected function loadEntity(Route $route, RouteMatchInterface $route_match) {
Chris@0 89 $entity_type = $route->getOption('_content_moderation_entity_type');
Chris@0 90
Chris@0 91 if ($entity = $route_match->getParameter($entity_type)) {
Chris@0 92 if ($entity instanceof EntityInterface) {
Chris@0 93 return $entity;
Chris@0 94 }
Chris@0 95 }
Chris@0 96 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()));
Chris@0 97 }
Chris@0 98
Chris@0 99 }