Merge remote-tracking branch 'origin/master' into origin-2608-websocket-event-bus...
[arvados.git] / services / api / lib / assign_uuid.rb
1 module AssignUuid
2
3   def self.included(base)
4     base.extend(ClassMethods)
5     base.before_create :assign_uuid
6   end
7
8   module ClassMethods
9     def uuid_prefix
10       Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1]
11     end
12     def generate_uuid
13       [Server::Application.config.uuid_prefix,
14        self.uuid_prefix,
15        rand(2**256).to_s(36)[-15..-1]].
16         join '-'
17     end
18   end
19
20   protected
21
22   def respond_to_uuid?
23     self.respond_to? :uuid
24   end
25
26   def assign_uuid
27     return true if !self.respond_to_uuid?
28     return true if uuid and current_user and current_user.is_admin
29     self.uuid = self.class.generate_uuid
30   end
31 end