3036: Adding pre-validation callback to set the portable_data_hash if necessay.
authorPeter Amstutz <peter.amstutz@curoverse.com>
Fri, 15 Aug 2014 02:55:11 +0000 (22:55 -0400)
committerPeter Amstutz <peter.amstutz@curoverse.com>
Fri, 15 Aug 2014 02:55:11 +0000 (22:55 -0400)
Added validation callback to check that portable_data_hash matches manifest_text.

services/api/app/models/collection.rb

index b451ad0293f0d499d9517829f6596c8ae4acefe9..72cd0b681912cd65e031a9bf2fe1de3f70580890 100644 (file)
@@ -3,6 +3,9 @@ class Collection < ArvadosModel
   include KindAndEtag
   include CommonApiTemplate
 
+  before_validation :set_portable_data_hash
+  validate :ensure_manifest_matches_hash
+
   api_accessible :user, extend: :common do |t|
     t.add :data_size
     t.add :files
@@ -15,6 +18,22 @@ class Collection < ArvadosModel
     t.add :manifest_text
   end
 
+  def set_portable_data_hash
+    if portable_data_hash.nil? or portable_data_hash == "" or
+        (manifest_text_changed? and !portable_data_hash_changed?)
+      portable_data_hash = "#{Digest::MD5.hexdigest(manifest_text)}+#{manifest_text.length}"
+    end
+    true
+  end
+
+  def ensure_manifest_matches_hash
+    unless Digest::MD5.hexdigest(manifest_text) == portable_data_hash
+      errors.add(:portable_data_hash, "does not match hash of manifest_text")
+      return false
+    end
+    true
+  end
+
   def redundancy_status
     if redundancy_confirmed_as.nil?
       'unconfirmed'