You are here

public function MediaLibraryUiBuilder::checkAccess in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::checkAccess()
  2. 10 core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::checkAccess()

Check access to the media library.

Parameters

\Drupal\Core\Session\AccountInterface $account: Run access checks for this account.

\Drupal\media_library\MediaLibraryState $state: (optional) The current state of the media library, derived from the current request.

Return value

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

File

core/modules/media_library/src/MediaLibraryUiBuilder.php, line 181

Class

MediaLibraryUiBuilder
Service which builds the media library.

Namespace

Drupal\media_library

Code

public function checkAccess(AccountInterface $account, MediaLibraryState $state = NULL) {
  if (!$state) {
    try {
      $state = MediaLibraryState::fromRequest($this->request);
    } catch (BadRequestHttpException $e) {
      return AccessResult::forbidden($e
        ->getMessage());
    } catch (\InvalidArgumentException $e) {
      return AccessResult::forbidden($e
        ->getMessage());
    }
  }

  // Deny access if the view or display are removed.
  $view = $this->entityTypeManager
    ->getStorage('view')
    ->load('media_library');
  if (!$view) {
    return AccessResult::forbidden('The media library view does not exist.')
      ->setCacheMaxAge(0);
  }
  if (!$view
    ->getDisplay('widget')) {
    return AccessResult::forbidden('The media library widget display does not exist.')
      ->addCacheableDependency($view);
  }

  // The user must at least be able to view media in order to access the media
  // library.
  $can_view_media = AccessResult::allowedIfHasPermission($account, 'view media')
    ->addCacheableDependency($view);

  // Delegate any further access checking to the opener service nominated by
  // the media library state.
  return $this->openerResolver
    ->get($state)
    ->checkAccess($state, $account)
    ->andIf($can_view_media);
}