You are here

social_user_export.module in Open Social 8.7

The Social User Export module.

File

modules/social_features/social_user_export/social_user_export.module
View source
<?php

/**
 * @file
 * The Social User Export module.
 */
use Drupal\file\Entity\File;

/**
 * Implements hook_file_download().
 */
function social_user_export_file_download($uri) {
  $scheme = \Drupal::service('file_system')
    ->uriScheme($uri);
  $target = file_uri_target($uri);

  // Get the file to see who the owner is.
  $query = \Drupal::entityQuery('file');
  $query
    ->condition('uri', $uri);
  $fid = $query
    ->execute();

  /* @var \Drupal\file\FileInterface $file */
  $file = File::load(reset($fid));
  $access = FALSE;

  // Allow access to users with correct permission or file owner.
  if (\Drupal::currentUser()
    ->hasPermission('administer users') || \Drupal::currentUser()
    ->id() === $file
    ->get('uid')
    ->getString()) {
    $access = TRUE;
  }
  if ($scheme === 'private' && $access && preg_match('/^csv\\/export-(users|enrollments)-([a-f0-9]{12})\\.csv$/i', $target)) {
    return [
      'Content-disposition' => 'attachment; filename="' . basename($target) . '"',
    ];
  }
}

Functions