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
13 module CurrentApiClient
18 def current_api_client
19 Thread.current[:api_client]
22 def current_api_client_authorization
23 Thread.current[:api_client_authorization]
27 Thread.current[:api_url_base]
30 def current_default_owner
31 # owner_uuid for newly created objects
32 ((current_api_client_authorization &&
33 current_api_client_authorization.default_owner_uuid) ||
34 (current_user && current_user.default_owner_uuid) ||
35 (current_user && current_user.uuid) ||
39 # Where is the client connecting from?
40 def current_api_client_ip_address
41 Thread.current[:api_client_ip_address]
45 [Rails.configuration.ClusterID,
47 '000000000000000'].join('-')
51 [Rails.configuration.ClusterID,
53 '000000000000000'].join('-')
56 def anonymous_group_uuid
57 [Rails.configuration.ClusterID,
59 'anonymouspublic'].join('-')
62 def anonymous_user_uuid
63 [Rails.configuration.ClusterID,
65 'anonymouspublic'].join('-')
69 $system_user = check_cache $system_user do
70 real_current_user = Thread.current[:user]
72 Thread.current[:user] = User.new(is_admin: true,
74 uuid: system_user_uuid)
75 User.where(uuid: system_user_uuid).
76 first_or_create!(is_active: true,
82 Thread.current[:user] = real_current_user
88 $system_group = check_cache $system_group do
90 ActiveRecord::Base.transaction do
91 Group.where(uuid: system_group_uuid).
92 first_or_create!(name: "System group",
93 description: "System group",
94 group_class: "role") do |g|
96 User.all.collect(&:uuid).each do |user_uuid|
97 Link.create!(link_class: 'permission',
99 tail_uuid: system_group_uuid,
100 head_uuid: user_uuid)
108 def all_users_group_uuid
109 [Rails.configuration.ClusterID,
111 'fffffffffffffff'].join('-')
115 $all_users_group = check_cache $all_users_group do
116 act_as_system_user do
117 ActiveRecord::Base.transaction do
118 Group.where(uuid: all_users_group_uuid).
119 first_or_create!(name: "All users",
120 description: "All users",
127 def act_as_system_user
129 act_as_user system_user do
133 Thread.current[:user] = system_user
138 user_was = Thread.current[:user]
139 Thread.current[:user] = user
143 Thread.current[:user] = user_was
148 $anonymous_group = check_cache $anonymous_group do
149 act_as_system_user do
150 ActiveRecord::Base.transaction do
151 Group.where(uuid: anonymous_group_uuid).
152 first_or_create!(group_class: "role",
153 name: "Anonymous users",
154 description: "Anonymous users")
160 def anonymous_group_read_permission
161 $anonymous_group_read_permission =
162 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 system_root_token_api_client
193 $system_root_token_api_client = check_cache $system_root_token_api_client do
194 act_as_system_user do
195 ActiveRecord::Base.transaction do
196 ApiClient.find_or_create_by!(is_trusted: true, url_prefix: "", name: "SystemRootToken")
202 def empty_collection_pdh
203 'd41d8cd98f00b204e9800998ecf8427e+0'
207 $empty_collection = check_cache $empty_collection do
208 act_as_system_user do
209 ActiveRecord::Base.transaction do
211 where(portable_data_hash: empty_collection_pdh).
212 first_or_create(manifest_text: '', owner_uuid: system_user.uuid, name: "empty collection") do |c|
214 Link.where(tail_uuid: anonymous_group.uuid,
216 link_class: 'permission',
228 # If the given value is nil, or the cache has been cleared since it
229 # was set, yield. Otherwise, return the given value.
230 def check_cache value
231 if not Rails.env.test? and
232 ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
233 not File.owned? ActionController::Base.cache_store.cache_path
234 # If we don't own the cache dir, we're probably
235 # crunch-dispatch. Whoever we are, using this cache is likely to
236 # either fail or screw up the cache for someone else. So we'll
237 # just assume the $globals are OK to live forever.
239 # The reason for making the globals expire with the cache in the
240 # first place is to avoid leaking state between test cases: in
241 # production, we don't expect the database seeds to ever go away
242 # even when the cache is cleared, so there's no particular
243 # reason to expire our global variables.
245 Rails.cache.fetch "CurrentApiClient.$globals" do
250 return value unless value.nil?