I'm currently working on a project where certain authenticated users are allowed to submit their own products. The client needs the authors to be able to download their own creations but there's nothing in place for this yet. I've tried using the file entity hooks and hook_file_download hooks but no such luck. Making this feature possible is very easy if it were to be in the module as standard.

Where commerce_file_access is defined, before the rest of the checks for permissions and more, I added:

if (commerce_file_current_user_is_author($file->uid)) {
    return TRUE;
  }

And then just simply defined a new function at the bottom:

function commerce_file_current_user_is_author($fid) {
  global $user;

  if ($fid == $user->uid) {
    return TRUE;
  }
}

The possibility of adding an option in the config page of this module to run this check, would also be quite simple.

The requirements given to me I guess are quite specific and wouldn't usually happen for other users of this module. The module is fantastic otherwise and has saved me a lot of time and effort!

Thank you,

Josh Brown

Comments

joshbrown81 created an issue. See original summary.

drusite’s picture

Hello,
I tried these functions to put into module file and the links stay gray for a product creator. I show them through Views by using (Commerce Product) Commerce Product: Files (Files) (for admin they are downloadable but for the authenticated users they stay not downloadable). Is there a particularity related to Views? I put the first part of the snippet into function commerce_file_access($op, $file, $account = NULL) { ... Is that right?

drusite’s picture

The function commerce_file_can_download($license, $file, $account = NULL) { was also tried. It doesn't work. Maybe it happens because the directory for private files is situated outside of drupal directory.

AlbionBrown’s picture

Hello @drusite,

sorry I've taken so long to get back to you. The way I implemented the check was as below

function commerce_file_access($op, $file, $account = NULL) {

  // CUSTOM - If the logged-in user is the author of the product, allow access
  if (commerce_file_current_user_is_author($file->uid)) {
    return TRUE;
  }

.. rest of commerce_file_access function

Best,

Josh Brown

DamienMcKenna’s picture

Version: 7.x-2.0-beta3 » 7.x-2.x-dev