You are here

function webform_entity_print_webform_submission_access in Webform 6.x

Implements hook_webform_submission_access().

File

modules/webform_entity_print/webform_entity_print.module, line 29
Provides Entity Print (PDF) integration.

Code

function webform_entity_print_webform_submission_access(WebformSubmissionInterface $webform_submission, $operation, AccountInterface $account) {
  if ($operation !== 'view') {
    return AccessResult::neutral();
  }

  // Only override access controls when displaying images.
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if (!in_array($route_name, [
    'system.files',
    'image.style_private',
  ])) {
    return AccessResult::neutral();
  }

  // Make sure the webform entity print token is defined.
  $webform_entity_print_token = \Drupal::request()->query
    ->get(WEBFORM_ENTITY_PRINT_IMAGE_TOKEN);
  if (!$webform_entity_print_token) {
    return AccessResult::neutral();
  }

  // Make sure the URI contains /{webform}/{sid}/.
  $uri = \Drupal::request()
    ->getUri();
  $webform_id = $webform_submission
    ->getWebform()
    ->id();
  $sid = $webform_submission
    ->id();
  if (!preg_match('#(?:/private|/system/files)/webform/' . $webform_id . '/' . $sid . '#', parse_url($uri, PHP_URL_PATH))) {
    return AccessResult::neutral();
  }
  $encrypt_token = _webform_entity_print_token_generate($uri);
  return AccessResult::allowedIf($webform_entity_print_token === $encrypt_token);
}