You are here

class FillPdfAccessHelper in FillPDF 5.0.x

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

Class FillPdfAccessHelper.

@package Drupal\fillpdf

Hierarchy

Expanded class hierarchy of FillPdfAccessHelper

1 string reference to 'FillPdfAccessHelper'
fillpdf.services.yml in ./fillpdf.services.yml
fillpdf.services.yml
1 service uses FillPdfAccessHelper
fillpdf.access_helper in ./fillpdf.services.yml
Drupal\fillpdf\FillPdfAccessHelper

File

src/FillPdfAccessHelper.php, line 14

Namespace

Drupal\fillpdf
View source
class FillPdfAccessHelper implements FillPdfAccessHelperInterface {

  /**
   * The FillPDF link manipulator.
   *
   * @var \Drupal\fillpdf\Service\FillPdfLinkManipulator
   */
  protected $linkManipulator;

  /**
   * The FillPDF context manager.
   *
   * @var \Drupal\fillpdf\Service\FillPdfContextManager
   */
  protected $contextManager;

  /**
   * Constructs a FillPdfAccessManager object.
   *
   * @param \Drupal\fillpdf\FillPdfLinkManipulatorInterface $link_manipulator
   *   The FillPDF link manipulator.
   * @param \Drupal\fillpdf\FillPdfContextManagerInterface $context_manager
   *   The FillPDF context manager.
   */
  public function __construct(FillPdfLinkManipulatorInterface $link_manipulator, FillPdfContextManagerInterface $context_manager) {
    $this->linkManipulator = $link_manipulator;
    $this->contextManager = $context_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function canGeneratePdfFromUrlString($url, AccountInterface $account) {
    $context = $this->linkManipulator
      ->parseUrlString($url);
    return $this
      ->canGeneratePdfFromContext($context, $account);
  }

  /**
   * {@inheritdoc}
   */
  public function canGeneratePdfFromLink(Url $link, AccountInterface $account) {
    $context = $this->linkManipulator
      ->parseLink($link);
    return $this
      ->canGeneratePdfFromContext($context, $account);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FillPdfAccessHelper::$contextManager protected property The FillPDF context manager.
FillPdfAccessHelper::$linkManipulator protected property The FillPDF link manipulator.
FillPdfAccessHelper::canGeneratePdfFromContext public function This is the main access checking function of this class. Overrides FillPdfAccessHelperInterface::canGeneratePdfFromContext
FillPdfAccessHelper::canGeneratePdfFromLink public function Provides a way to check access from a link argument. Overrides FillPdfAccessHelperInterface::canGeneratePdfFromLink
FillPdfAccessHelper::canGeneratePdfFromUrlString public function Provides a way to pass in a FillPDF Link string to check access. Overrides FillPdfAccessHelperInterface::canGeneratePdfFromUrlString
FillPdfAccessHelper::__construct public function Constructs a FillPdfAccessManager object.