1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
10 $anonymous_group_read_permission = nil
11 $empty_collection = nil
12 $public_project_group = nil
13 $public_project_group_read_permission = nil
15 module CurrentApiClient
20 def current_api_client
21 Thread.current[:api_client]
24 def current_api_client_authorization
25 Thread.current[:api_client_authorization]
29 Thread.current[:api_url_base]
32 def current_default_owner
33 # owner_uuid for newly created objects
34 ((current_api_client_authorization &&
35 current_api_client_authorization.default_owner_uuid) ||
36 (current_user && current_user.default_owner_uuid) ||
37 (current_user && current_user.uuid) ||
41 # Where is the client connecting from?
42 def current_api_client_ip_address
43 Thread.current[:api_client_ip_address]
47 [Rails.configuration.ClusterID,
49 '000000000000000'].join('-')
53 [Rails.configuration.ClusterID,
55 '000000000000000'].join('-')
58 def anonymous_group_uuid
59 [Rails.configuration.ClusterID,
61 'anonymouspublic'].join('-')
64 def anonymous_user_uuid
65 [Rails.configuration.ClusterID,
67 'anonymouspublic'].join('-')
70 def public_project_uuid
71 [Rails.configuration.ClusterID,
73 'publicfavorites'].join('-')
77 $system_user = check_cache $system_user do
78 real_current_user = Thread.current[:user]
80 Thread.current[:user] = User.new(is_admin: true,
82 uuid: system_user_uuid)
83 User.where(uuid: system_user_uuid).
84 first_or_create!(is_active: true,
90 Thread.current[:user] = real_current_user
96 $system_group = check_cache $system_group do
98 ActiveRecord::Base.transaction do
99 Group.where(uuid: system_group_uuid).
100 first_or_create!(name: "System group",
101 description: "System group",
102 group_class: "role") do |g|
104 User.all.collect(&:uuid).each do |user_uuid|
105 Link.create!(link_class: 'permission',
107 tail_uuid: system_group_uuid,
108 head_uuid: user_uuid)
116 def all_users_group_uuid
117 [Rails.configuration.ClusterID,
119 'fffffffffffffff'].join('-')
123 $all_users_group = check_cache $all_users_group do
124 act_as_system_user do
125 ActiveRecord::Base.transaction do
126 Group.where(uuid: all_users_group_uuid).
127 first_or_create!(name: "All users",
128 description: "All users",
135 def act_as_system_user
137 act_as_user system_user do
141 Thread.current[:user] = system_user
146 user_was = Thread.current[:user]
147 Thread.current[:user] = user
151 Thread.current[:user] = user_was
156 $anonymous_group = check_cache $anonymous_group do
157 act_as_system_user do
158 ActiveRecord::Base.transaction do
159 Group.where(uuid: anonymous_group_uuid).
160 first_or_create!(group_class: "role",
161 name: "Anonymous users",
162 description: "Anonymous users")
168 def anonymous_group_read_permission
169 $anonymous_group_read_permission =
170 check_cache $anonymous_group_read_permission do
171 act_as_system_user do
172 Link.where(tail_uuid: all_users_group.uuid,
173 head_uuid: anonymous_group.uuid,
174 link_class: "permission",
175 name: "can_read").first_or_create!
181 $anonymous_user = check_cache $anonymous_user do
182 act_as_system_user do
183 User.where(uuid: anonymous_user_uuid).
184 first_or_create!(is_active: false,
187 first_name: 'Anonymous',
188 last_name: '') do |u|
190 Link.where(tail_uuid: anonymous_user_uuid,
191 head_uuid: anonymous_group.uuid,
192 link_class: 'permission',
200 def public_project_group
201 $public_project_group = check_cache $public_project_group do
202 act_as_system_user do
203 ActiveRecord::Base.transaction do
204 Group.where(uuid: public_project_uuid).
205 first_or_create!(group_class: "project",
206 name: "Public favorites",
207 description: "Public favorites")
213 def public_project_read_permission
214 $public_project_group_read_permission =
215 check_cache $public_project_group_read_permission do
216 act_as_system_user do
217 Link.where(tail_uuid: anonymous_group.uuid,
218 head_uuid: public_project_group.uuid,
219 link_class: "permission",
220 name: "can_read").first_or_create!
225 def system_root_token_api_client
226 $system_root_token_api_client = check_cache $system_root_token_api_client do
227 act_as_system_user do
228 ActiveRecord::Base.transaction do
229 ApiClient.find_or_create_by!(is_trusted: true, url_prefix: "", name: "SystemRootToken")
235 def empty_collection_pdh
236 'd41d8cd98f00b204e9800998ecf8427e+0'
240 $empty_collection = check_cache $empty_collection do
241 act_as_system_user do
242 ActiveRecord::Base.transaction do
244 where(portable_data_hash: empty_collection_pdh).
245 first_or_create(manifest_text: '', owner_uuid: system_user.uuid, name: "empty collection") do |c|
247 Link.where(tail_uuid: anonymous_group.uuid,
249 link_class: 'permission',
261 # If the given value is nil, or the cache has been cleared since it
262 # was set, yield. Otherwise, return the given value.
263 def check_cache value
264 if not Rails.env.test? and
265 ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
266 not File.owned? ActionController::Base.cache_store.cache_path
267 # If we don't own the cache dir, we're probably
268 # crunch-dispatch. Whoever we are, using this cache is likely to
269 # either fail or screw up the cache for someone else. So we'll
270 # just assume the $globals are OK to live forever.
272 # The reason for making the globals expire with the cache in the
273 # first place is to avoid leaking state between test cases: in
274 # production, we don't expect the database seeds to ever go away
275 # even when the cache is cleared, so there's no particular
276 # reason to expire our global variables.
278 Rails.cache.fetch "CurrentApiClient.$globals" do
283 return value unless value.nil?