3 UUID_REGEX = /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/
5 def self.included(base)
6 base.extend(ClassMethods)
7 base.validate :validate_uuid
8 base.before_create :assign_uuid
9 base.before_destroy :destroy_permission_links
10 base.has_many(:links_via_head,
11 -> { where("not (link_class = 'permission')") },
13 foreign_key: :head_uuid,
16 base.has_many(:links_via_tail,
17 -> { where("not (link_class = 'permission')") },
19 foreign_key: :tail_uuid,
26 Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1]
29 [Server::Application.config.uuid_prefix,
31 rand(2**256).to_s(36)[-15..-1]].
39 self.respond_to? :uuid
43 if self.respond_to_uuid? and self.uuid_changed?
44 if current_user.andand.is_admin and self.uuid.is_a?(String)
45 if (re = self.uuid.match HasUuid::UUID_REGEX)
46 if re[1] == self.class.uuid_prefix
49 self.errors.add(:uuid, "type field is '#{re[1]}', expected '#{self.class.uuid_prefix}'")
53 self.errors.add(:uuid, "not a valid Arvados uuid '#{self.uuid}'")
58 self.errors.add(:uuid, "assignment not permitted")
60 self.errors.add(:uuid, "change not permitted")
70 if self.respond_to_uuid? and self.uuid.nil? or self.uuid.empty?
71 self.uuid = self.class.generate_uuid
76 def destroy_permission_links
78 Link.destroy_all(['link_class=? and (head_uuid=? or tail_uuid=?)',
79 'permission', uuid, uuid])