0

I have developed a website in Drupal 7. Right now I have a requirement: A page should display a PDF file (already generated). Only authenticated users should have access to view or download this file, but anyone with direct access to this static file link can view it and download it.

Is there any method I can use to prevent this and redirect them to the login page? What kind of logical programming needs to be applied in this case?

3
  • 1
    You could use the private file system to store the file, and an approach like the R4032login module for the redirect
    – Clive
    May 11, 2021 at 11:43
  • can you please help me understand in more detail ?
    – mayur3868
    May 11, 2021 at 13:38
  • Here's a good start File system settings.
    – prkos
    May 11, 2021 at 16:43

1 Answer 1

0

As @Clive and @prkos mentioned in comments above, Drupal has different stream wrappers for public and private file systems:

https://www.drupal.org/node/15368

I know you are using Drupal 7, but the Drupal 8 docs on the topic mention:

In Drupal 8, you can no longer set private file system via UI. You set it in your settings.php file.

So you might find it convenient that you can configure this in the Drupal 7 admin UI ( "Private file system path" under admin/config/media/file-system) without any edits to your site's codebase.

If you have configured the private stream wrapper correctly, you can prevent anonymous users from downloading those files and instead respond to their attempts with an HTTP error 403.

The remaining desired functionality is provided by the community-contributed module that @Clive mentioned:

https://www.drupal.org/project/r4032login

The module is available for Drupal 7, 8, and 9. Its project page will explain its usage, but basically it redirects users to the login page when they ask to access a restricted resource, and redirects them to their intended destination once they log in successfully.

2
  • 1
    your guidance worked. Thank you. Thing I noticed is if I create private folder inside sites/default/public/private, it never gonna work. Also I used anonymous_login ( drupal.org/project/anonymous_login ) which worked perfectly. Thank You !
    – mayur3868
    May 25, 2021 at 4:16
  • Glad to hear that my answer was helpful! Please accept it by clicking on the green check mark. Otherwise, this answered question will continue to show up in lists of unanswered questions. May 25, 2021 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.