You are here

function webform_file_access in Webform 6.x

Same name and namespace in other branches
  1. 8.5 webform.module \webform_file_access()

Implements hook_file_access().

See also

file_file_download()

webform_preprocess_file_link()

File

./webform.module, line 760
Enables the creation of webforms and questionnaires.

Code

function webform_file_access(FileInterface $file, $operation, AccountInterface $account) {
  $is_webform_download = $operation === 'download' && strpos($file
    ->getFileUri(), 'private://webform/') === 0;

  // Block access to temporary anonymous private file uploads
  // only when an anonymous user is attempting to download the file.
  // Links to anonymous file uploads are automatically suppressed.
  // @see webform_preprocess_file_link()
  // @see webform_file_download()
  if ($is_webform_download && $file
    ->isTemporary() && $file
    ->getOwner() && $file
    ->getOwner()
    ->isAnonymous() && \Drupal::routeMatch()
    ->getRouteName() === 'system.files') {
    return AccessResult::forbidden();
  }

  // Allow access to files associated with a webform submission.
  // This prevent uploaded webform files from being lost when another user
  // edits a submission with multiple file uploads.
  // @see \Drupal\file\Element\ManagedFile::valueCallback
  if ($is_webform_download && ManagedFile::accessFile($file, $account)) {
    return AccessResult::allowed();
  }
  return AccessResult::neutral();
}