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'
30 @user_is_manager = false
32 if @object.uuid != current_user.uuid
34 @share_links = Link.permissions_for(@object)
35 @user_is_manager = true
36 rescue ArvadosApiClient::AccessForbiddenException,
37 ArvadosApiClient::NotFoundException
46 # Returning an array of hashes instead of an array of strings will allow
47 # us to tell the interface to get counts for each pane (using :filters).
48 # It also seems to me that something like these could be used to configure the contents of the panes.
52 :name => 'Data_collections',
53 :filters => [%w(uuid is_a arvados#collection)]
56 :name => 'Jobs_and_pipelines',
57 :filters => [%w(uuid is_a) + [%w(arvados#job arvados#pipelineInstance)]]
60 :name => 'Pipeline_templates',
61 :filters => [%w(uuid is_a arvados#pipelineTemplate)]
64 :name => 'Subprojects',
65 :filters => [%w(uuid is_a arvados#group)]
67 { :name => 'Other_objects',
68 :filters => [%w(uuid is_a) + [%w(arvados#human arvados#specimen arvados#trait)]]
71 # Note that adding :filters to 'Sharing' won't help show the count for it because @user_is_manager is only set in #show
72 # Therefore if a count were desired there we'd want to set @user_is_manager in a before_filter or somesuch.
73 pane_list << { :name => 'Sharing',
74 :count => @share_links.count } if @user_is_manager
75 pane_list << { :name => 'Advanced' }
78 # Called via AJAX and returns Javascript that populates tab counts into tab titles.
79 # References #show_pane_list action which should return an array of hashes each with :name
80 # and then optionally a :filters to run or a straight up :count
82 # This action could easily be moved to the ApplicationController to genericize the tab_counts behaviour,
83 # but one or more new routes would have to be created, the js.erb would also have to be moved
86 show_pane_list.each do |pane|
89 @tab_counts[pane[:name]] = pane[:count]
91 @tab_counts[pane[:name]] = @object.contents(filters: pane[:filters]).items_available
98 params[:item_uuids] = [params[:item_uuid]]
100 render template: 'projects/remove_items'
106 params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
107 if (item.class == Link and
108 item.link_class == 'name' and
109 item.tail_uuid == @object.uuid)
110 # Given uuid is a name link, linking an object to this
111 # project. First follow the link to find the item we're removing,
112 # then delete the link.
114 item = ArvadosBase.find item.head_uuid
116 # Given uuid is an object. Delete all names.
117 links += Link.where(tail_uuid: @object.uuid,
118 head_uuid: item.uuid,
122 @removed_uuids << link.uuid
125 if item.owner_uuid == @object.uuid
126 # Object is owned by this project. Remove it from the project by
127 # changing owner to the current user.
128 item.update_attributes owner_uuid: current_user.uuid
129 @removed_uuids << item.uuid
135 while (objects = Link.filter([['owner_uuid','=',@object.uuid],
136 ['tail_uuid','=',@object.uuid]])).any?
137 objects.each do |object|
141 while (objects = @object.contents(include_linked: false)).any?
142 objects.each do |object|
143 object.update_attributes! owner_uuid: current_user.uuid
146 if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
147 params[:return_to] ||= group_path(@object.owner_uuid)
149 params[:return_to] ||= projects_path
154 def find_objects_for_index
155 @objects = all_projects
159 def load_contents_objects kinds=[]
160 kind_filters = @filters.select do |attr,op,val|
161 op == 'is_a' and val.is_a? Array and val.count > 1
163 if /^created_at\b/ =~ @order[0] and kind_filters.count == 1
164 # If filtering on multiple types and sorting by date: Get the
165 # first page of each type, sort the entire set, truncate to one
166 # page, and use the last item on this page as a filter for
167 # retrieving the next page. Ideally the API would do this for
168 # us, but it doesn't (yet).
169 nextpage_operator = /\bdesc$/i =~ @order[0] ? '<' : '>'
172 kind_filters.each do |attr,op,val|
173 (val.is_a?(Array) ? val : [val]).each do |type|
174 objects = @object.contents(order: @order,
176 include_linked: true,
177 filters: (@filters - kind_filters + [['uuid', 'is_a', type]]),
179 objects.each do |object|
180 @name_link_for[object.andand.uuid] = objects.links_for(object, 'name').first
185 @objects = @objects.to_a.sort_by(&:created_at)
186 @objects.reverse! if nextpage_operator == '<'
187 @objects = @objects[0..@limit-1]
188 @next_page_filters = @filters.reject do |attr,op,val|
189 attr == 'created_at' and op == nextpage_operator
192 @next_page_filters += [['created_at',
194 @objects.last.created_at]]
195 @next_page_href = url_for(partial: :contents_rows,
196 filters: @next_page_filters.to_json)
198 @next_page_href = nil
201 @objects = @object.contents(order: @order,
203 include_linked: true,
206 @next_page_href = next_page_href(partial: :contents_rows)
209 preload_links_for_objects(@objects.to_a)
214 return render_not_found("object not found")
218 load_contents_objects
222 content: render_to_string(partial: 'show_contents_rows.html',
224 next_page_href: @next_page_href
235 @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
236 @new_resource_attrs[:name] ||= 'New project'
241 @updates = params['project']
245 helper_method :get_objects_and_names
246 def get_objects_and_names(objects=nil)
247 objects = @objects if objects.nil?
248 objects_and_names = []
249 objects.each do |object|
250 if objects.respond_to? :links_for and
251 !(name_links = objects.links_for(object, 'name')).empty?
252 name_links.each do |name_link|
253 objects_and_names << [object, name_link]
255 elsif @name_link_for.andand[object.uuid]
256 objects_and_names << [object, @name_link_for[object.uuid]]
257 elsif object.respond_to? :name
258 objects_and_names << [object, object]
260 objects_and_names << [object,
261 Link.new(owner_uuid: @object.uuid,
262 tail_uuid: @object.uuid,
263 head_uuid: object.uuid,
273 if not params[:uuids].andand.any?
274 @errors = ["No user/group UUIDs specified to share with."]
275 return render_error(status: 422)
277 results = {"success" => [], "errors" => []}
278 params[:uuids].each do |shared_uuid|
280 Link.create(tail_uuid: shared_uuid, link_class: "permission",
281 name: "can_read", head_uuid: @object.uuid)
282 rescue ArvadosApiClient::ApiError => error
283 error_list = error.api_response.andand[:errors]
284 if error_list.andand.any?
285 results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
287 error_code = error.api_status || "Bad status"
288 results["errors"] << "#{shared_uuid}: #{error_code} response"
291 results["success"] << shared_uuid
294 if results["errors"].empty?
295 results.delete("errors")
301 f.json { render(json: results, status: status) }