You are here

protected function MediaAccessController::checkAccess in Media entity 8

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandler::checkAccess

File

src/MediaAccessController.php, line 18

Class

MediaAccessController
Defines an access controller for the media entity.

Namespace

Drupal\media_entity

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($account
    ->hasPermission('administer media')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  $is_owner = $account
    ->id() && $account
    ->id() == $entity
    ->getPublisherId() ? TRUE : FALSE;
  switch ($operation) {
    case 'view':
      return AccessResult::allowedIf($account
        ->hasPermission('view media') && $entity->status->value);
    case 'update':
      return AccessResult::allowedIf($account
        ->hasPermission('update media') && $is_owner || $account
        ->hasPermission('update any media'))
        ->cachePerPermissions()
        ->cachePerUser()
        ->addCacheableDependency($entity);
    case 'delete':
      return AccessResult::allowedIf($account
        ->hasPermission('delete media') && $is_owner || $account
        ->hasPermission('delete any media'))
        ->cachePerPermissions()
        ->cachePerUser()
        ->addCacheableDependency($entity);
  }

  // No opinion.
  return AccessResult::neutral()
    ->cachePerPermissions();
}