rename foreign uuid attributes
[arvados.git] / services / api / lib / current_api_client.rb
1 module CurrentApiClient
2   def current_user
3     Thread.current[:user]
4   end
5
6   def current_api_client
7     Thread.current[:api_client]
8   end
9
10   def current_api_client_authorization
11     Thread.current[:api_client_authorization]
12   end
13
14   def current_default_owner
15     # owner_uuid for newly created objects
16     ((current_api_client_authorization &&
17       current_api_client_authorization.default_owner) ||
18      (current_user && current_user.default_owner) ||
19      (current_user && current_user.uuid) ||
20      nil)
21   end
22
23   # Where is the client connecting from?
24   def current_api_client_ip_address
25     Thread.current[:api_client_ip_address]
26   end
27
28   def system_user_uuid
29     [Server::Application.config.uuid_prefix,
30      User.uuid_prefix,
31      '000000000000000'].join('-')
32   end
33
34   def system_user
35     if not $system_user
36       real_current_user = Thread.current[:user]
37       Thread.current[:user] = User.new(is_admin: true, is_active: true)
38       $system_user = User.where('uuid=?', system_user_uuid).first
39       if !$system_user
40         $system_user = User.new(uuid: system_user_uuid,
41                                 is_active: true,
42                                 is_admin: true,
43                                 email: 'root',
44                                 first_name: 'root',
45                                 last_name: '')
46         $system_user.save!
47         $system_user.reload
48       end
49       Thread.current[:user] = real_current_user
50     end
51     $system_user
52   end
53
54   def act_as_system_user
55     Thread.current[:user] = system_user
56   end
57 end