1 class Arvados::V1::CollectionsController < ApplicationController
3 # Collections are owned by system_user. Creating a collection has
4 # two effects: The collection is added if it doesn't already
5 # exist, and a "permission" Link is added (if one doesn't already
6 # exist) giving the current user (or specified owner_uuid)
7 # permission to read it.
8 owner_uuid = resource_attrs.delete(:owner_uuid) || current_user.uuid
9 unless current_user.can? write: owner_uuid
10 logger.warn "User #{current_user.andand.uuid} tried to set collection owner_uuid to #{owner_uuid}"
11 raise ArvadosModel::PermissionDeniedError
14 @object = model_class.new resource_attrs.reject { |k,v| k == :owner_uuid }
17 rescue ActiveRecord::RecordNotUnique
18 logger.debug resource_attrs.inspect
19 if resource_attrs[:manifest_text] and resource_attrs[:uuid]
20 @existing_object = model_class.
21 where('uuid=? and manifest_text=?',
22 resource_attrs[:uuid],
23 resource_attrs[:manifest_text]).
25 @object = @existing_object || @object
31 owner_uuid: owner_uuid,
32 link_class: 'permission',
34 head_uuid: @object.uuid,
37 ActiveRecord::Base.transaction do
38 if Link.where(link_attrs).empty?
39 Link.create! link_attrs
48 render json: @object.as_api_response(:with_data)
51 def collection_uuid(uuid)
52 m = /([a-f0-9]{32}(\+[0-9]+)?)(\+.*)?/.match(uuid)
60 def script_param_edges(visited, sp)
64 script_param_edges(visited, v)
68 script_param_edges(visited, v)
72 m = collection_uuid(sp)
74 generate_provenance_edges(visited, m)
79 def generate_provenance_edges(visited, uuid)
80 m = collection_uuid(uuid)
83 if not uuid or uuid.empty? or visited[uuid]
87 logger.debug "visiting #{uuid}"
90 # uuid is a collection
91 Collection.readable_by(current_user).where(uuid: uuid).each do |c|
92 visited[uuid] = c.as_api_response
93 visited[uuid][:files] = []
95 visited[uuid][:files] << f
99 Job.readable_by(current_user).where(output: uuid).each do |job|
100 generate_provenance_edges(visited, job.uuid)
103 Job.readable_by(current_user).where(log: uuid).each do |job|
104 generate_provenance_edges(visited, job.uuid)
108 # uuid is something else
109 rsc = ArvadosModel::resource_class_for_uuid uuid
111 Job.readable_by(current_user).where(uuid: uuid).each do |job|
112 visited[uuid] = job.as_api_response
113 script_param_edges(visited, job.script_parameters)
116 rsc.where(uuid: uuid).each do |r|
117 visited[uuid] = r.as_api_response
122 Link.readable_by(current_user).
123 where(head_uuid: uuid, link_class: "provenance").
125 visited[link.uuid] = link.as_api_response
126 generate_provenance_edges(visited, link.tail_uuid)
129 #puts "finished #{uuid}"
134 generate_provenance_edges(visited, @object[:uuid])
138 def generate_used_by_edges(visited, uuid)
139 m = collection_uuid(uuid)
142 if not uuid or uuid.empty? or visited[uuid]
146 logger.debug "visiting #{uuid}"
149 # uuid is a collection
150 Collection.readable_by(current_user).where(uuid: uuid).each do |c|
151 visited[uuid] = c.as_api_response
152 visited[uuid][:files] = []
154 visited[uuid][:files] << f
158 if uuid == "d41d8cd98f00b204e9800998ecf8427e+0"
159 # special case for empty collection
163 Job.readable_by(current_user).where(["jobs.script_parameters like ?", "%#{uuid}%"]).each do |job|
164 generate_used_by_edges(visited, job.uuid)
168 # uuid is something else
169 rsc = ArvadosModel::resource_class_for_uuid uuid
171 Job.readable_by(current_user).where(uuid: uuid).each do |job|
172 visited[uuid] = job.as_api_response
173 generate_used_by_edges(visited, job.output)
176 rsc.where(uuid: uuid).each do |r|
177 visited[uuid] = r.as_api_response
182 Link.readable_by(current_user).
183 where(tail_uuid: uuid, link_class: "provenance").
185 visited[link.uuid] = link.as_api_response
186 generate_used_by_edges(visited, link.head_uuid)
189 #puts "finished #{uuid}"
194 generate_used_by_edges(visited, @object[:uuid])
199 def find_object_by_uuid
201 if !@object and !params[:uuid].match(/^[0-9a-f]+\+\d+$/)
202 # Normalize the given uuid and search again.
203 hash_part = params[:uuid].match(/^([0-9a-f]*)/)[1]
204 collection = Collection.where('uuid like ?', hash_part + '+%').first
206 # We know the collection exists, and what its real uuid is in
207 # the database. Now, throw out @objects and repeat the usual
208 # lookup procedure. (Returning the collection at this point
209 # would bypass permission checks.)
211 @where = { uuid: collection.uuid }
212 find_objects_for_index
213 @object = @objects.first