1 class ProjectsController < ApplicationController
6 def find_object_by_uuid
7 if current_user and params[:uuid] == current_user.uuid
8 @object = current_user.dup
9 @object.uuid = current_user.uuid
17 def attribute_editable? attr, *args
19 when 'description', 'name'
37 %w(Data_collections Jobs_and_pipelines Pipeline_templates Subprojects Other_objects Sharing Advanced)
39 %w(Data_collections Jobs_and_pipelines Pipeline_templates Subprojects Other_objects Advanced)
44 params[:item_uuids] = [params[:item_uuid]]
46 render template: 'projects/remove_items'
52 params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
53 if (item.class == Link and
54 item.link_class == 'name' and
55 item.tail_uuid == @object.uuid)
56 # Given uuid is a name link, linking an object to this
57 # project. First follow the link to find the item we're removing,
58 # then delete the link.
60 item = ArvadosBase.find item.head_uuid
62 # Given uuid is an object. Delete all names.
63 links += Link.where(tail_uuid: @object.uuid,
68 @removed_uuids << link.uuid
71 if item.owner_uuid == @object.uuid
72 # Object is owned by this project. Remove it from the project by
73 # changing owner to the current user.
74 item.update_attributes owner_uuid: current_user.uuid
75 @removed_uuids << item.uuid
81 move_or_copy_items :copy
85 move_or_copy_items :move
88 def move_or_copy_items action
89 target_uuid = params['target']
90 uuids_to_add = session[:selected_move_or_copy_items]
93 collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
95 each do |resource_class|
96 resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
97 if resource_class == Collection and not Collection.attribute_info.include?(:name)
98 dst = Link.new(owner_uuid: target_uuid,
99 tail_uuid: target_uuid,
107 if dst.respond_to? :'name='
109 dst.name = "Copy of #{dst.name}"
111 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
114 if resource_class == Collection
115 dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
120 raise ArgumentError.new "Unsupported action #{action}"
122 dst.owner_uuid = target_uuid
123 dst.tail_uuid = target_uuid if dst.class == Link
128 session[:selected_move_or_copy_items] = nil
133 while (objects = Link.filter([['owner_uuid','=',@object.uuid],
134 ['tail_uuid','=',@object.uuid]])).any?
135 objects.each do |object|
139 while (objects = @object.contents(include_linked: false)).any?
140 objects.each do |object|
141 object.update_attributes! owner_uuid: current_user.uuid
144 if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
145 params[:return_to] ||= group_path(@object.owner_uuid)
147 params[:return_to] ||= projects_path
152 def find_objects_for_index
153 @objects = all_projects
157 def load_contents_objects kinds=[]
158 kind_filters = @filters.select do |attr,op,val|
159 op == 'is_a' and val.is_a? Array and val.count > 1
161 if /^created_at\b/ =~ @order[0] and kind_filters.count == 1
162 # If filtering on multiple types and sorting by date: Get the
163 # first page of each type, sort the entire set, truncate to one
164 # page, and use the last item on this page as a filter for
165 # retrieving the next page. Ideally the API would do this for
166 # us, but it doesn't (yet).
167 nextpage_operator = /\bdesc$/i =~ @order[0] ? '<' : '>'
170 kind_filters.each do |attr,op,val|
171 (val.is_a?(Array) ? val : [val]).each do |type|
172 objects = @object.contents(order: @order,
174 include_linked: true,
175 filters: (@filters - kind_filters + [['uuid', 'is_a', type]]),
177 objects.each do |object|
178 @name_link_for[object.andand.uuid] = objects.links_for(object, 'name').first
183 @objects = @objects.to_a.sort_by(&:created_at)
184 @objects.reverse! if nextpage_operator == '<'
185 @objects = @objects[0..@limit-1]
186 @next_page_filters = @filters.reject do |attr,op,val|
187 attr == 'created_at' and op == nextpage_operator
190 @next_page_filters += [['created_at',
192 @objects.last.created_at]]
193 @next_page_href = url_for(partial: :contents_rows,
194 filters: @next_page_filters.to_json)
196 @next_page_href = nil
199 @objects = @object.contents(order: @order,
201 include_linked: true,
204 @next_page_href = next_page_href(partial: :contents_rows)
207 preload_links_for_objects(@objects.to_a)
212 return render_not_found("object not found")
215 @user_is_manager = false
217 if @object.uuid != current_user.uuid
219 @share_links = Link.permissions_for(@object)
220 @user_is_manager = true
221 rescue ArvadosApiClient::AccessForbiddenException,
222 ArvadosApiClient::NotFoundException
227 load_contents_objects
231 content: render_to_string(partial: 'show_contents_rows.html',
233 next_page_href: @next_page_href
244 @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
245 @new_resource_attrs[:name] ||= 'New project'
250 @updates = params['project']
254 helper_method :get_objects_and_names
255 def get_objects_and_names(objects=nil)
256 objects = @objects if objects.nil?
257 objects_and_names = []
258 objects.each do |object|
259 if objects.respond_to? :links_for and
260 !(name_links = objects.links_for(object, 'name')).empty?
261 name_links.each do |name_link|
262 objects_and_names << [object, name_link]
264 elsif @name_link_for.andand[object.uuid]
265 objects_and_names << [object, @name_link_for[object.uuid]]
266 elsif object.respond_to? :name
267 objects_and_names << [object, object]
268 elsif not Collection.attribute_info.include?(:name)
269 objects_and_names << [object,
270 Link.new(owner_uuid: @object.uuid,
271 tail_uuid: @object.uuid,
272 head_uuid: object.uuid,
281 if not params[:uuids].andand.any?
282 @errors = ["No user/group UUIDs specified to share with."]
283 return render_error(status: 422)
285 results = {"success" => [], "errors" => []}
286 params[:uuids].each do |shared_uuid|
288 Link.create(tail_uuid: shared_uuid, link_class: "permission",
289 name: "can_read", head_uuid: @object.uuid)
290 rescue ArvadosApiClient::ApiError => error
291 error_list = error.api_response.andand[:errors]
292 if error_list.andand.any?
293 results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
295 error_code = error.api_status || "Bad status"
296 results["errors"] << "#{shared_uuid}: #{error_code} response"
299 results["success"] << shared_uuid
302 if results["errors"].empty?
303 results.delete("errors")
309 f.json { render(json: results, status: status) }