X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1bb9f9ca3ffe077f2d90d7de65fbee37a717ef23..1db007eb53d0401a7a0ba168add7c4a094790fa5:/apps/workbench/app/controllers/collections_controller.rb?ds=sidebyside diff --git a/apps/workbench/app/controllers/collections_controller.rb b/apps/workbench/app/controllers/collections_controller.rb index 370c681f07..1d710419da 100644 --- a/apps/workbench/app/controllers/collections_controller.rb +++ b/apps/workbench/app/controllers/collections_controller.rb @@ -1,7 +1,8 @@ class CollectionsController < ApplicationController - skip_around_filter :thread_with_mandatory_api_token, only: [:show_file] - skip_before_filter :find_object_by_uuid, only: [:provenance, :show_file] - skip_before_filter :check_user_agreements, only: [:show_file] + skip_around_filter(:thread_with_mandatory_api_token, + only: [:show_file, :show_file_links]) + skip_before_filter(:find_object_by_uuid, + only: [:provenance, :show_file, :show_file_links]) RELATION_LIMIT = 5 @@ -89,13 +90,20 @@ class CollectionsController < ApplicationController @request_url = request.url end + def show_file_links + Thread.current[:reader_tokens] = [params[:reader_token]] + find_object_by_uuid + render layout: false + end + def show_file # We pipe from arv-get to send the file to the user. Before we start it, # we ask the API server if the file actually exists. This serves two # purposes: it lets us return a useful status code for common errors, and # helps us figure out which token to provide to arv-get. coll = nil - usable_token = find_usable_token do + tokens = [Thread.current[:arvados_api_token], params[:reader_token]].compact + usable_token = find_usable_token(tokens) do coll = Collection.find(params[:uuid]) end if usable_token.nil? @@ -112,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 @@ -134,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, @@ -146,20 +163,45 @@ 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 - # Iterate over every token available to make it the current token and + def find_usable_token(token_list) + # Iterate over every given token to make it the current token and # yield the given block. # If the block succeeds, return the token it used. # Otherwise, render an error response based on the most specific # error we encounter, and return nil. - read_tokens = [Thread.current[:arvados_api_token]].compact - if params[:reader_tokens].is_a? Array - read_tokens += params[:reader_tokens] - end most_specific_error = [401] - read_tokens.each do |api_token| + token_list.each do |api_token| using_specific_api_token(api_token) do begin yield @@ -203,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