1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 UUID_REGEX = /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/
9 def self.included(base)
10 base.extend(ClassMethods)
11 base.validate :validate_uuid
12 base.before_create :assign_uuid
13 base.before_destroy :destroy_permission_links
14 base.has_many(:links_via_head,
15 -> { where("not (link_class = 'permission')") },
17 foreign_key: :head_uuid,
20 base.has_many(:links_via_tail,
21 -> { where("not (link_class = 'permission')") },
23 foreign_key: :tail_uuid,
30 Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1]
33 [Server::Application.config.uuid_prefix,
35 rand(2**256).to_s(36)[-15..-1]].
43 self.respond_to? :uuid
47 if self.respond_to_uuid? and self.uuid_changed?
48 if current_user.andand.is_admin and self.uuid.is_a?(String)
49 if (re = self.uuid.match HasUuid::UUID_REGEX)
50 if re[1] == self.class.uuid_prefix
53 self.errors.add(:uuid, "type field is '#{re[1]}', expected '#{self.class.uuid_prefix}'")
57 self.errors.add(:uuid, "not a valid Arvados uuid '#{self.uuid}'")
62 self.errors.add(:uuid, "assignment not permitted")
64 self.errors.add(:uuid, "change not permitted")
74 if self.respond_to_uuid? and self.uuid.nil? or self.uuid.empty?
75 self.uuid = self.class.generate_uuid
80 def destroy_permission_links
82 Link.destroy_all(['link_class=? and (head_uuid=? or tail_uuid=?)',
83 'permission', uuid, uuid])