You are here

public function EntityPrintController::checkAccess in Entity Print 8

Same name and namespace in other branches
  1. 8.2 src/Controller/EntityPrintController.php \Drupal\entity_print\Controller\EntityPrintController::checkAccess()

Validate that the current user has access.

We need to validate that the user is allowed to access this entity also the print version.

Parameters

string $entity_type: The entity type.

int $entity_id: The entity id.

Return value

bool TRUE if they have access otherwise FALSE.

1 string reference to 'EntityPrintController::checkAccess'
entity_print.routing.yml in ./entity_print.routing.yml
entity_print.routing.yml

File

src/Controller/EntityPrintController.php, line 131

Class

EntityPrintController

Namespace

Drupal\entity_print\Controller

Code

public function checkAccess($entity_type, $entity_id) {
  if (empty($entity_id)) {
    return AccessResult::forbidden();
  }
  $account = $this
    ->currentUser();

  // Invalid storage type.
  if (!$this->entityTypeManager
    ->hasHandler($entity_type, 'storage')) {
    return AccessResult::forbidden();
  }

  // Unable to find the entity requested.
  if (!($entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id))) {
    return AccessResult::forbidden();
  }

  // Check if the user has the permission "bypass entity print access".
  $access_result = AccessResult::allowedIfHasPermission($account, 'bypass entity print access');
  if ($access_result
    ->isAllowed()) {
    return $access_result
      ->andIf($entity
      ->access('view', $account, TRUE));
  }

  // Check if the user is allowed to view all bundles of the entity type.
  $access_result = AccessResult::allowedIfHasPermission($account, 'entity print access type ' . $entity_type);
  if ($access_result
    ->isAllowed()) {
    return $access_result
      ->andIf($entity
      ->access('view', $account, TRUE));
  }

  // Check if the user is allowed to view that bundle type.
  $access_result = AccessResult::allowedIfHasPermission($account, 'entity print access bundle ' . $entity
    ->bundle());
  if ($access_result
    ->isAllowed()) {
    return $access_result
      ->andIf($entity
      ->access('view', $account, TRUE));
  }
  return AccessResult::forbidden();
}