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 # Where is the client connecting from?
23 def current_api_client_ip_address
24 Thread.current[:api_client_ip_address]
28 [Rails.configuration.ClusterID,
30 '000000000000000'].join('-')
34 [Rails.configuration.ClusterID,
36 '000000000000000'].join('-')
39 def anonymous_group_uuid
40 [Rails.configuration.ClusterID,
42 'anonymouspublic'].join('-')
45 def anonymous_user_uuid
46 [Rails.configuration.ClusterID,
48 'anonymouspublic'].join('-')
51 def public_project_uuid
52 [Rails.configuration.ClusterID,
54 'publicfavorites'].join('-')
58 real_current_user = Thread.current[:user]
60 Thread.current[:user] = User.new(is_admin: true,
62 uuid: system_user_uuid)
63 $system_user = check_cache($system_user) do
64 User.where(uuid: system_user_uuid).
65 first_or_create!(is_active: true,
72 Thread.current[:user] = real_current_user
77 $system_group = check_cache($system_group) do
79 ActiveRecord::Base.transaction do
80 Group.where(uuid: system_group_uuid).
81 first_or_create!(name: "System group",
82 description: "System group",
83 group_class: "role") do |g|
85 User.all.collect(&:uuid).each do |user_uuid|
86 Link.create!(link_class: 'permission',
88 tail_uuid: system_group_uuid,
97 def all_users_group_uuid
98 [Rails.configuration.ClusterID,
100 'fffffffffffffff'].join('-')
104 $all_users_group = check_cache($all_users_group) do
105 act_as_system_user do
106 ActiveRecord::Base.transaction do
107 Group.where(uuid: all_users_group_uuid).
108 first_or_create!(name: "All users",
109 description: "All users",
116 def act_as_system_user
118 act_as_user system_user do
122 Thread.current[:user] = system_user
127 user_was = Thread.current[:user]
128 Thread.current[:user] = user
132 Thread.current[:user] = user_was
134 user_was.forget_cached_group_perms
140 $anonymous_group = check_cache($anonymous_group) do
141 act_as_system_user do
142 ActiveRecord::Base.transaction do
143 Group.where(uuid: anonymous_group_uuid).
144 first_or_create!(group_class: "role",
145 name: "Anonymous users",
146 description: "Anonymous users")
152 def anonymous_group_read_permission
153 $anonymous_group_read_permission = check_cache($anonymous_group_read_permission) do
154 act_as_system_user do
155 Link.where(tail_uuid: all_users_group.uuid,
156 head_uuid: anonymous_group.uuid,
157 link_class: "permission",
158 name: "can_read").first_or_create!
164 $anonymous_user = check_cache($anonymous_user) do
165 act_as_system_user do
166 User.where(uuid: anonymous_user_uuid).
167 first_or_create!(is_active: false,
170 first_name: 'Anonymous',
171 last_name: '') do |u|
173 Link.where(tail_uuid: anonymous_user_uuid,
174 head_uuid: anonymous_group.uuid,
175 link_class: 'permission',
183 def public_project_group
184 $public_project_group = check_cache($public_project_group) do
185 act_as_system_user do
186 ActiveRecord::Base.transaction do
187 Group.where(uuid: public_project_uuid).
188 first_or_create!(group_class: "project",
189 name: "Public favorites",
190 description: "Public favorites")
196 def public_project_read_permission
197 $public_project_group_read_permission = check_cache($public_project_group_read_permission) do
198 act_as_system_user do
199 Link.where(tail_uuid: anonymous_group.uuid,
200 head_uuid: public_project_group.uuid,
201 link_class: "permission",
202 name: "can_read").first_or_create!
207 def anonymous_user_token_api_client
208 $anonymous_user_token_api_client = check_cache($anonymous_user_token_api_client) do
209 act_as_system_user do
210 ActiveRecord::Base.transaction do
211 ApiClient.find_or_create_by!(is_trusted: false, url_prefix: "", name: "AnonymousUserToken")
217 def system_root_token_api_client
218 $system_root_token_api_client = check_cache($system_root_token_api_client) do
219 act_as_system_user do
220 ActiveRecord::Base.transaction do
221 ApiClient.find_or_create_by!(is_trusted: true, url_prefix: "", name: "SystemRootToken")
227 def empty_collection_pdh
228 'd41d8cd98f00b204e9800998ecf8427e+0'
232 $empty_collection = check_cache($empty_collection) do
233 act_as_system_user do
234 ActiveRecord::Base.transaction do
236 where(portable_data_hash: empty_collection_pdh).
237 first_or_create(manifest_text: '', owner_uuid: system_user.uuid, name: "empty collection") do |c|
239 Link.where(tail_uuid: anonymous_group.uuid,
241 link_class: 'permission',
251 # Purge the module globals if necessary. If the cached value is
252 # non-nil and the globals weren't purged, return the cached
253 # value. Otherwise, call the block.
255 # Purge is only done in test mode.
256 def check_cache(cached)
257 if Rails.env != 'test'
258 return (cached || yield)
260 t = Rails.cache.fetch "CurrentApiClient.$system_globals_reset" do
263 if t != $system_globals_reset
264 reset_system_globals(t)
271 def reset_system_globals(t)
272 $system_globals_reset = t
275 $all_users_group = nil
276 $anonymous_group = nil
277 $anonymous_group_read_permission = nil
278 $anonymous_user = nil
279 $public_project_group = nil
280 $public_project_group_read_permission = nil
281 $anonymous_user_token_api_client = nil
282 $system_root_token_api_client = nil
283 $empty_collection = nil
285 module_function :reset_system_globals
288 CurrentApiClient.reset_system_globals(0)