Merge branch '16470-api-rails-52'
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 17 Aug 2020 19:48:31 +0000 (16:48 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 17 Aug 2020 19:48:31 +0000 (16:48 -0300)
Refs #16470

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

1  2 
services/api/app/models/arvados_model.rb

index 67794208de7c999c7b8b0b3a8c451f2b7bb36c57,c3e1ff42ad0781ed91c8da1250e9a5d0f2e47f28..6fb8ff2b33549af8e4e512a1374363f8dee8fa64
@@@ -16,6 -16,7 +16,7 @@@ class ArvadosModel < ApplicationRecor
    include DbCurrentTime
    extend RecordFilters
  
+   after_find :schedule_restoring_changes
    after_initialize :log_start_state
    before_save :ensure_permission_to_save
    before_save :ensure_owner_uuid_is_permitted
    def reload(*args)
      super
      log_start_state
+     self
    end
  
    def self.create raw_params={}, *args
      %r/[a-z0-9]{5}-#{uuid_prefix}-[a-z0-9]{15}/
    end
  
 +  def check_readable_uuid attr, attr_value
 +    return if attr_value.nil?
 +    if (r = ArvadosModel::resource_class_for_uuid attr_value)
 +      unless skip_uuid_read_permission_check.include? attr
 +        r = r.readable_by(current_user)
 +      end
 +      if r.where(uuid: attr_value).count == 0
 +        errors.add(attr, "'#{attr_value}' not found")
 +      end
 +    else
 +      # Not a valid uuid or PDH, but that (currently) is not an error.
 +    end
 +  end
 +
    def ensure_valid_uuids
      specials = [system_user_uuid]
  
          next if skip_uuid_existence_check.include? attr
          attr_value = send attr
          next if specials.include? attr_value
 -        if attr_value
 -          if (r = ArvadosModel::resource_class_for_uuid attr_value)
 -            unless skip_uuid_read_permission_check.include? attr
 -              r = r.readable_by(current_user)
 -            end
 -            if r.where(uuid: attr_value).count == 0
 -              errors.add(attr, "'#{attr_value}' not found")
 -            end
 -          end
 -        end
 +        check_readable_uuid attr, attr_value
        end
      end
    end
               Rails.configuration.AuditLogs.MaxDeleteBatch.to_i > 0)
    end
  
+   def schedule_restoring_changes
+     # This will be checked at log_start_state, to reset any (virtual) changes
+     # produced by the act of reading a serialized attribute.
+     @fresh_from_database = true
+   end
    def log_start_state
      if is_audit_logging_enabled?
        @old_attributes = Marshal.load(Marshal.dump(attributes))
        @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes))
+       if @fresh_from_database
+         # This instance was created from reading a database record. Attributes
+         # haven't been changed, but those serialized attributes will be reported
+         # as unpersisted, so we restore them to avoid issues with lock!() and
+         # with_lock().
+         restore_attributes
+         @fresh_from_database = nil
+       end
      end
    end