2 require 'record_filters'
4 class ArvadosModel < ActiveRecord::Base
5 self.abstract_class = true
7 include CurrentApiClient # current_user, current_api_client, etc.
11 attr_protected :created_at
12 attr_protected :modified_by_user_uuid
13 attr_protected :modified_by_client_uuid
14 attr_protected :modified_at
15 after_initialize :log_start_state
16 before_save :ensure_permission_to_save
17 before_save :ensure_owner_uuid_is_permitted
18 before_save :ensure_ownership_path_leads_to_user
19 before_destroy :ensure_owner_uuid_is_permitted
20 before_destroy :ensure_permission_to_destroy
21 before_create :update_modified_by_fields
22 before_update :maybe_update_modified_by_fields
23 after_create :log_create
24 after_update :log_update
25 after_destroy :log_destroy
26 after_find :convert_serialized_symbols_to_strings
27 before_validation :normalize_collection_uuids
28 before_validation :set_default_owner
29 validate :ensure_serialized_attribute_type
30 validate :ensure_valid_uuids
32 # Note: This only returns permission links. It does not account for
33 # permissions obtained via user.is_admin or
34 # user.uuid==object.owner_uuid.
35 has_many(:permissions,
36 foreign_key: :head_uuid,
39 conditions: "link_class = 'permission'")
41 class PermissionDeniedError < StandardError
47 class AlreadyLockedError < StandardError
53 class InvalidStateTransitionError < StandardError
59 class UnauthorizedError < StandardError
65 class UnresolvableContainerError < StandardError
71 def self.kind_class(kind)
72 kind.match(/^arvados\#(.+)$/)[1].classify.safe_constantize rescue nil
76 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}"
79 def self.selectable_attributes(template=:user)
80 # Return an array of attribute name strings that can be selected
81 # in the given template.
82 api_accessible_attributes(template).map { |attr_spec| attr_spec.first.to_s }
85 def self.searchable_columns operator
86 textonly_operator = !operator.match(/[<=>]/)
87 self.columns.select do |col|
91 when :datetime, :integer, :boolean
99 def self.attribute_column attr
100 self.columns.select { |col| col.name == attr.to_s }.first
103 def self.attributes_required_columns
104 # This method returns a hash. Each key is the name of an API attribute,
105 # and it's mapped to a list of database columns that must be fetched
106 # to generate that attribute.
107 # This implementation generates a simple map of attributes to
108 # matching column names. Subclasses can override this method
109 # to specify that method-backed API attributes need to fetch
110 # specific columns from the database.
111 all_columns = columns.map(&:name)
112 api_column_map = Hash.new { |hash, key| hash[key] = [] }
113 methods.grep(/^api_accessible_\w+$/).each do |method_name|
114 next if method_name == :api_accessible_attributes
115 send(method_name).each_pair do |api_attr_name, col_name|
116 col_name = col_name.to_s
117 if all_columns.include?(col_name)
118 api_column_map[api_attr_name.to_s] |= [col_name]
125 def self.ignored_select_attributes
126 ["href", "kind", "etag"]
129 def self.columns_for_attributes(select_attributes)
130 if select_attributes.empty?
131 raise ArgumentError.new("Attribute selection list cannot be empty")
133 api_column_map = attributes_required_columns
135 select_attributes.each do |s|
136 next if ignored_select_attributes.include? s
137 if not s.is_a? String or not api_column_map.include? s
141 if not invalid_attrs.empty?
142 raise ArgumentError.new("Invalid attribute(s): #{invalid_attrs.inspect}")
144 # Given an array of attribute names to select, return an array of column
145 # names that must be fetched from the database to satisfy the request.
146 select_attributes.flat_map { |attr| api_column_map[attr] }.uniq
149 def self.default_orders
150 ["#{table_name}.modified_at desc", "#{table_name}.uuid"]
153 def self.unique_columns
157 # If current user can manage the object, return an array of uuids of
158 # users and groups that have permission to write the object. The
159 # first two elements are always [self.owner_uuid, current user's
162 # If current user can write but not manage the object, return
163 # [self.owner_uuid, current user's uuid].
165 # If current user cannot write this object, just return
168 return [owner_uuid] if not current_user
169 unless (owner_uuid == current_user.uuid or
170 current_user.is_admin or
171 (current_user.groups_i_can(:manage) & [uuid, owner_uuid]).any?)
172 if ((current_user.groups_i_can(:write) + [current_user.uuid]) &
173 [uuid, owner_uuid]).any?
174 return [owner_uuid, current_user.uuid]
179 [owner_uuid, current_user.uuid] + permissions.collect do |p|
180 if ['can_write', 'can_manage'].index p.name
186 # Return a query with read permissions restricted to the union of of the
187 # permissions of the members of users_list, i.e. if something is readable by
188 # any user in users_list, it will be readable in the query returned by this
190 def self.readable_by(*users_list)
191 # Get rid of troublesome nils
194 # Load optional keyword arguments, if they exist.
195 if users_list.last.is_a? Hash
196 kwargs = users_list.pop
201 # Check if any of the users are admin. If so, we're done.
202 if users_list.select { |u| u.is_admin }.any?
206 # Collect the UUIDs of the authorized users.
207 user_uuids = users_list.map { |u| u.uuid }
209 # Collect the UUIDs of all groups readable by any of the
210 # authorized users. If one of these (or the UUID of one of the
211 # authorized users themselves) is an object's owner_uuid, that
212 # object is readable.
213 owner_uuids = user_uuids + users_list.flat_map { |u| u.groups_i_can(:read) }
217 sql_table = kwargs.fetch(:table_name, table_name)
219 # Match any object (evidently a group or user) whose UUID is
220 # listed explicitly in owner_uuids.
221 sql_conds += ["#{sql_table}.uuid in (:owner_uuids)"]
223 # Match any object whose owner is listed explicitly in
225 sql_conds += ["#{sql_table}.owner_uuid IN (:owner_uuids)"]
227 # Match the head of any permission link whose tail is listed
228 # explicitly in owner_uuids.
229 sql_conds += ["#{sql_table}.uuid IN (SELECT head_uuid FROM links WHERE link_class='permission' AND tail_uuid IN (:owner_uuids))"]
231 if sql_table == "links"
232 # Match any permission link that gives one of the authorized
233 # users some permission _or_ gives anyone else permission to
234 # view one of the authorized users.
235 sql_conds += ["(#{sql_table}.link_class in (:permission_link_classes) AND "+
236 "(#{sql_table}.head_uuid IN (:user_uuids) OR #{sql_table}.tail_uuid IN (:user_uuids)))"]
239 where(sql_conds.join(' OR '),
240 owner_uuids: owner_uuids,
241 user_uuids: user_uuids,
242 permission_link_classes: ['permission', 'resources'])
245 def logged_attributes
246 attributes.except(*Rails.configuration.unlogged_attributes)
249 def self.full_text_searchable_columns
250 self.columns.select do |col|
251 col.type == :string or col.type == :text
255 def self.full_text_tsvector
256 parts = full_text_searchable_columns.collect do |column|
257 "coalesce(#{column},'')"
259 "to_tsvector('english', #{parts.join(" || ' ' || ")})"
262 def self.apply_filters query, filters
263 ft = record_filters filters, self
264 if not ft[:cond_out].any?
267 query.where('(' + ft[:cond_out].join(') AND (') + ')',
273 def self.deep_sort_hash(x)
275 x.sort.collect do |k, v|
276 [k, deep_sort_hash(v)]
279 x.collect { |v| deep_sort_hash(v) }
285 def ensure_ownership_path_leads_to_user
286 if new_record? or owner_uuid_changed?
287 uuid_in_path = {owner_uuid => true, uuid => true}
289 while (owner_class = ArvadosModel::resource_class_for_uuid(x)) != User
292 # Test for cycles with the new version, not the DB contents
294 elsif !owner_class.respond_to? :find_by_uuid
295 raise ActiveRecord::RecordNotFound.new
297 x = owner_class.find_by_uuid(x).owner_uuid
299 rescue ActiveRecord::RecordNotFound => e
300 errors.add :owner_uuid, "is not owned by any user: #{e}"
305 errors.add :owner_uuid, "would create an ownership cycle"
307 errors.add :owner_uuid, "has an ownership cycle"
311 uuid_in_path[x] = true
317 def set_default_owner
318 if new_record? and current_user and respond_to? :owner_uuid=
319 self.owner_uuid ||= current_user.uuid
323 def ensure_owner_uuid_is_permitted
324 raise PermissionDeniedError if !current_user
326 if self.owner_uuid.nil?
327 errors.add :owner_uuid, "cannot be nil"
328 raise PermissionDeniedError
331 rsc_class = ArvadosModel::resource_class_for_uuid owner_uuid
332 unless rsc_class == User or rsc_class == Group
333 errors.add :owner_uuid, "must be set to User or Group"
334 raise PermissionDeniedError
337 # Verify "write" permission on old owner
338 # default fail unless one of:
339 # owner_uuid did not change
340 # previous owner_uuid is nil
341 # current user is the old owner
342 # current user is this object
343 # current user can_write old owner
344 unless !owner_uuid_changed? or
345 owner_uuid_was.nil? or
346 current_user.uuid == self.owner_uuid_was or
347 current_user.uuid == self.uuid or
348 current_user.can? write: self.owner_uuid_was
349 logger.warn "User #{current_user.uuid} tried to modify #{self.class.to_s} #{uuid} but does not have permission to write old owner_uuid #{owner_uuid_was}"
350 errors.add :owner_uuid, "cannot be changed without write permission on old owner"
351 raise PermissionDeniedError
354 # Verify "write" permission on new owner
355 # default fail unless one of:
356 # current_user is this object
357 # current user can_write new owner, or this object if owner unchanged
358 if new_record? or owner_uuid_changed? or is_a?(ApiClientAuthorization)
359 write_target = owner_uuid
363 unless current_user == self or current_user.can? write: write_target
364 logger.warn "User #{current_user.uuid} tried to modify #{self.class.to_s} #{uuid} but does not have permission to write new owner_uuid #{owner_uuid}"
365 errors.add :owner_uuid, "cannot be changed without write permission on new owner"
366 raise PermissionDeniedError
372 def ensure_permission_to_save
373 unless (new_record? ? permission_to_create : permission_to_update)
374 raise PermissionDeniedError
378 def permission_to_create
379 current_user.andand.is_active
382 def permission_to_update
384 logger.warn "Anonymous user tried to update #{self.class.to_s} #{self.uuid_was}"
387 if !current_user.is_active
388 logger.warn "Inactive user #{current_user.uuid} tried to update #{self.class.to_s} #{self.uuid_was}"
391 return true if current_user.is_admin
392 if self.uuid_changed?
393 logger.warn "User #{current_user.uuid} tried to change uuid of #{self.class.to_s} #{self.uuid_was} to #{self.uuid}"
399 def ensure_permission_to_destroy
400 raise PermissionDeniedError unless permission_to_destroy
403 def permission_to_destroy
407 def maybe_update_modified_by_fields
408 update_modified_by_fields if self.changed? or self.new_record?
412 def update_modified_by_fields
413 current_time = db_current_time
414 self.updated_at = current_time
415 self.owner_uuid ||= current_default_owner if self.respond_to? :owner_uuid=
416 self.modified_at = current_time
417 self.modified_by_user_uuid = current_user ? current_user.uuid : nil
418 self.modified_by_client_uuid = current_api_client ? current_api_client.uuid : nil
422 def self.has_symbols? x
425 return true if has_symbols?(k) or has_symbols?(v)
429 return true if has_symbols?(k)
434 return true if x.start_with?(':') && !x.start_with?('::')
439 def self.recursive_stringify x
441 Hash[x.collect do |k,v|
442 [recursive_stringify(k), recursive_stringify(v)]
446 recursive_stringify k
450 elsif x.is_a? String and x.start_with?(':') and !x.start_with?('::')
457 def ensure_serialized_attribute_type
458 # Specifying a type in the "serialize" declaration causes rails to
459 # raise an exception if a different data type is retrieved from
460 # the database during load(). The validation preventing such
461 # crash-inducing records from being inserted in the database in
462 # the first place seems to have been left as an exercise to the
464 self.class.serialized_attributes.each do |colname, attr|
466 if self.attributes[colname].class != attr.object_class
467 self.errors.add colname.to_sym, "must be a #{attr.object_class.to_s}, not a #{self.attributes[colname].class.to_s}"
468 elsif self.class.has_symbols? attributes[colname]
469 self.errors.add colname.to_sym, "must not contain symbols: #{attributes[colname].inspect}"
475 def convert_serialized_symbols_to_strings
476 # ensure_serialized_attribute_type should prevent symbols from
477 # getting into the database in the first place. If someone managed
478 # to get them into the database (perhaps using an older version)
479 # we'll convert symbols to strings when loading from the
480 # database. (Otherwise, loading and saving an object with existing
481 # symbols in a serialized field will crash.)
482 self.class.serialized_attributes.each do |colname, attr|
483 if self.class.has_symbols? attributes[colname]
484 attributes[colname] = self.class.recursive_stringify attributes[colname]
485 self.send(colname + '=',
486 self.class.recursive_stringify(attributes[colname]))
491 def foreign_key_attributes
492 attributes.keys.select { |a| a.match(/_uuid$/) }
495 def skip_uuid_read_permission_check
496 %w(modified_by_client_uuid)
499 def skip_uuid_existence_check
503 def normalize_collection_uuids
504 foreign_key_attributes.each do |attr|
505 attr_value = send attr
506 if attr_value.is_a? String and
507 attr_value.match(/^[0-9a-f]{32,}(\+[@\w]+)*$/)
509 send "#{attr}=", Collection.normalize_uuid(attr_value)
511 # TODO: abort instead of silently accepting unnormalizable value?
517 @@prefixes_hash = nil
518 def self.uuid_prefixes
519 unless @@prefixes_hash
521 Rails.application.eager_load!
522 ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
523 if k.respond_to?(:uuid_prefix)
524 @@prefixes_hash[k.uuid_prefix] = k
531 def self.uuid_like_pattern
532 "#{Rails.configuration.uuid_prefix}-#{uuid_prefix}-_______________"
536 %r/[a-z0-9]{5}-#{uuid_prefix}-[a-z0-9]{15}/
539 def ensure_valid_uuids
540 specials = [system_user_uuid]
542 foreign_key_attributes.each do |attr|
543 if new_record? or send (attr + "_changed?")
544 next if skip_uuid_existence_check.include? attr
545 attr_value = send attr
546 next if specials.include? attr_value
548 if (r = ArvadosModel::resource_class_for_uuid attr_value)
549 unless skip_uuid_read_permission_check.include? attr
550 r = r.readable_by(current_user)
552 if r.where(uuid: attr_value).count == 0
553 errors.add(attr, "'#{attr_value}' not found")
570 def self.readable_by (*u)
575 [{:uuid => u[:uuid]}]
579 def self.resource_class_for_uuid(uuid)
580 if uuid.is_a? ArvadosModel
583 unless uuid.is_a? String
587 uuid.match HasUuid::UUID_REGEX do |re|
588 return uuid_prefixes[re[1]] if uuid_prefixes[re[1]]
591 if uuid.match(/.+@.+/)
598 # ArvadosModel.find_by_uuid needs extra magic to allow it to return
599 # an object in any class.
600 def self.find_by_uuid uuid
601 if self == ArvadosModel
602 # If called directly as ArvadosModel.find_by_uuid rather than via subclass,
603 # delegate to the appropriate subclass based on the given uuid.
604 self.resource_class_for_uuid(uuid).unscoped.find_by_uuid(uuid)
611 @old_attributes = Marshal.load(Marshal.dump(attributes))
612 @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes))
615 def log_change(event_type)
616 log = Log.new(event_type: event_type).fill_object(self)
623 log_change('create') do |log|
624 log.fill_properties('old', nil, nil)
630 log_change('update') do |log|
631 log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
637 log_change('delete') do |log|
638 log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)