function MediaRevisionAccessCheck::checkAccess

Same name in other branches
  1. 9 core/modules/media/src/Access/MediaRevisionAccessCheck.php \Drupal\media\Access\MediaRevisionAccessCheck::checkAccess()

Checks media item revision access.

Parameters

\Drupal\media\MediaInterface $media: The media item to check.

\Drupal\Core\Session\AccountInterface $account: A user object representing the user for whom the operation is to be performed.

string $op: (optional) The specific operation being checked. Defaults to 'view'.

Return value

bool TRUE if the operation may be performed, FALSE otherwise.

File

core/modules/media/src/Access/MediaRevisionAccessCheck.php, line 92

Class

MediaRevisionAccessCheck
Provides an access checker for media item revisions.

Namespace

Drupal\media\Access

Code

public function checkAccess(MediaInterface $media, AccountInterface $account, $op = 'view') {
    if (!$media || $op !== 'view') {
        // If there was no media to check against, or the $op was not one of the
        // supported ones, we return access denied.
        return FALSE;
    }
    // Statically cache access by revision ID, language code, user account ID,
    // and operation.
    $langcode = $media->language()
        ->getId();
    $cid = $media->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op;
    if (!isset($this->access[$cid])) {
        // Perform basic permission checks first.
        if (!$account->hasPermission('view all media revisions') && !$account->hasPermission('administer media')) {
            $this->access[$cid] = FALSE;
            return FALSE;
        }
        // There should be at least two revisions. If the revision ID of the
        // given media item and the revision ID of the default revision differ,
        // then we already have two different revisions so there is no need for a
        // separate database check.
        if ($media->isDefaultRevision() && $this->countDefaultLanguageRevisions($media) == 1) {
            $this->access[$cid] = FALSE;
        }
        elseif ($account->hasPermission('administer media')) {
            $this->access[$cid] = TRUE;
        }
        else {
            // First check the access to the default revision and finally, if the
            // media passed in is not the default revision then access to that, too.
            $this->access[$cid] = $this->mediaAccess
                ->access($this->mediaStorage
                ->load($media->id()), $op, $account) && ($media->isDefaultRevision() || $this->mediaAccess
                ->access($media, $op, $account));
        }
    }
    return $this->access[$cid];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.