X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f1ee0cb2f85c4ea988700da2e3957565b6861ad5..1416b0952adc0bfee85e15d9c86a51c32fcfd003:/services/api/app/models/arvados_model.rb diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index 6a59d3bbaa..c67a3961d9 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: AGPL-3.0 +require 'arvados_model_updates' require 'has_uuid' require 'record_filters' require 'serializers' @@ -10,6 +11,7 @@ require 'request_error' class ArvadosModel < ActiveRecord::Base self.abstract_class = true + include ArvadosModelUpdates include CurrentApiClient # current_user, current_api_client, etc. include DbCurrentTime extend RecordFilters @@ -539,7 +541,9 @@ class ArvadosModel < ActiveRecord::Base self.updated_at = current_time self.owner_uuid ||= current_default_owner if self.respond_to? :owner_uuid= self.modified_at = current_time - self.modified_by_user_uuid = current_user ? current_user.uuid : nil + if !anonymous_updater + self.modified_by_user_uuid = current_user ? current_user.uuid : nil + end self.modified_by_client_uuid = current_api_client ? current_api_client.uuid : nil true end @@ -592,16 +596,24 @@ class ArvadosModel < ActiveRecord::Base end end - def self.where_serialized(colname, value) + def self.where_serialized(colname, value, md5: false) + colsql = colname.to_s + if md5 + colsql = "md5(#{colsql})" + end if value.empty? # rails4 stores as null, rails3 stored as serialized [] or {} - sql = "#{colname.to_s} is null or #{colname.to_s} IN (?)" + sql = "#{colsql} is null or #{colsql} IN (?)" sorted = value else - sql = "#{colname.to_s} IN (?)" + sql = "#{colsql} IN (?)" sorted = deep_sort_hash(value) end - where(sql, [sorted.to_yaml, SafeJSON.dump(sorted)]) + params = [sorted.to_yaml, SafeJSON.dump(sorted)] + if md5 + params = params.map { |x| Digest::MD5.hexdigest(x) } + end + where(sql, params) end Serializer = { @@ -759,36 +771,51 @@ class ArvadosModel < ActiveRecord::Base end end + def is_audit_logging_enabled? + return !(Rails.configuration.max_audit_log_age.to_i == 0 && + Rails.configuration.max_audit_log_delete_batch.to_i > 0) + end + def log_start_state - @old_attributes = Marshal.load(Marshal.dump(attributes)) - @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes)) + if is_audit_logging_enabled? + @old_attributes = Marshal.load(Marshal.dump(attributes)) + @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes)) + end end def log_change(event_type) - log = Log.new(event_type: event_type).fill_object(self) - yield log - log.save! - log_start_state + if is_audit_logging_enabled? + log = Log.new(event_type: event_type).fill_object(self) + yield log + log.save! + log_start_state + end end def log_create - log_change('create') do |log| - log.fill_properties('old', nil, nil) - log.update_to self + if is_audit_logging_enabled? + log_change('create') do |log| + log.fill_properties('old', nil, nil) + log.update_to self + end end end def log_update - log_change('update') do |log| - log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) - log.update_to self + if is_audit_logging_enabled? + log_change('update') do |log| + log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) + log.update_to self + end end end def log_destroy - log_change('delete') do |log| - log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) - log.update_to nil + if is_audit_logging_enabled? + log_change('delete') do |log| + log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) + log.update_to nil + end end end end