2961: Don't call #render_index from #index on folders_controller because #index
[arvados.git] / apps / workbench / app / controllers / collections_controller.rb
index 3b4943f5889a1910815417157c351aa5f1ad7704..6a5df8754ff9b69147e8f4669dba7f70df8a4d73 100644 (file)
@@ -88,6 +88,8 @@ class CollectionsController < ApplicationController
       info[:links] << link
     end
     @request_url = request.url
+
+    render_index
   end
 
   def show_file_links
@@ -120,6 +122,18 @@ class CollectionsController < ApplicationController
     self.response_body = file_enumerator opts
   end
 
+  def sharing_scopes
+    ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/collections/#{@object.uuid}/", "GET /arvados/v1/keep_services/accessible"]
+  end
+
+  def search_scopes
+    begin
+      ApiClientAuthorization.filter([['scopes', '=', sharing_scopes]]).results
+    rescue ArvadosApiClient::AccessForbiddenException
+      nil
+    end
+  end
+
   def show
     return super if !@object
     if current_user
@@ -129,10 +143,10 @@ class CollectionsController < ApplicationController
       end
       @output_of = jobs_with.call(output: @object.uuid)
       @log_of = jobs_with.call(log: @object.uuid)
-      folder_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
+      @folder_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
         .where(head_uuid: @object.uuid, link_class: 'name').results
-      folder_hash = Group.where(uuid: folder_links.map(&:tail_uuid)).to_hash
-      @folders = folder_links.map { |link| folder_hash[link.tail_uuid] }
+      folder_hash = Group.where(uuid: @folder_links.map(&:tail_uuid)).to_hash
+      @folders = @folder_links.map { |link| folder_hash[link.tail_uuid] }
       @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
         .where(head_uuid: @object.uuid, link_class: 'permission',
                name: 'can_read').results
@@ -142,6 +156,7 @@ class CollectionsController < ApplicationController
         .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
                link_class: 'resources', name: 'wants')
         .results.any?
+      @search_sharing = search_scopes
     end
     @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
                                                           {:request => request,
@@ -152,6 +167,37 @@ class CollectionsController < ApplicationController
                                                                :direction => :top_down,
                                                                :combine_jobs => :script_only,
                                                                :pdata_only => true}) rescue nil
+
+    super
+  end
+
+  def sharing_popup
+    @search_sharing = search_scopes
+    respond_to do |format|
+      format.html
+      format.js
+    end
+  end
+
+  helper_method :download_link
+
+  def download_link
+    collections_url + "/download/#{@object.uuid}/#{@search_sharing.first.api_token}"
+  end
+
+  def share
+    a = ApiClientAuthorization.create(scopes: sharing_scopes)
+    @search_sharing = search_scopes
+    render 'sharing_popup'
+  end
+
+  def unshare
+    @search_sharing = search_scopes
+    @search_sharing.each do |s|
+      s.destroy
+    end
+    @search_sharing = search_scopes
+    render 'sharing_popup'
   end
 
   protected