You are here

public function FillPdfAccessHelper::canGeneratePdfFromContext in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/FillPdfAccessHelper.php \Drupal\fillpdf\FillPdfAccessHelper::canGeneratePdfFromContext()

This is the main access checking function of this class.

Method self::canGeneratePdfFromLinkUrl() should delegate to this one.

Parameters

array $context: As returned by FillPdfLinkManipulator's parse functions.

\Drupal\Core\Session\AccountInterface $account: The user whose access is being checked.

Return value

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

Overrides FillPdfAccessHelperInterface::canGeneratePdfFromContext

See also

\Drupal\fillpdf\FillPdfAccessHelperInterface::canGeneratePdfFromLink()

2 calls to FillPdfAccessHelper::canGeneratePdfFromContext()
FillPdfAccessHelper::canGeneratePdfFromLink in src/FillPdfAccessHelper.php
Provides a way to check access from a link argument.
FillPdfAccessHelper::canGeneratePdfFromUrlString in src/FillPdfAccessHelper.php
Provides a way to pass in a FillPDF Link string to check access.

File

src/FillPdfAccessHelper.php, line 62

Class

FillPdfAccessHelper
Class FillPdfAccessHelper.

Namespace

Drupal\fillpdf

Code

public function canGeneratePdfFromContext(array $context, AccountInterface $account) {
  $is_admin = $account
    ->hasPermission('administer pdfs');
  $can_publish_all = $account
    ->hasPermission('publish all pdfs');
  $cachedAllowed = AccessResult::allowed()
    ->cachePerUser()
    ->cachePerPermissions();
  if ($can_publish_all || $is_admin) {
    return $cachedAllowed;
  }
  $is_sample = $context['sample'];
  if ($is_sample && $is_admin) {
    return $cachedAllowed;
  }
  $cachedForbidden = AccessResult::forbidden()
    ->cachePerUser()
    ->cachePerPermissions();
  $can_publish = $account
    ->hasPermission('publish own pdfs');
  if (!$is_sample && $can_publish) {
    $entities = $this->contextManager
      ->loadEntities($context);

    /** @var \Drupal\Core\Entity\EntityInterface[] $type_entities */
    foreach ($entities as $type_entities) {
      foreach ($type_entities as $entity) {

        // If there are any entities in the context that the user can't view,
        // deny access.
        if (!$entity
          ->access('view', $account)) {
          return $cachedForbidden;
        }
      }
    }
    return $cachedAllowed;
  }
  return $cachedForbidden;
}