Answer the question
In order to leave comments, you need to log in
How to restrict user access to folders in storge?
Good evening! How can I restrict access to only my files in storage, in which everything is divided by user id. The essence is such that one user could not have access to files that he did not download. Thanks
Answer the question
In order to leave comments, you need to log in
Create a disk with visibility private in the filesystems config or change file privacy on boot.
Make the relation user hasMany files.
Make the FilePolicy policy where to register in the view / download method
return $file->user_id === $user->id;
Before running the Storage methods, run the policy check everywhere. For example in the controller
$this->authorize('download', $file);
return Storage::disk($disk)->download($path);
At the level of restricting direct access to files, no way!
Write the solution in your code, serving files from PHP.
Alternatively, instead of the usual numeric id, use a unique independent uuid, but access will still be direct, but it’s not so easy to calculate it.
You also need to use Temporary URLs, especially since Laravel can do this out of the box.
And don't forget to put files in storage without the public mark.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question