dimanche 25 mars 2018

Upload, Store, and Download files with privacy restrictions in Laravel

I'm developing a website where Administrator can upload files (in a restricted area just for administrators) and associate the files with users. When regular users login to the website they'll have a "Documents" section where they can see the files that were uploaded and associated with them.

I dug a lot online but all the information made me confused so I thought of asking you all :)

This is what I basically trying to achieve:

  1. An Administrator can access a restricted area where she can upload multiple files using an upload form and for each file, she can choose with which users to associate the file.
    I Want to be able to create a form and before any update occurs, to list the file names and a Multi-Select box with all the users near each filename.

  2. The uploaded file will be associated with one or more users and users can be associated with more than one file. i.e many-to-many relationship.
    I thought of creating a file_user pivot table so each File class can be associated with many instances of User class and vice versa.

  3. The name of the file should stay as the original name but there should be a possibility for duplicate names. Thus, I should create something to distinct the files and keep them unique (but still, with the same filename).
    I thought of different folder names, but it will be messy in the server.

  4. Users should be able to access and download only files that are associated with them. They shouldn't be able to access a file which is not associated with them, neither to know of its existent. (i.e File Not Found instead of Permission Denied).


I already have an authentication implemented and I know how to create a page that is restricted only to Admin. In order to implement the files-upload and download services I thought of something like this:

  1. Create a File model, with migration and Controller
  2. Create a FileController@create (the multi-file upload form view), FileController@store (to store the files in the database), FileController@index (handles the view where the user sees the files associated with them and can download them) and FileController@download (responsible to download a file to an authorized user) functions in FileController.
  3. Create views for the above methods
  4. Create a files table in the database (via migration) to contain file_id and file_name.
  5. Create a file_user pivot table because of their many-to-many characteristic.

I'm struggling with implementing the content of the above, with restricting the files to the allowed users, and I'm not sure that this is the best and most-secure approach.

How would you implement this? If you have time, I'll appreciate if you'll add pieces of code for each stage so I can understand better. A simple application that implements this would be the best to learn from. So far I didn't find any online.

I would be happy to elaborate more and explain in details the pieces that weren't clear.
Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire