From 6f5bd10b8b4bce3c3e3e4d136ddb25326aa72ce6 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Mon, 31 Mar 2014 11:54:29 -0700 Subject: [PATCH] re-unify bookmark#index and bookmark#export (and, while we're at it, add the other catalog#index response formats --- app/controllers/bookmarks_controller.rb | 47 ++++++++++--------- app/helpers/bookmarks_helper.rb | 7 +-- lib/blacklight/routes.rb | 1 - spec/controllers/bookmarks_controller_spec.rb | 4 +- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 5498ce403d..12e44e535b 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -20,41 +20,31 @@ def search_action_url *args catalog_index_url *args end - before_filter :verify_user, :except => :export + before_filter :verify_user def index - @bookmarks = current_or_guest_user.bookmarks + @bookmarks = token_or_current_or_guest_user.bookmarks bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s } @response, @document_list = get_solr_response_for_document_ids(bookmark_ids) respond_to do |format| format.html { } + format.rss { render :layout => false } + format.atom { render :layout => false } + format.json do + render json: render_search_results_as_json + end + + additional_response_formats(format) + format.endnote do # Just concatenate individual endnote exports with blank lines. Not # every record can be exported as endnote -- only include those that # can. render :text => @document_list.collect {|d| d.export_as(:endnote) if d.export_formats.keys.include? :endnote}.join("\n"), :layout => false end - end - end - # Much like #index, but does NOT require authentication, instead - # gets an _encrypted and signed_ user_id in params, and it delivers - # that users bookmarks, in some export format. - # - # Used for third party services requiring callback urls, such as refworks, - # that need to export a certain users bookmarks without being auth'd as - # that user. - def export - user_id = decrypt_user_id params[:encrypted_user_id] - - @bookmarks = User.find(user_id).bookmarks - bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s } - - @response, @document_list = get_solr_response_for_field_values(SolrDocument.unique_key, bookmark_ids) - - respond_to do |format| format.refworks_marc_txt do # Just concatenate individual refworks_marc_txt exports with blank lines. Not # every record can be exported as refworks_marc_txt -- only include those that @@ -134,7 +124,9 @@ def clear protected def verify_user - flash[:notice] = I18n.t('blacklight.bookmarks.need_login') and raise Blacklight::Exceptions::AccessDenied unless current_or_guest_user + unless current_or_guest_user or (action == "index" and token_or_current_or_guest_user) + flash[:notice] = I18n.t('blacklight.bookmarks.need_login') and raise Blacklight::Exceptions::AccessDenied + end end def start_new_search_session? @@ -171,4 +163,17 @@ def message_encryptor derived_secret = bookmarks_export_secret_token("bookmarks session key") ActiveSupport::MessageEncryptor.new(derived_secret) end + + def token_or_current_or_guest_user + token_user || current_or_guest_user + end + + def token_user + @token_user ||= if params[:encrypted_user_id] + user_id = decrypt_user_id params[:encrypted_user_id] + User.find(user_id) + else + nil + end + end end diff --git a/app/helpers/bookmarks_helper.rb b/app/helpers/bookmarks_helper.rb index 9776e5201f..2731ee3def 100644 --- a/app/helpers/bookmarks_helper.rb +++ b/app/helpers/bookmarks_helper.rb @@ -5,11 +5,8 @@ module BookmarksHelper # user's bookmarks in 'refworks marc txt' format -- we tell refworks # to expect that format. def bookmarks_refworks_export_url(user_id = current_or_guest_user.id) - callback_url = export_bookmarks_callback_url( - encrypt_user_id(current_or_guest_user.id), - :refworks_marc_txt, - params_for_search ) + callback_url = bookmarks_url(params_for_search.merge(format: :refworks_marc_txt, encrypted_user_id: encrypt_user_id(current_or_guest_user.id) )) "http://www.refworks.com/express/expressimport.asp?vendor=#{CGI.escape(application_name)}&filter=MARC%20Format&encoding=65001&url=#{CGI.escape(callback_url)}" end -end \ No newline at end of file +end diff --git a/lib/blacklight/routes.rb b/lib/blacklight/routes.rb index 77a673f620..6e6fdc93ee 100644 --- a/lib/blacklight/routes.rb +++ b/lib/blacklight/routes.rb @@ -61,7 +61,6 @@ module RouteSets def bookmarks(_) add_routes do |options| delete "bookmarks/clear", :to => "bookmarks#clear", :as => "clear_bookmarks" - get "bookmarks/export/:encrypted_user_id", :to => "bookmarks#export", :as => "export_bookmarks_callback" resources :bookmarks end end diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb index e18eda5431..f17659878e 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -32,7 +32,7 @@ end end - describe "export" do + describe ".refworks format" do render_views before(:all) do @@ -52,7 +52,7 @@ User.should_receive(:find).with(user_id).and_return(@user_with_3) - get :export, :format => :refworks_marc_txt, :encrypted_user_id => encrypted_user_id + get :index, :format => :refworks_marc_txt, :encrypted_user_id => encrypted_user_id expect(response.code).to eq "200" # For some reason having trouble getting actual doc.export_as(:refworks_marc_txt)