Merge branch '2640-folder-api' into 1970-folder-view
[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_api_base
15     Thread.current[:api_url_base]
16   end
17
18   def current_default_owner
19     # owner_uuid for newly created objects
20     ((current_api_client_authorization &&
21       current_api_client_authorization.default_owner_uuid) ||
22      (current_user && current_user.default_owner_uuid) ||
23      (current_user && current_user.uuid) ||
24      nil)
25   end
26
27   # Where is the client connecting from?
28   def current_api_client_ip_address
29     Thread.current[:api_client_ip_address]
30   end
31
32   def system_user_uuid
33     [Server::Application.config.uuid_prefix,
34      User.uuid_prefix,
35      '000000000000000'].join('-')
36   end
37
38   def system_group_uuid
39     [Server::Application.config.uuid_prefix,
40      Group.uuid_prefix,
41      '000000000000000'].join('-')
42   end
43
44   def system_user
45     if not $system_user
46       real_current_user = Thread.current[:user]
47       Thread.current[:user] = User.new(is_admin: true,
48                                        is_active: true,
49                                        uuid: system_user_uuid)
50       $system_user = User.where('uuid=?', system_user_uuid).first
51       if !$system_user
52         $system_user = User.new(uuid: system_user_uuid,
53                                 is_active: true,
54                                 is_admin: true,
55                                 email: 'root',
56                                 first_name: 'root',
57                                 last_name: '')
58         $system_user.save!
59         $system_user.reload
60       end
61       Thread.current[:user] = real_current_user
62     end
63     $system_user
64   end
65
66   def system_group
67     if not $system_group
68       act_as_system_user do
69         ActiveRecord::Base.transaction do
70           $system_group = Group.
71             where(uuid: system_group_uuid).first_or_create do |g|
72             g.update_attributes(name: "System group",
73                                 description: "System group")
74             User.all.collect(&:uuid).each do |user_uuid|
75               Link.create(link_class: 'permission',
76                           name: 'can_manage',
77                           tail_kind: 'arvados#group',
78                           tail_uuid: system_group_uuid,
79                           head_kind: 'arvados#user',
80                           head_uuid: user_uuid)
81             end
82           end
83         end
84       end
85     end
86     $system_group
87   end
88
89   def act_as_system_user
90     if block_given?
91       user_was = Thread.current[:user]
92       Thread.current[:user] = system_user
93       begin
94         yield
95       ensure
96         Thread.current[:user] = user_was
97       end
98     else
99       Thread.current[:user] = system_user
100     end
101   end
102 end