I want to set permissions for uploaded files in a way that only authenticated users (or users with any particular role) can access/download. I don't want to just hide the files but want to deny access completely. So if someone has the direct file URL, he should be logged in to access it.
Private files folder: I managed to set it up so that only admin user can access the files, but for authenticated users if access denied.
Comments
Comment #2
catchThis is a support request I think, but leaving as a bug report for now.
If files are referenced by an entity that an authenticated user has access to, then they'll be accessible to authenticated users. Can you confirm whether you're uploading the files via core's file field or via some other method?
Comment #3
bhanojeerao commentedThanks for the reply catch.
I am using core's file field for uploading the files.
I have a custom content type, i created a file field for this content type in manage fields section.
I added content under this content type, each content having its uploaded file (text, document, PPT etc). When i view this content in the front end then i can see uploaded file.
So anonymous user should not have access to this file. I want the same logic for entire website where i have file concept.
Comment #4
Opinder Thakur commentedUsing Module Field Permissions, Site administrators able to set field-level permissions to edit, view and create fields on any entity according to the specific roles.
Using this module we can set the permission of download file for authenticated or anonymous users.
Comment #5
bhanojeerao commentedI already checked this module Field Permissions (https://www.drupal.org/project/field_permissions).
This is hiding the fields from users when i set permission. As i mentioned in question, I don't want to just hide file/field but want to denied access completely when user clicks on the link.If we hide fields then user can not come to know that there are some fields for authenticated users.
Please help me on this.
Comment #6
bhanojeerao commentedAny help on this ?
Comment #7
chiranjeeb2410 commentedComment #9
zanvidmar commentedI have similar problem and I solve it like this:
I created custom module which ads cookie (user_cookie_save()) based on user role on log in (hook_user_login()) and delete it on log out (hook_user_logout()). I created "private" folder with .htaccess that allow access based on this cookie value.
This means that private files must be in this folder - separated form other files.
In my case I have only 2 options for users (can see, can not see file) but if you have multilevel permissions this solution probably won't work.
I know that this is not super secure technique, but in my case it is enough to stop bots and regular user to access this files.
If you are interested, I can share my code.
Comment #10
cilefen commentedDoes hook_file_download not work for the use case in the original post?
Comment #11
zanvidmar commentedI did not successfully find a solution to use hook_file_download if someone has the direct file URL - it seems that Drupal is not included in file rendering process at all.
From Drupal Docs (hook_file_download)
"This hook allows modules to enforce permissions on file downloads whenever Drupal is handling file download, as opposed to the web server bypassing Drupal and returning the file from a public directory."
So how can we force Drupal to handle file display/download if user has direct URL?
Comment #12
cilefen commentedDo you mean URLs to the public file system?
Comment #13
zanvidmar commentedYes, for example: "www.example.com/sites/default/files/2017-11/example.pdf"
Comment #14
cilefen commented@zanvidmar I see. The subject of this issue is the private file system. The public system is, well... public. I suggest opening a forums thread to discuss.
Comment #15
seirerman commentedDid you check out the content access module (https://www.drupal.org/project/content_access)?
I use it for content types with private file fields, an depending on the permission I set for the content type (or even a specific node) anonymous users can either see the node (and the attached file!) or not.
If not, they get an "access denied" error for both the node and the file by drupal.
Comment #17
alx_benjamin commentedTry this:
Comment #18
kumkum29 commented@alx_benjamin
I search to create a custom access to the files of the site (depending by roles). Your code is an interesting base code for entity access.
In your opinion can I use this code for the files?
Thanks for your help.
Comment #25
josedsilvaThanks @alx_benjamin #17 worked.
#18 @kumkum29, yes you can use it with files. $entity->bundle() == 'file'
Comment #27
cilefen commentedIt looks like there is an answer.