FillPdfAccessHelper.php in FillPDF 8.4
File
src/FillPdfAccessHelper.php
View source
<?php
namespace Drupal\fillpdf;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
class FillPdfAccessHelper implements FillPdfAccessHelperInterface {
protected $linkManipulator;
protected $contextManager;
public function __construct(FillPdfLinkManipulatorInterface $link_manipulator, FillPdfContextManagerInterface $context_manager) {
$this->linkManipulator = $link_manipulator;
$this->contextManager = $context_manager;
}
public function canGeneratePdfFromUrlString($url, AccountInterface $account) {
$context = $this->linkManipulator
->parseUrlString($url);
return $this
->canGeneratePdfFromContext($context, $account);
}
public function canGeneratePdfFromLink(Url $link, AccountInterface $account) {
$context = $this->linkManipulator
->parseLink($link);
return $this
->canGeneratePdfFromContext($context, $account);
}
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);
foreach ($entities as $type_entities) {
foreach ($type_entities as $entity) {
if (!$entity
->access('view', $account)) {
return $cachedForbidden;
}
}
}
return $cachedAllowed;
}
return $cachedForbidden;
}
}