diff --git a/commerce_file.module b/commerce_file.module index eb7748ef42f301fef0497c57e2356fd1b29a806d..463027e67905f90e7311881984a284af516714f8 100644 --- a/commerce_file.module +++ b/commerce_file.module @@ -137,6 +137,36 @@ function commerce_file_file_download_headers_alter(array &$headers, $file) { } } +/** + * Implements hook_permission(). + */ +function commerce_file_permission() { + return array( + 'bypass license control' => array( + 'title' => t('Bypass license control'), + 'description' => t('Download files without having a license.'), + 'restrict access' => TRUE, + ), + ); +} + +/** + * Checks whether the provided user is allowed to bypass license control. + * + * This allows the user to download a file without having a license. + * Since there's no license, download limits can't be checked and respected. + * + * @param $account + * The account to check for. If not given, the current user is used instead. + * + * @return + * TRUE if the account is allowed to bypass license control, FALSE + * otherwise. + */ +function commerce_file_bypass_license_control($account = NULL) { + return user_access('bypass license control', $account) || user_access('administer licenses', $account); +} + /** * Implements hook_file_entity_access(). */ @@ -175,7 +205,7 @@ function commerce_file_file_download($uri) { } // If the file is licensable, don't allow it to be downloaded this way. - if (commerce_file_is_licensable($file)) { + if (commerce_file_is_licensable($file) && !commerce_file_bypass_license_control()) { return -1; } } @@ -211,8 +241,8 @@ function commerce_file_access($op, $file, $account = NULL) { if (!empty($_SESSION['commerce_license_uid']) && empty($account->uid)) { $account = user_load($_SESSION['commerce_license_uid']); } - // No need to check access for admin. - if (user_access('administer licenses', $account)) { + // No need to check access. + if (commerce_file_bypass_license_control($account)) { return TRUE; } // This file is not licensable. @@ -385,8 +415,8 @@ function commerce_file_can_download($license, $file, $account = NULL) { if (!empty($_SESSION['commerce_license_uid']) && empty($account->uid)) { $account = user_load($_SESSION['commerce_license_uid']); } - // The admin can always download. - if (user_access('administer licenses', $account)) { + // No need to check access. + if (commerce_file_bypass_license_control($account)) { return TRUE; } // This is not this user's license, and he has no admin rights. @@ -700,8 +730,7 @@ function commerce_file_field_formatter_view($entity_type, $entity, $field, $inst $license = commerce_file_get_product_license($entity); $access = TRUE; if (!empty($settings['check_access'])) { - // The user must either have admin privileges, or have a license. - $access = (user_access('administer licenses') || $license); + $access = ($license || commerce_file_bypass_license_control()); } foreach ($items as $delta => $item) { @@ -745,8 +774,10 @@ function theme_commerce_file_download_link($variables) { $icon = $variables['icon'] . ' '; } - $can_download = FALSE; - if (!empty($variables['license'])) { + if (empty($variables['license'])) { + $can_download = commerce_file_bypass_license_control(); + } + else { // The file that this theme function receives is just the filefield item // array converted to an object (yay, D7!). The limit download check needs // the actual file entity.