You are here

public static function WebformManagedFileBase::accessFile in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::accessFile()

Check access for a file associated with a webform submission.

Parameters

\Drupal\file\FileInterface $file: A file.

\Drupal\Core\Session\AccountInterface|null $account: A user account.

Return value

bool|null Returns NULL if the file is not attached to a webform submission. Returns FALSE if the user can't access the file. Returns TRUE if the user can access the file.

1 call to WebformManagedFileBase::accessFile()
WebformManagedFileBase::accessFileDownload in src/Plugin/WebformElement/WebformManagedFileBase.php
Control access to webform submission private file downloads.

File

src/Plugin/WebformElement/WebformManagedFileBase.php, line 1311

Class

WebformManagedFileBase
Provides a base class webform 'managed_file' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public static function accessFile(FileInterface $file, AccountInterface $account = NULL) {
  if (empty($file)) {
    return NULL;
  }

  /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
  $file_usage = \Drupal::service('file.usage');
  $usage = $file_usage
    ->listUsage($file);

  // Check for webform submission usage.
  if (!isset($usage['webform']) || !isset($usage['webform']['webform_submission'])) {
    return NULL;
  }

  // Check entity ids.
  $entity_ids = array_keys($usage['webform']['webform_submission']);
  if (empty($entity_ids)) {
    return NULL;
  }

  // Check the first webform submission since files are can only applied to
  // one submission.
  $entity_id = reset($entity_ids);
  $webform_submission = WebformSubmission::load($entity_id);
  if (!$webform_submission) {
    return NULL;
  }
  return $webform_submission
    ->access('view', $account);
}