X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/cc6f3141d4e55c8adab282762bad9a8643336346..3b3c3a0869c2cf528b9e1c45c969bbbd47f6446e:/services/api/lib/has_uuid.rb diff --git a/services/api/lib/has_uuid.rb b/services/api/lib/has_uuid.rb index 9f1bbf4d25..74d09e94e9 100644 --- a/services/api/lib/has_uuid.rb +++ b/services/api/lib/has_uuid.rb @@ -7,8 +7,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 @@ -31,36 +41,33 @@ module HasUuid def validate_uuid if self.respond_to_uuid? and self.uuid_changed? - if self.uuid_was.nil? or self.uuid_was == "" - if self.uuid.is_a?(String) and self.uuid.length > 0 and current_user.andand.is_admin - if (re = self.uuid.match HasUuid::UUID_REGEX) - if re[1] == self.class.uuid_prefix - return true - else - self.errors.add(:uuid, "Matched uuid type '#{re[1]}', expected '#{self.class.uuid_prefix}'") - return false - end + if current_user.andand.is_admin and self.uuid.is_a?(String) + if (re = self.uuid.match HasUuid::UUID_REGEX) + if re[1] == self.class.uuid_prefix + return true else - self.errors.add(:uuid, "'#{self.uuid}' is not a valid Arvados UUID") + self.errors.add(:uuid, "type field is '#{re[1]}', expected '#{self.class.uuid_prefix}'") return false end - elsif self.uuid.nil? or self.uuid == "" - return true else - self.errors.add(:uuid, "Not permitted to specify uuid") + self.errors.add(:uuid, "not a valid Arvados uuid '#{self.uuid}'") return false end else - self.errors.add(:uuid, "Not permitted to change uuid") + if self.new_record? + self.errors.add(:uuid, "assignment not permitted") + else + self.errors.add(:uuid, "change not permitted") + end return false end + else + return true end - - true end def assign_uuid - if self.respond_to_uuid? and self.uuid.nil? or self.uuid == "" + if self.respond_to_uuid? and self.uuid.nil? or self.uuid.empty? self.uuid = self.class.generate_uuid end true