Chris@0: mediaStorage = $entity_type_manager->getStorage('media'); Chris@0: $this->mediaAccess = $entity_type_manager->getAccessControlHandler('media'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks routing access for the media item revision. Chris@0: * Chris@0: * @param \Symfony\Component\Routing\Route $route Chris@0: * The route to check against. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The currently logged in account. Chris@0: * @param int $media_revision Chris@0: * (optional) The media item revision ID. If not specified, but $media is, Chris@0: * access is checked for that object's revision. Chris@0: * @param \Drupal\media\MediaInterface $media Chris@0: * (optional) A media item. Used for checking access to a media items Chris@0: * default revision when $media_revision is unspecified. Ignored when Chris@0: * $media_revision is specified. If neither $media_revision nor $media are Chris@0: * specified, then access is denied. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * The access result. Chris@0: */ Chris@0: public function access(Route $route, AccountInterface $account, $media_revision = NULL, MediaInterface $media = NULL) { Chris@0: if ($media_revision) { Chris@0: $media = $this->mediaStorage->loadRevision($media_revision); Chris@0: } Chris@0: $operation = $route->getRequirement('_access_media_revision'); Chris@0: return AccessResult::allowedIf($media && $this->checkAccess($media, $account, $operation))->cachePerPermissions()->addCacheableDependency($media); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks media item revision access. Chris@0: * Chris@0: * @param \Drupal\media\MediaInterface $media Chris@0: * The media item to check. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * A user object representing the user for whom the operation is to be Chris@0: * performed. Chris@0: * @param string $op Chris@0: * (optional) The specific operation being checked. Defaults to 'view'. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the operation may be performed, FALSE otherwise. Chris@0: */ Chris@0: public function checkAccess(MediaInterface $media, AccountInterface $account, $op = 'view') { Chris@0: if (!$media || $op !== 'view') { Chris@0: // If there was no media to check against, or the $op was not one of the Chris@0: // supported ones, we return access denied. Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Statically cache access by revision ID, language code, user account ID, Chris@0: // and operation. Chris@0: $langcode = $media->language()->getId(); Chris@0: $cid = $media->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op; Chris@0: Chris@0: if (!isset($this->access[$cid])) { Chris@0: // Perform basic permission checks first. Chris@0: if (!$account->hasPermission('view all media revisions') && !$account->hasPermission('administer media')) { Chris@0: $this->access[$cid] = FALSE; Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // There should be at least two revisions. If the revision ID of the Chris@0: // given media item and the revision ID of the default revision differ, Chris@0: // then we already have two different revisions so there is no need for a Chris@0: // separate database check. Chris@0: if ($media->isDefaultRevision() && ($this->countDefaultLanguageRevisions($media) == 1)) { Chris@0: $this->access[$cid] = FALSE; Chris@0: } Chris@0: elseif ($account->hasPermission('administer media')) { Chris@0: $this->access[$cid] = TRUE; Chris@0: } Chris@0: else { Chris@0: // First check the access to the default revision and finally, if the Chris@0: // media passed in is not the default revision then access to that, too. Chris@0: $this->access[$cid] = $this->mediaAccess->access($this->mediaStorage->load($media->id()), $op, $account) && ($media->isDefaultRevision() || $this->mediaAccess->access($media, $op, $account)); Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->access[$cid]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Counts the number of revisions in the default language. Chris@0: * Chris@0: * @param \Drupal\media\MediaInterface $media Chris@0: * The media item for which to to count the revisions. Chris@0: * Chris@0: * @return int Chris@0: * The number of revisions in the default language. Chris@0: */ Chris@0: protected function countDefaultLanguageRevisions(MediaInterface $media) { Chris@0: $entity_type = $media->getEntityType(); Chris@0: $count = $this->mediaStorage->getQuery() Chris@0: ->allRevisions() Chris@0: ->condition($entity_type->getKey('id'), $media->id()) Chris@0: ->condition($entity_type->getKey('default_langcode'), 1) Chris@0: ->count() Chris@0: ->execute(); Chris@0: return $count; Chris@0: } Chris@0: Chris@0: }