I believe that this bug ends here, related to "File_Entity" Module.
(Before i was mislead to thought that the bug was related to "Field Permission" Module, but it seams not to be the case.)

Se even ISSUE at https://www.drupal.org/node/2776087

The choice "Download Link" makes the attached file invisible for authorized user. If I try to copypaste the download link (From user 1 to the other authorized user in another browser) the download access is "Permission denied".

The permission setting is on for the authorized user, on all possible "Download any files" and I have even tried to set up a new "File Type" = Attachment for .zip files. All tests failed... ;( - with MIME TYPE for .zip.)

Only user with permission "Bypass file access control" is able to see or download the files. The bug has been confirmed in a empty Drupal Core 7.50 test with exactly same behave. (with minimum modules activated).

If FORMAT setting for the files Field is set to "Generic file" (Who belongs to the File Module settings) under the "Manage Display" settings (for the content type) the field permission to authorized Roles will work, but it doesn't if setting is "Download Link".

(It seams to be same problem in DP 8, maybe a patch fixed the problem there, have not test it.)

Please, this is a hart function in Drupal, to be able to set up attachments for file download against authorized Roles. Hope this can be fixed soon ;)

- PS - If any interest I can send you my hole site test on empty DP 7.50, with the bug.
- Else is easy to try on any page...;)
My skill is not enough to do something about this bug, else I have tried...

Comments

Göran created an issue. See original summary.

göran’s picture

I have found a "workaround" solution, where I can set the permission, related to Roles (Authorized users) for downloads links.

First Download, install and set up Media Module and Media Library and Media_WYSIWYG (included).

Second, create a new Text field, set the permission for that text field as you want, using the Field Permission Module.

Zip the files you want to upload for download-link for, if special MIMETYPE (in my case the file ends .gbx and there is no Mimetypes for that file type, so I use ZIP instead)
Inside the text field, ad Links through the media-module:s WYSIWYG editor.

This field will only be visible to authorized, and all links inside the field will follow

Not the best solution, but it work meanwhile waiting for a Bug fix

/ sheers

mxr576’s picture

I had the same issue yesterday. In my case, as I can see, the reason of this problem was that all affected files' type was "undefined" in the file_managed table and also mime type of these files wasn't mapped to any pre-defined file types (document, image, video, audio) by file entity module. Because type was "undefined" file_entity_file_entity_access() returned FILE_ENTITY_ACCESS_IGNORE, so file_entity_access() continued with this
if ($op == 'view' && is_object($file)) {}
statement and because the operation was "download", this functioned returned with FALSE in the 1733 line so auth. users could not download files which type was "undefined".

There are workarounds for this problem of course. You can ensure that the type of files is one from the pre-defined ones programmatically (ex.: in hook_file_presave()) or you can define new file types on the UI (admin/structure/file-types/add) and add mime types of your files to this new file type. After that you just need to ensure that current user roles has right to "Download {any,own} XY files" on the permission page.
Other solution could be is implementing you own file access logic by implementing hook_file_entity_access() in a custom module.

But I'm wondering, would not be better if we would modify the file_entity_access() function to handle not only the view operation but also the download operation as well? We could use the same logic here, which would mean that we need to introduce new permissions like:

  • Download private files
  • Download own private files
  • Download own files
  • Download files
spesic’s picture

That's a great writeup @mxr576

I agree that the permanent solution should be configuring download action permissions.
In the meantime, I like the hook_file_entity_access route and use view action to determine whether the user should be able to download the file.
I cannot think of a scenario of where I'd allow a user to "view" but not "download" the file (not to say there aren't any).

Something like the following should work:

/**
 * Implements of hook_file_entity_access().
 * This is a workaround until download action gets its own permission set.
 */
function your_custom_module_file_entity_access($op, $file, $account) {
  // Check to see if the user has access to view the file. If they do, let them download it.
  if ($op == 'download' && file_entity_access('view', $file, $account)) {
    return FILE_ENTITY_ACCESS_ALLOW;
  }

  // Doesn't allow or deny access, it allows original file_entity_access to finish execution and make a decision.
  return FILE_ENTITY_ACCESS_IGNORE;
}
Tourista’s picture

Any news about this bug ? I have tons of files my users can't download because files are .zip :(

thmohd’s picture

Go to Structure -> File types -> Document
Append in the Mimetypes field the following:

application/zip

ey’s picture

Version: 7.x-2.0-beta3 » 7.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new485 bytes

Thanks @mxr576 and @spesic.

I've created a patch, which not only checks for $op == 'view' but also for $op == 'download' in file_entity_access() function.

Please review the patch, my first local tests on a project were positive, but we're also testing further.

Status: Needs review » Needs work

The last submitted patch, 7: download-link-permissions-dont-work-2781275-7.patch, failed testing.

ey’s picture

Status: Needs work » Needs review
StatusFileSize
new405 bytes

Another try. Call file_entity_access() function itself as with $op = 'view' as fall back for $op = 'download'

Please review.

Status: Needs review » Needs work

The last submitted patch, 9: download-link-permissions-dont-work-2781275-9.patch, failed testing.

ey’s picture

I cannot think of a scenario of where I'd allow a user to "view" but not "download" the file (not to say there aren't any).

@spesic, I'm totally agree with you on that. I don't know why the tests are failing, but I guess simply changing the order of tests would solve this issue. I took a short look at the tests and see that the test user gets 'view files' permission before the tests for download.

So with that patch checking the view permissions as well, the test user can download the file even if it doesn't have neither 'download own document files' nor 'download any document files'.

I'll try to create a new patch with the tests re-ordered so that tests for download permissions come before the tests for view permissions.

ey’s picture

Next attempt with re-ordered tests.

ey’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 12: download-link-permissions-dont-work-2781275-12.patch, failed testing.

priyanka1989’s picture

Hi All,

I am facing this issue again on Drupal 8. Please help.

joseph.olstad’s picture

new patch

knock on wood, this should pass tests.

added one line:

$file->status = FILE_STATUS_PERMANENT;

just the line before file_save() in the test

joseph.olstad’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: download-link-permissions-dont-work-2781275-16.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joseph.olstad’s picture

Status: Needs work » Closed (cannot reproduce)

had a closer look at the patch, it makes no sense.

I haven't actually noticed this issue yet, likely outdated.

joseph.olstad’s picture

Status: Closed (cannot reproduce) » Needs work

Might be worthwhile taking a closer look again.

marameodesign’s picture

Hi all,

I can confirm this issue is still happening. I have an Entity file and even though anonymous have permission to see files, if I setup the "Downlod Link" field formatter from DS, only admin is able to see the output and download the file.

Copy the URL of the button, and pasting it in an incognito window, gives the access denied.

I have also tried to add PDF as a Mime type in its own, still doesn't work.

agileadam’s picture

I just had to deal with this on a Drupal 7 site.
I needed the public file to be available to anyone.
So, I made quick and dirty DS field that gets the job done.

/**
 * Implements hook_ds_fields_info().
 */
function mymodule_ds_fields_info($entity_type) {
  $fields = [];

  // Other fields here on the real site...

  $fields['node']['census_excel_as_download_link'] = [
    'title' => t('Excel file as download link (mymodule.module)'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function' => 'mymodule_ds_field_census_excel_as_download_link',
    'ui_limit' => ['data_census|full'],
  ];

  if (isset($fields[$entity_type])) {
    return [$entity_type => $fields[$entity_type]];
  }

  return [];
}

/**
 * Callback for 'Census Data excel file as download link' field.
 */
function mymodule_ds_field_census_excel_as_download_link($field) {
  $markup = '';
  if (!empty($field['entity']->field_data_file_upload[LANGUAGE_NONE][0]['uri'])) {
    $file_uri = $field['entity']->field_data_file_upload[LANGUAGE_NONE][0]['uri'];
    $file_url = file_create_url($file_uri);
    $markup = l(t('Download Table in Excel Format'), $file_url);
  }
  return $markup;
}
andyg5000’s picture

I thought I needed this patch, but it was related to the mime type as others have mentioned. After adding the mime-type for my new files (application/octet-stream) I then had to go update the records in `file_managed` table to have the correct type of `document`. They were `undefined` since the mime type wasn't associated with the file type at upload time. Not saying that's the fix for this issue (maybe), but hopefully that helps others!

joseph.olstad’s picture

Component: Code » Documentation
Category: Bug report » Support request

This looks more like a support request and a documentation issue.

Solution is to correctly configure your mime types as discussed in comment #23
Other use cases/scenarios may want to have a look at the solution discussed in comment #24

I haven't yet added any documentation for this but would be willing to review a patch that updates the README.txt file.