You are here

function webform_scheduled_tasks_file_download in Webform Scheduled Tasks 8.2

Implements hook_file_download().

File

./webform_scheduled_tasks.module, line 34
Module file.

Code

function webform_scheduled_tasks_file_download($uri) {

  // Only grant access to files saved in the emailed export directory.
  if (strpos($uri, EmailedExport::DESTINATION_DIRECTORY) !== 0) {
    return NULL;
  }

  // Load the file and deny access by default if a single file with the given
  // URI was not found.
  $file_storage = \Drupal::service('entity_type.manager')
    ->getStorage('file');
  $files = $file_storage
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (count($files) !== 1) {
    return -1;
  }
  $file = reset($files);

  // Grant access to the file if the user can administer all submissions. This
  // explicitly doesn't take into account the access of the whole submission,
  // given the exports are whole groups of submissions. The permission is
  // restricted, so should cover a sufficient level of access.
  return \Drupal::currentUser()
    ->hasPermission('administer webform submission') ? file_get_content_headers($file) : -1;
}