1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 module CurrentApiClient
10 def current_api_client
11 Thread.current[:api_client]
14 def current_api_client_authorization
15 Thread.current[:api_client_authorization]
19 Thread.current[:api_url_base]
22 def current_default_owner
23 # owner_uuid for newly created objects
24 ((current_api_client_authorization &&
25 current_api_client_authorization.default_owner_uuid) ||
26 (current_user && current_user.default_owner_uuid) ||
27 (current_user && current_user.uuid) ||
31 # Where is the client connecting from?
32 def current_api_client_ip_address
33 Thread.current[:api_client_ip_address]
37 [Rails.configuration.ClusterID,
39 '000000000000000'].join('-')
43 [Rails.configuration.ClusterID,
45 '000000000000000'].join('-')
48 def anonymous_group_uuid
49 [Rails.configuration.ClusterID,
51 'anonymouspublic'].join('-')
54 def anonymous_user_uuid
55 [Rails.configuration.ClusterID,
57 'anonymouspublic'].join('-')
60 def public_project_uuid
61 [Rails.configuration.ClusterID,
63 'publicfavorites'].join('-')
67 real_current_user = Thread.current[:user]
69 Thread.current[:user] = User.new(is_admin: true,
71 uuid: system_user_uuid)
72 $system_user = check_cache($system_user) do
73 User.where(uuid: system_user_uuid).
74 first_or_create!(is_active: true,
81 Thread.current[:user] = real_current_user
86 $system_group = check_cache($system_group) do
88 ActiveRecord::Base.transaction do
89 Group.where(uuid: system_group_uuid).
90 first_or_create!(name: "System group",
91 description: "System group",
92 group_class: "role") do |g|
94 User.all.collect(&:uuid).each do |user_uuid|
95 Link.create!(link_class: 'permission',
97 tail_uuid: system_group_uuid,
106 def all_users_group_uuid
107 [Rails.configuration.ClusterID,
109 'fffffffffffffff'].join('-')
113 $all_users_group = check_cache($all_users_group) do
114 act_as_system_user do
115 ActiveRecord::Base.transaction do
116 Group.where(uuid: all_users_group_uuid).
117 first_or_create!(name: "All users",
118 description: "All users",
125 def act_as_system_user
127 act_as_user system_user do
131 Thread.current[:user] = system_user
136 user_was = Thread.current[:user]
137 Thread.current[:user] = user
141 Thread.current[:user] = user_was
143 user_was.forget_cached_group_perms
149 $anonymous_group = check_cache($anonymous_group) do
150 act_as_system_user do
151 ActiveRecord::Base.transaction do
152 Group.where(uuid: anonymous_group_uuid).
153 first_or_create!(group_class: "role",
154 name: "Anonymous users",
155 description: "Anonymous users")
161 def anonymous_group_read_permission
162 $anonymous_group_read_permission = check_cache($anonymous_group_read_permission) do
163 act_as_system_user do
164 Link.where(tail_uuid: all_users_group.uuid,
165 head_uuid: anonymous_group.uuid,
166 link_class: "permission",
167 name: "can_read").first_or_create!
173 $anonymous_user = check_cache($anonymous_user) do
174 act_as_system_user do
175 User.where(uuid: anonymous_user_uuid).
176 first_or_create!(is_active: false,
179 first_name: 'Anonymous',
180 last_name: '') do |u|
182 Link.where(tail_uuid: anonymous_user_uuid,
183 head_uuid: anonymous_group.uuid,
184 link_class: 'permission',
192 def public_project_group
193 $public_project_group = check_cache($public_project_group) do
194 act_as_system_user do
195 ActiveRecord::Base.transaction do
196 Group.where(uuid: public_project_uuid).
197 first_or_create!(group_class: "project",
198 name: "Public favorites",
199 description: "Public favorites")
205 def public_project_read_permission
206 $public_project_group_read_permission = check_cache($public_project_group_read_permission) do
207 act_as_system_user do
208 Link.where(tail_uuid: anonymous_group.uuid,
209 head_uuid: public_project_group.uuid,
210 link_class: "permission",
211 name: "can_read").first_or_create!
216 def anonymous_user_token_api_client
217 $anonymous_user_token_api_client = check_cache($anonymous_user_token_api_client) do
218 act_as_system_user do
219 ActiveRecord::Base.transaction do
220 ApiClient.find_or_create_by!(is_trusted: false, url_prefix: "", name: "AnonymousUserToken")
226 def system_root_token_api_client
227 $system_root_token_api_client = check_cache($system_root_token_api_client) do
228 act_as_system_user do
229 ActiveRecord::Base.transaction do
230 ApiClient.find_or_create_by!(is_trusted: true, url_prefix: "", name: "SystemRootToken")
236 def empty_collection_pdh
237 'd41d8cd98f00b204e9800998ecf8427e+0'
241 $empty_collection = check_cache($empty_collection) do
242 act_as_system_user do
243 ActiveRecord::Base.transaction do
245 where(portable_data_hash: empty_collection_pdh).
246 first_or_create(manifest_text: '', owner_uuid: system_user.uuid, name: "empty collection") do |c|
248 Link.where(tail_uuid: anonymous_group.uuid,
250 link_class: 'permission',
260 # Purge the module globals if necessary. If the cached value is
261 # non-nil and the globals weren't purged, return the cached
262 # value. Otherwise, call the block.
264 # Purge is only done in test mode.
265 def check_cache(cached)
266 if Rails.env != 'test'
267 return (cached || yield)
269 t = Rails.cache.fetch "CurrentApiClient.$system_globals_reset" do
272 if t != $system_globals_reset
273 reset_system_globals(t)
280 def reset_system_globals(t)
281 $system_globals_reset = t
284 $all_users_group = nil
285 $anonymous_group = nil
286 $anonymous_group_read_permission = nil
287 $anonymous_user = nil
288 $public_project_group = nil
289 $public_project_group_read_permission = nil
290 $anonymous_user_token_api_client = nil
291 $system_root_token_api_client = nil
292 $empty_collection = nil
294 module_function :reset_system_globals
297 CurrentApiClient.reset_system_globals(0)