Hello,
Sometimes you need to have nodes that you want to be readable by everyone and show attachment's existance but the attachments must be available for downloading to pre-selected roles or users only. The attachments must be protected in such a case even from direct request with the URL from a user not having the access privileges.
Drupal has a public and a private way to get and download files but only through private way we can insert restrictions. Multiple solutions can exist to the above situations:
a) An idea is to have a subdirectory with a ".access" file that has roles and/or user ids that are allowed to access files in this directory. This will allow easy management of files with a "filemanager" module.
b) An other idea is to have also a "filename".access file for each file available for downloading with an access restriction based on roles/users. If this files does not exist, it is available to every body.
I am a newbie to Drupal, but any ideas about changes that are needed to implemented are welcome.
Comments
Comment #1
dcoun commentedAny help, which file/module handles the file downloading in private method ?
Comment #2
dcoun commentedI have done some progress. I am not a programmer in real life, so any contribution is needed.
Lets first describe the idea:
We have nodes that are viewable from selected roles or everyone, but we want their file attachments to be visible but only selected roles can download them.
Method:
We use private method so all files that are going to be downloaded must be "validated" before with hook_file_download($file). A database table can be used to keep permissions for each file. A module can do all this job.
Implementation
Lets name our module fileaccesscontrol. We create a database table:
The above is a database table having in mind different ways of setting permissions, not yet implemented.
The following can exist in our module:
The above code gives by default access to the file when no entry exists in the database for the requested file. We need permissions which roles can do this job. So, we add the following:
The problem is now how to fill the database table with the files to whom access is granted only for selected roles.
An idea is to use hook_form_alter and include a checkbox with allowed roles. Can the following code do the job?
The problem is that the above should exist for each attachment in the node's attachments section. How can we have it for each attachment?
Comment #3
dcoun commented