6 $anonymous_group_read_permission = nil
7 $empty_collection = nil
9 module CurrentApiClient
14 def current_api_client
15 Thread.current[:api_client]
18 def current_api_client_authorization
19 Thread.current[:api_client_authorization]
23 Thread.current[:api_url_base]
26 def current_default_owner
27 # owner_uuid for newly created objects
28 ((current_api_client_authorization &&
29 current_api_client_authorization.default_owner_uuid) ||
30 (current_user && current_user.default_owner_uuid) ||
31 (current_user && current_user.uuid) ||
35 # Where is the client connecting from?
36 def current_api_client_ip_address
37 Thread.current[:api_client_ip_address]
41 [Server::Application.config.uuid_prefix,
43 '000000000000000'].join('-')
47 [Server::Application.config.uuid_prefix,
49 '000000000000000'].join('-')
52 def anonymous_group_uuid
53 [Server::Application.config.uuid_prefix,
55 'anonymouspublic'].join('-')
58 def anonymous_user_uuid
59 [Server::Application.config.uuid_prefix,
61 'anonymouspublic'].join('-')
65 $system_user = check_cache $system_user do
66 real_current_user = Thread.current[:user]
68 Thread.current[:user] = User.new(is_admin: true,
70 uuid: system_user_uuid)
71 User.where(uuid: system_user_uuid).
72 first_or_create!(is_active: true,
78 Thread.current[:user] = real_current_user
84 $system_group = check_cache $system_group do
86 ActiveRecord::Base.transaction do
87 Group.where(uuid: system_group_uuid).
88 first_or_create!(name: "System group",
89 description: "System group") do |g|
91 User.all.collect(&:uuid).each do |user_uuid|
92 Link.create!(link_class: 'permission',
94 tail_kind: 'arvados#group',
95 tail_uuid: system_group_uuid,
96 head_kind: 'arvados#user',
105 def all_users_group_uuid
106 [Server::Application.config.uuid_prefix,
108 'fffffffffffffff'].join('-')
112 $all_users_group = check_cache $all_users_group do
113 act_as_system_user do
114 ActiveRecord::Base.transaction do
115 Group.where(uuid: all_users_group_uuid).
116 first_or_create!(name: "All users",
117 description: "All users",
124 def act_as_system_user
126 act_as_user system_user do
130 Thread.current[:user] = system_user
135 #auth_was = Thread.current[:api_client_authorization]
136 user_was = Thread.current[:user]
137 Thread.current[:user] = user
138 #Thread.current[:api_client_authorization] = ApiClientAuthorization.
139 # where('user_id=? and scopes is null', user.id).
140 # order('expires_at desc').
145 Thread.current[:user] = user_was
146 #Thread.current[:api_client_authorization] = auth_was
151 $anonymous_group = check_cache $anonymous_group do
152 act_as_system_user do
153 ActiveRecord::Base.transaction do
154 Group.where(uuid: anonymous_group_uuid).
155 first_or_create!(group_class: "role",
156 name: "Anonymous users",
157 description: "Anonymous users")
163 def anonymous_group_read_permission
164 $anonymous_group_read_permission =
165 check_cache $anonymous_group_read_permission do
166 act_as_system_user do
167 Link.where(tail_uuid: all_users_group.uuid,
168 head_uuid: anonymous_group.uuid,
169 link_class: "permission",
170 name: "can_read").first_or_create!
176 $anonymous_user = check_cache $anonymous_user do
177 act_as_system_user do
178 User.where(uuid: anonymous_user_uuid).
179 first_or_create!(is_active: false,
182 first_name: 'Anonymous',
183 last_name: '') do |u|
185 Link.where(tail_uuid: anonymous_user_uuid,
186 head_uuid: anonymous_group.uuid,
187 link_class: 'permission',
195 def empty_collection_uuid
196 'd41d8cd98f00b204e9800998ecf8427e+0'
200 $empty_collection = check_cache $empty_collection do
201 act_as_system_user do
202 ActiveRecord::Base.transaction do
204 where(portable_data_hash: empty_collection_uuid).
205 first_or_create!(manifest_text: '', owner_uuid: anonymous_group.uuid)
213 # If the given value is nil, or the cache has been cleared since it
214 # was set, yield. Otherwise, return the given value.
215 def check_cache value
216 if not Rails.env.test? and
217 ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
218 not File.owned? ActionController::Base.cache_store.cache_path
219 # If we don't own the cache dir, we're probably
220 # crunch-dispatch. Whoever we are, using this cache is likely to
221 # either fail or screw up the cache for someone else. So we'll
222 # just assume the $globals are OK to live forever.
224 # The reason for making the globals expire with the cache in the
225 # first place is to avoid leaking state between test cases: in
226 # production, we don't expect the database seeds to ever go away
227 # even when the cache is cleared, so there's no particular
228 # reason to expire our global variables.
230 Rails.cache.fetch "CurrentApiClient.$globals" do
235 return value unless value.nil?