You are here

class FillPdfAccessController in FillPDF 5.0.x

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

Defines a custom access controller for the FillPDF generation route.

Hierarchy

Expanded class hierarchy of FillPdfAccessController

File

src/FillPdfAccessController.php, line 16

Namespace

Drupal\fillpdf
View source
class FillPdfAccessController implements ContainerInjectionInterface {
  use MessengerTrait;
  use LoggerChannelTrait;

  /**
   * The FillPDF access helper.
   *
   * @var \Drupal\fillpdf\FillPdfAccessHelperInterface
   */
  protected $accessHelper;

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

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

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a FillPdfAccessManager object.
   *
   * @param \Drupal\fillpdf\FillPdfAccessHelperInterface $access_helper
   *   The FillPDF access helper.
   * @param \Drupal\fillpdf\FillPdfLinkManipulatorInterface $link_manipulator
   *   The FillPDF link manipulator.
   * @param \Drupal\fillpdf\FillPdfContextManagerInterface $context_manager
   *   The FillPDF context manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(FillPdfAccessHelperInterface $access_helper, FillPdfLinkManipulatorInterface $link_manipulator, FillPdfContextManagerInterface $context_manager, RequestStack $request_stack, AccountInterface $current_user) {
    $this->accessHelper = $access_helper;
    $this->linkManipulator = $link_manipulator;
    $this->contextManager = $context_manager;
    $this->requestStack = $request_stack;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('fillpdf.access_helper'), $container
      ->get('fillpdf.link_manipulator'), $container
      ->get('fillpdf.context_manager'), $container
      ->get('request_stack'), $container
      ->get('current_user'));
  }

  /**
   * Checks whether the current user has access to the current request.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   Access result value object.
   */
  public function checkLink() {
    try {
      $context = $this->linkManipulator
        ->parseRequest($this->requestStack
        ->getCurrentRequest());
    } catch (\InvalidArgumentException $exception) {
      $message = $exception
        ->getMessage();
      $is_admin = $this->currentUser
        ->hasPermission('administer pdfs');
      $this
        ->messenger()
        ->addError($is_admin ? $message : t('An error occurred. Please notify the administrator.'));
      $this
        ->getLogger('fillpdf')
        ->error($message);
      return AccessResult::forbidden();
    }
    $account = $this->currentUser;
    return $this->accessHelper
      ->canGeneratePdfFromContext($context, $account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FillPdfAccessController::$accessHelper protected property The FillPDF access helper.
FillPdfAccessController::$contextManager protected property The FillPDF context manager.
FillPdfAccessController::$currentUser protected property The current user.
FillPdfAccessController::$linkManipulator protected property The FillPDF link manipulator.
FillPdfAccessController::$requestStack protected property The request stack.
FillPdfAccessController::checkLink public function Checks whether the current user has access to the current request.
FillPdfAccessController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FillPdfAccessController::__construct public function Constructs a FillPdfAccessManager object.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.