2

I have one file placed in my module. I want that only authenticated users or admin can access the content of this file. For anonymous users, it should be shown 'You don't have permission to access...'

Link is like <a href = "' . $base_url . '/' . drupal_get_path('module', 'my_module') . '/files/myfile.xml" target="_blank">Sample File</a>'

Right now issue is, even if i logged out, I am able to see the content of this link.

I tried this:

if(isset($user->roles[1])) {
        global $base_url;
        return $mylink;
      }

but above code is not working for me.

What is the best possible way to achieve this?

3

If I've understood your requirement correctly, you've some files other than normal Drupal files, like csv, txt which should not be accessible to Anonymous user.

This should be done at .htaccess level or you can give a try to private file system in Drupal Core. Check the ans given to "how to restrict file access to specific user roles" for more details on Private File system. The question is related to Drupal 7 because of which contributed modules will not be available for Drupal 8. But you can check configuration options mentioned for Drupal 8 Core.

7
  • No, none of the above approach is working. Still I can access the link from anonymous user. The path(my link) starts from domain/modules/mymodule/file... .Can't we put any limit that such paths starting with '/Drupal/modules/` should be unassailable for anonymous users? Nov 14 '16 at 10:41
  • Ohh... So you've some files other than normal Drupal files, like csv, txt which should not be accessible to Anonymous user, correct?
    – Yogesh
    Nov 14 '16 at 10:56
  • yes right... I have a folder structure like .. module -- mymodule--file--sample.xml And I want if someone(anonymous user) directly access this file link, he shouldn't access the content of that xml file. Nov 14 '16 at 10:59
  • Understood. This can't be handled using above method. This should be done at .htaccess level or you can give a try to private file system.
    – Yogesh
    Nov 14 '16 at 11:02
  • Check out this ans, The question is related to Drupal 7 because of which modules will not be available for Drupal 8. But you can check configuration options mentioned for Drupal 8 Core.
    – Yogesh
    Nov 14 '16 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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