You are here

public function LicenseFileManager::canDownload in Commerce File 8.2

Gets whether the licensed file can be downloaded.

The logic first checks whether the current user has the permission to bypass the license control or administer licenses, and then check the download limits.

Parameters

\Drupal\commerce_license\Entity\LicenseInterface $license: The license entity.

\Drupal\file\FileInterface $file: The file entity.

\Drupal\Core\Session\AccountInterface|null $account: The user to check for. When omitted, the license owner is used instead.

Return value

bool Whether the given licensed file can be downloaded by the current user.

Overrides LicenseFileManagerInterface::canDownload

File

src/LicenseFileManager.php, line 81

Class

LicenseFileManager
Provides a service for managing licensed files.

Namespace

Drupal\commerce_file

Code

public function canDownload(LicenseInterface $license, FileInterface $file, AccountInterface $account = NULL) {
  $account = $account ?: $license
    ->getOwner();

  // When the current user has the permission to bypass license control or
  // administer licenses, the file can be downloaded.
  if ($account
    ->hasPermission('bypass license control') || $account
    ->hasPermission('administer commerce_license')) {
    return TRUE;
  }

  // If the current account cannot access the license, or if the license
  // is not active, do not allow the download.
  if (!$license
    ->access('view', $account) || $license
    ->getState()
    ->getId() !== 'active') {
    return FALSE;
  }

  // Now, check if a download limit is configured, either globally or for the
  // product variation referenced by the license.
  $download_limit = $this
    ->getDownloadLimit($license);

  // If no download limit is configured, allow the download.
  if (!$download_limit) {
    return TRUE;
  }
  $counts = $this->downloadLogger
    ->getDownloadCounts($license);
  if (isset($counts[$file
    ->id()]) && $counts[$file
    ->id()] >= $download_limit) {
    return FALSE;
  }
  return TRUE;
}