X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/af0ece600054c97aae9661ed06731af47873a7ff..8e6cd14b7884a691a110110b0f366577437c6d9e:/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 7b8f12ba54..fd542ca909 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -1,10 +1,12 @@ require 'has_uuid' +require 'record_filters' class ArvadosModel < ActiveRecord::Base self.abstract_class = true include CurrentApiClient # current_user, current_api_client, etc. include DbCurrentTime + extend RecordFilters attr_protected :created_at attr_protected :modified_by_user_uuid @@ -40,7 +42,13 @@ class ArvadosModel < ActiveRecord::Base class AlreadyLockedError < StandardError def http_status - 403 + 422 + end + end + + class InvalidStateTransitionError < StandardError + def http_status + 422 end end @@ -231,7 +239,7 @@ class ArvadosModel < ActiveRecord::Base end def logged_attributes - attributes.except *Rails.configuration.unlogged_attributes + attributes.except(*Rails.configuration.unlogged_attributes) end def self.full_text_searchable_columns @@ -244,16 +252,32 @@ class ArvadosModel < ActiveRecord::Base parts = full_text_searchable_columns.collect do |column| "coalesce(#{column},'')" end - # We prepend a space to the tsvector() argument here. Otherwise, - # it might start with a column that has its own (non-full-text) - # index, which causes Postgres to use the column index instead of - # the tsvector index, which causes full text queries to be just as - # slow as if we had no index at all. - "to_tsvector('english', ' ' || #{parts.join(" || ' ' || ")})" + "to_tsvector('english', #{parts.join(" || ' ' || ")})" + end + + def self.apply_filters query, filters + ft = record_filters filters, self + if not ft[:cond_out].any? + return query + end + query.where('(' + ft[:cond_out].join(') AND (') + ')', + *ft[:param_out]) end protected + def self.deep_sort_hash(x) + if x.is_a? Hash + x.sort.collect do |k, v| + [k, deep_sort_hash(v)] + end.to_h + elsif x.is_a? Array + x.collect { |v| deep_sort_hash(v) } + else + x + end + end + def ensure_ownership_path_leads_to_user if new_record? or owner_uuid_changed? uuid_in_path = {owner_uuid => true, uuid => true} @@ -461,7 +485,7 @@ class ArvadosModel < ActiveRecord::Base end def foreign_key_attributes - attributes.keys.select { |a| a.match /_uuid$/ } + attributes.keys.select { |a| a.match(/_uuid$/) } end def skip_uuid_read_permission_check @@ -476,7 +500,7 @@ class ArvadosModel < ActiveRecord::Base foreign_key_attributes.each do |attr| attr_value = send attr if attr_value.is_a? String and - attr_value.match /^[0-9a-f]{32,}(\+[@\w]+)*$/ + attr_value.match(/^[0-9a-f]{32,}(\+[@\w]+)*$/) begin send "#{attr}=", Collection.normalize_uuid(attr_value) rescue @@ -501,7 +525,7 @@ class ArvadosModel < ActiveRecord::Base end def self.uuid_like_pattern - "_____-#{uuid_prefix}-_______________" + "#{Rails.configuration.uuid_prefix}-#{uuid_prefix}-_______________" end def self.uuid_regex @@ -555,13 +579,12 @@ class ArvadosModel < ActiveRecord::Base unless uuid.is_a? String return nil end - resource_class = nil uuid.match HasUuid::UUID_REGEX do |re| return uuid_prefixes[re[1]] if uuid_prefixes[re[1]] end - if uuid.match /.+@.+/ + if uuid.match(/.+@.+/) return Email end @@ -574,7 +597,7 @@ class ArvadosModel < ActiveRecord::Base if self == ArvadosModel # If called directly as ArvadosModel.find_by_uuid rather than via subclass, # delegate to the appropriate subclass based on the given uuid. - self.resource_class_for_uuid(uuid).find_by_uuid(uuid) + self.resource_class_for_uuid(uuid).unscoped.find_by_uuid(uuid) else super end @@ -607,7 +630,7 @@ class ArvadosModel < ActiveRecord::Base end def log_destroy - log_change('destroy') do |log| + log_change('delete') do |log| log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) log.update_to nil end