2044: Sharing link now shown in "Sharing and permissions" panel, no longer uses
[arvados.git] / apps / workbench / app / controllers / collections_controller.rb
index 7af552ded9c644e5bec88aaae65efb7d1ae6a07b..1d710419dad09a55810a2abba05a5693812adb7f 100644 (file)
@@ -120,6 +120,14 @@ class CollectionsController < ApplicationController
     self.response_body = file_enumerator opts
   end
 
+  def sharing_scopes
+    ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/keep_services"]
+  end
+
+  def search_scopes
+    ApiClientAuthorization.where(filters: [['scopes', '=', sharing_scopes]])
+  end
+
   def show
     return super if !@object
     if current_user
@@ -142,6 +150,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.select { |s| s.scopes != ['all'] }
     end
     @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
                                                           {:request => request,
@@ -154,6 +163,35 @@ class CollectionsController < ApplicationController
                                                                :pdata_only => true}) rescue nil
   end
 
+  def sharing_popup
+    @search_sharing = search_scopes.select { |s| s.scopes != ['all'] }
+    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.select { |s| s.scopes != ['all'] }
+    render 'sharing_popup'
+  end
+
+  def unshare
+    @search_sharing = search_scopes.select { |s| s.scopes != ['all'] }
+    @search_sharing.each do |s|
+      s.destroy
+    end
+    @search_sharing = search_scopes.select { |s| s.scopes != ['all'] }
+    render 'sharing_popup'
+  end
+
   protected
 
   def find_usable_token(token_list)
@@ -207,20 +245,18 @@ class CollectionsController < ApplicationController
     end
     def each
       return unless @opts[:uuid] && @opts[:file]
-      env = Hash[ENV].
-        merge({
-                'ARVADOS_API_HOST' =>
-                arvados_api_client.arvados_v1_base.
-                sub(/\/arvados\/v1/, '').
-                sub(/^https?:\/\//, ''),
-                'ARVADOS_API_TOKEN' =>
-                @opts[:arvados_api_token],
-                'ARVADOS_API_HOST_INSECURE' =>
-                Rails.configuration.arvados_insecure_https ? 'true' : 'false'
-              })
+
+      env = Hash[ENV].dup
+
+      require 'uri'
+      u = URI.parse(arvados_api_client.arvados_v1_base)
+      env['ARVADOS_API_HOST'] = "#{u.host}:#{u.port}"
+      env['ARVADOS_API_TOKEN'] = @opts[:arvados_api_token]
+      env['ARVADOS_API_HOST_INSECURE'] = "true" if Rails.configuration.arvados_insecure_https
+
       IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
                'rb') do |io|
-        while buf = io.read(2**20)
+        while buf = io.read(2**16)
           yield buf
         end
       end