From f056e8dcb21c9b820f44ddf4590b8a1c4c1fc8db Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 9 Mar 2018 23:07:53 -0600 Subject: [PATCH] Retrieve protected files using the defined file model Adds support to retrieve protected files using the class they are defined to be using if that class differs from the default System\Models\File class. This makes it possible to use a custom class extending the base file model class that does output processing on the file data (for example, an EncryptedFile class that has to decrypt the file contents before they can be output to the browser). --- modules/backend/controllers/Files.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/backend/controllers/Files.php b/modules/backend/controllers/Files.php index 80c952fe3..97a68ee69 100644 --- a/modules/backend/controllers/Files.php +++ b/modules/backend/controllers/Files.php @@ -110,6 +110,23 @@ class Files extends Controller if (!$file = FileModel::find((int) $id)) { throw new ApplicationException('Unable to find file'); } + + /** + * Ensure that the file model utilized for this request is + * the one specified in the relationship configuration + */ + if ($file->attachment) { + $fileModel = $file->attachment->{$file->field}()->getRelated(); + + /** + * Only attempt to get file model through its assigned class + * when the assigned class differs from the default one that + * the file has already been loaded from + */ + if (get_class($file) !== get_class($fileModel)) { + $file = $fileModel->find($file->id); + } + } $verifyCode = self::getUniqueCode($file); if ($code != $verifyCode) {