Score:0

How can the custom files copied by code to the private directory be accessible through URL for authenticated users?

us flag

In my project, I am using headless Drupal with .Net on the front end.

I have to use private and encrypted file systems for the project requirements. The files which I am uploading through Drupal private file field from content type are accessible through URL, but as encrypted files aren't accessible through direct URL, I thought of copying the decrypted files from the encrypted folder to the private folder so they become accessible. Strangely, these files aren't accessible for authenticated users, who get access denied for this particular case.

I can't copy these files to the public folder as it is accessible to anonymous users too. So here I am facing two problems:

  • Is there any way to restrict public files(sites/default/files/*) to authenticated users only so that anonymous users can't view them? I tried the rewrite rules in the .htaccess file, but it isn't restricting the URL to particular users, and it redirects users to a private URL.
  • Is there any way to access the copied files from the private folder via URL?

These files aren't uploaded through Drupal private file field, but they are copied from the encrypted folder to the private folder through custom code.

Score:-1
in flag

I would suggest using a Controller whose route parameter(s) can be used to specify the desired file. Then your controller (either directly or by delegating to another service) can access the file, decrypt it, then return the contents. The route that you define for your controller supports permissions, so you can easily apply access control.

Drupal console is a fantastic resource for generating Drupal assets, such as controllers. To generate a new controller with Drupal console: drupal generate:controller.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.