From b68c79c058af85a9cc61f1c8c17cf88814a2928f Mon Sep 17 00:00:00 2001
From: Florent Torregrosa <florent.torregrosa@gmail.com>
Date: Sun, 8 Feb 2015 16:32:27 +0100
Subject: [PATCH] Issue #2179473: Fix file upload for webform.

---
 protected_node.module |   37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/protected_node.module b/protected_node.module
index 5db5863..b34fad8 100644
--- a/protected_node.module
+++ b/protected_node.module
@@ -686,14 +686,30 @@ function protected_node_file_download($uri) {
   if (count($files)) {
     $file = reset($files);
     if ($file->status) {
-      $query = db_select('node', 'n');
-      $query->join('file_usage', 'fu', 'n.nid = fu.id');
-      $query->join('protected_nodes', 'pn', 'n.nid = pn.nid');
-      $query->fields('n', array('nid', 'uid'));
-      $query->fields('pn', array('protected_node_passwd_changed'));
-      $query->condition('fu.fid', $file->fid);
-      $query->condition('fu.type', 'node');
-      $query->condition('pn.protected_node_is_protected', '1');
+      // Is it a file submitted with a webform?
+      if (strpos($file->uri, '://webform/') !== FALSE) {
+        // Pass through Webform submissions to get the nid given the fid.
+        $query = db_select('file_usage', 'fu');
+        $query->join('webform_submissions', 'ws', 'ws.sid = fu.id');
+        $query->join('node', 'n', 'n.nid = ws.nid');
+        $query->join('protected_nodes', 'pn', 'n.nid = pn.nid');
+        $query->fields('n', array('nid', 'uid'));
+        $query->fields('pn', array('protected_node_passwd_changed'));
+        $query->condition('fu.module', 'webform');
+        $query->condition('fu.type', 'submission');
+        $query->condition('fu.fid', $file->fid);
+        $query->condition('pn.protected_node_is_protected', '1');
+      }
+      else {
+        $query = db_select('node', 'n');
+        $query->join('file_usage', 'fu', 'n.nid = fu.id');
+        $query->join('protected_nodes', 'pn', 'n.nid = pn.nid');
+        $query->fields('n', array('nid', 'uid'));
+        $query->fields('pn', array('protected_node_passwd_changed'));
+        $query->condition('fu.fid', $file->fid);
+        $query->condition('fu.type', 'node');
+        $query->condition('pn.protected_node_is_protected', '1');
+      }
       $number_of_results = $query->countQuery()->execute()->fetchField();
       if (0 == $number_of_results) {
         return array(); /* Row doesn't exist, it's not protected */
@@ -723,6 +739,11 @@ function protected_node_file_download($uri) {
         }
       }
     }
+    // Manage case of Webform files upload.
+    else {
+      return array();
+    }
+
   }
 
   // No password, access denied.
-- 
1.7.10.4

