1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 module CurrentApiClient
10 def current_api_client_authorization
11 Thread.current[:api_client_authorization]
15 Thread.current[:api_url_base]
18 # Where is the client connecting from?
19 def current_api_client_ip_address
20 Thread.current[:api_client_ip_address]
24 [Rails.configuration.ClusterID,
26 '000000000000000'].join('-')
30 [Rails.configuration.ClusterID,
32 '000000000000000'].join('-')
35 def anonymous_group_uuid
36 [Rails.configuration.ClusterID,
38 'anonymouspublic'].join('-')
41 def anonymous_user_uuid
42 [Rails.configuration.ClusterID,
44 'anonymouspublic'].join('-')
47 def public_project_uuid
48 [Rails.configuration.ClusterID,
50 'publicfavorites'].join('-')
54 real_current_user = Thread.current[:user]
56 Thread.current[:user] = User.new(is_admin: true,
58 uuid: system_user_uuid)
59 $system_user = check_cache($system_user) do
60 User.where(uuid: system_user_uuid).
61 first_or_create!(is_active: true,
68 Thread.current[:user] = real_current_user
73 $system_group = check_cache($system_group) do
75 ActiveRecord::Base.transaction do
76 Group.where(uuid: system_group_uuid).
77 first_or_create!(name: "System group",
78 description: "System group",
79 group_class: "role") do |g|
81 User.all.collect(&:uuid).each do |user_uuid|
82 Link.create!(link_class: 'permission',
84 tail_uuid: system_group_uuid,
93 def all_users_group_uuid
94 [Rails.configuration.ClusterID,
96 'fffffffffffffff'].join('-')
100 $all_users_group = check_cache($all_users_group) do
101 act_as_system_user do
102 ActiveRecord::Base.transaction do
103 Group.where(uuid: all_users_group_uuid).
104 first_or_create!(name: "All users",
105 description: "All users",
112 def act_as_system_user
114 act_as_user system_user do
118 Thread.current[:user] = system_user
123 user_was = Thread.current[:user]
124 Thread.current[:user] = user
128 Thread.current[:user] = user_was
130 user_was.forget_cached_group_perms
136 $anonymous_group = check_cache($anonymous_group) do
137 act_as_system_user do
138 ActiveRecord::Base.transaction do
139 Group.where(uuid: anonymous_group_uuid).
140 first_or_create!(group_class: "role",
141 name: "Anonymous users",
142 description: "Anonymous users")
148 def anonymous_group_read_permission
149 $anonymous_group_read_permission = check_cache($anonymous_group_read_permission) do
150 act_as_system_user do
151 Link.where(tail_uuid: all_users_group.uuid,
152 head_uuid: anonymous_group.uuid,
153 link_class: "permission",
154 name: "can_read").first_or_create!
160 $anonymous_user = check_cache($anonymous_user) do
161 act_as_system_user do
162 User.where(uuid: anonymous_user_uuid).
163 first_or_create!(is_active: false,
166 first_name: 'Anonymous',
167 last_name: '') do |u|
169 Link.where(tail_uuid: anonymous_user_uuid,
170 head_uuid: anonymous_group.uuid,
171 link_class: 'permission',
179 def public_project_group
180 $public_project_group = check_cache($public_project_group) do
181 act_as_system_user do
182 ActiveRecord::Base.transaction do
183 Group.where(uuid: public_project_uuid).
184 first_or_create!(group_class: "project",
185 name: "Public favorites",
186 description: "Public favorites")
192 def public_project_read_permission
193 $public_project_group_read_permission = check_cache($public_project_group_read_permission) do
194 act_as_system_user do
195 Link.where(tail_uuid: anonymous_group.uuid,
196 head_uuid: public_project_group.uuid,
197 link_class: "permission",
198 name: "can_read").first_or_create!
203 def empty_collection_pdh
204 'd41d8cd98f00b204e9800998ecf8427e+0'
208 $empty_collection = check_cache($empty_collection) do
209 act_as_system_user do
210 ActiveRecord::Base.transaction do
212 where(portable_data_hash: empty_collection_pdh).
213 first_or_create(manifest_text: '', owner_uuid: system_user.uuid, name: "empty collection") do |c|
215 Link.where(tail_uuid: anonymous_group.uuid,
217 link_class: 'permission',
227 # Purge the module globals if necessary. If the cached value is
228 # non-nil and the globals weren't purged, return the cached
229 # value. Otherwise, call the block.
231 # Purge is only done in test mode.
232 def check_cache(cached)
233 if Rails.env != 'test'
234 return (cached || yield)
236 t = Rails.cache.fetch "CurrentApiClient.$system_globals_reset" do
239 if t != $system_globals_reset
240 reset_system_globals(t)
247 def reset_system_globals(t)
248 $system_globals_reset = t
251 $all_users_group = nil
252 $anonymous_group = nil
253 $anonymous_group_read_permission = nil
254 $anonymous_user = nil
255 $public_project_group = nil
256 $public_project_group_read_permission = nil
257 $empty_collection = nil
259 module_function :reset_system_globals
262 CurrentApiClient.reset_system_globals(0)