X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6403b8728d3120d6cb22583b3963d2a030e88aa3..efeca068c2c25ee075ee8907a1937a5d4119775c:/services/api/lib/has_uuid.rb diff --git a/services/api/lib/has_uuid.rb b/services/api/lib/has_uuid.rb index df175f8602..2074566941 100644 --- a/services/api/lib/has_uuid.rb +++ b/services/api/lib/has_uuid.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + module HasUuid UUID_REGEX = /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ @@ -7,8 +11,18 @@ module HasUuid base.validate :validate_uuid base.before_create :assign_uuid base.before_destroy :destroy_permission_links - base.has_many :links_via_head, class_name: 'Link', foreign_key: :head_uuid, primary_key: :uuid, conditions: "not (link_class = 'permission')", dependent: :restrict - base.has_many :links_via_tail, class_name: 'Link', foreign_key: :tail_uuid, primary_key: :uuid, conditions: "not (link_class = 'permission')", dependent: :restrict + base.has_many(:links_via_head, + -> { where("not (link_class = 'permission')") }, + class_name: 'Link', + foreign_key: :head_uuid, + primary_key: :uuid, + dependent: :destroy) + base.has_many(:links_via_tail, + -> { where("not (link_class = 'permission')") }, + class_name: 'Link', + foreign_key: :tail_uuid, + primary_key: :uuid, + dependent: :destroy) end module ClassMethods @@ -16,7 +30,7 @@ module HasUuid Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1] end def generate_uuid - [Server::Application.config.uuid_prefix, + [Rails.configuration.ClusterID, self.uuid_prefix, rand(2**256).to_s(36)[-15..-1]]. join '-' @@ -36,18 +50,18 @@ module HasUuid if re[1] == self.class.uuid_prefix return true else - self.errors.add(:uuid, "Matched uuid type '#{re[1]}', expected '#{self.class.uuid_prefix}'") + self.errors.add(:uuid, "type field is '#{re[1]}', expected '#{self.class.uuid_prefix}'") return false end else - self.errors.add(:uuid, "'#{self.uuid}' is not a valid Arvados UUID") + self.errors.add(:uuid, "not a valid Arvados uuid '#{self.uuid}'") return false end else if self.new_record? - self.errors.add(:uuid, "Not permitted to specify uuid") + self.errors.add(:uuid, "assignment not permitted") else - self.errors.add(:uuid, "Not permitted to change uuid") + self.errors.add(:uuid, "change not permitted") end return false end @@ -65,8 +79,8 @@ module HasUuid def destroy_permission_links if uuid - Link.destroy_all(['link_class=? and (head_uuid=? or tail_uuid=?)', - 'permission', uuid, uuid]) + Link.where(['link_class=? and (head_uuid=? or tail_uuid=?)', + 'permission', uuid, uuid]).destroy_all end end end