It would be very helpful to other modules supporting FileField if FileField offered a single permission-checking function taking a $filepath and $user account as arguments and returning 1 (granted), -1 (denied), or 0 (not responsible).

Here's the motivation: modules dealing with files should have a way to check a user's access to an arbitrary file, regardless of whether the file's handled by core upload, FileField, or whatever. hook_file_download() is supposed to do this, but only checks against the current user - it can't take an arbitrary user account. I think the next best solution, until there's an API change, is for modules implementing hook_file_download to implement it like so:

<?php
// Implements hook_file_download().
function filefield_file_download($filepath) {
return filefield_file_download_user($filefield);
}

function filefield_file_download_user($filefield, $account) {
// Move all of the code currently in filefield_file_download() here, and simply pass on $account as necessary
}
?>

This way, other modules still need to hardcode support for file handling methods, but they only need to call one function (i.e. filefield_file_download_user), rather than copying all of the code in that function nearly verbatim.

Here is the motivating issue in Notifications Files: #867462: Improve permissions checking

I'm happy to write the patch for this if you'd accept it.

Comments

quicksketch’s picture

While I think this is a good idea, I'm not sure we should make this change unless you can find a viable alternative in Drupal 7's File module. Right now all the functionality of FileField has an equivalent in Drupal 7, but for this API change I'm not sure that there is such an equivalent in D7, meaning that modules would have no possible upgrade path (other than copy/pasting all the code again in D7).

danepowell’s picture

Shucks... I think you're right, there's no way around this in D7, which also implements hook_file_download() without taking a $user account as an argument. I don't suppose there's any chance of this getting changed in D7 now... I guess I'll put in a request for D8?

I'd appreciate any other suggestions you might have to solve #867462: Improve permissions checking The Drupal Way... (one thing I've considered is temporarily hijacking the current $user to use hook_file_download, but that's almost as ugly as copying / hardcoding permission checking for each file handling module)

danepowell’s picture

Status: Active » Closed (won't fix)