diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 8c6ec672f..14c9872cc 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -233,14 +233,20 @@ def index def show # only show findable DOIs to anonymous users and role user # use current_user role to determine permissions to access draft and registered dois - options = filter_doi_by_role(current_user) - response = Doi.find_by_id(params[:id], options) - fail ActiveRecord::RecordNotFound unless response.results.present? + options = filter_doi_by_role(current_user).merge(doi: params[:id]) + puts options + if options[:provider_symbol] + doi = Doi.joins(:client, :provider).where(["dataset.doi = :doi and (allocator.symbol = :provider_symbol or dataset.aasm_state = 'findable')", options]).where(options).first + elsif options[:client_symbol] + doi = Doi.joins(:client).where(["dataset.doi = :doi and (datacentre.symbol = :client_symbol or dataset.aasm_state = 'findable')", options]).first + else + doi = Doi.where(options).first + end + + fail ActiveRecord::RecordNotFound unless doi.present? respond_to do |format| format.json do - doi = response.results.first - options = {} options[:include] = @include options[:is_collection] = false @@ -253,9 +259,6 @@ def show render json: DoiSerializer.new(doi, options).serialized_json, status: :ok end - # use active_record for content negotiation - doi = response.records.first - format.citation do # fetch formatted citation render citation: doi, style: params[:style] || "apa", locale: params[:locale] || "en-US" diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index a01de2e50..873deb577 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -144,21 +144,11 @@ def set_doi @doi = nil - if rand(2) == 0 - bm = Benchmark.ms { - @doi = Doi.where(doi: params[:id], aasm_state: "findable").first - } - fail ActiveRecord::RecordNotFound unless @doi.present? - logger.warn method: "GET", path: "/works/#{@doi.doi}", message: "Request DB /works/#{@doi.doi}", duration: bm - else - bm = Benchmark.ms { - options = filter_doi_by_role(current_user) - response = Doi.find_by_id(params[:id], options) - @doi = response.results.first - } - fail ActiveRecord::RecordNotFound unless @doi.present? - logger.warn method: "GET", path: "/works/#{@doi.doi}", message: "Request ES /works/#{@doi.doi}", duration: bm - end + bm = Benchmark.ms { + @doi = Doi.where(doi: params[:id], aasm_state: "findable").first + } + fail ActiveRecord::RecordNotFound unless @doi.present? + logger.warn method: "GET", path: "/works/#{@doi.doi}", message: "Request DB /works/#{@doi.doi}", duration: bm end def set_include diff --git a/app/models/concerns/authenticable.rb b/app/models/concerns/authenticable.rb index bcd415c2a..fd61c4505 100644 --- a/app/models/concerns/authenticable.rb +++ b/app/models/concerns/authenticable.rb @@ -155,16 +155,16 @@ def secure_compare(a, b) # filter results based on user permissions def filter_doi_by_role(user) - return { state: "findable" } if user.blank? + return { aasm_state: "findable" } if user.blank? if %w(staff_admin staff_user).include?(user.role_id) {} elsif %w(provider_admin provider_user).include?(user.role_id) && user.provider_id.present? - { provider_id: user.provider_id } + { :provider_symbol => user.provider_id.upcase } elsif %w(client_admin client_user user temporary).include?(user.role_id) && user.client_id.present? - { client_id: user.client_id } + { :client_symbol => user.client_id.upcase } else - { state: "findable" } + { aasm_state: "findable" } end end end diff --git a/spec/concerns/authenticable_spec.rb b/spec/concerns/authenticable_spec.rb index c76278f65..3e2031fdc 100644 --- a/spec/concerns/authenticable_spec.rb +++ b/spec/concerns/authenticable_spec.rb @@ -70,43 +70,43 @@ it "provider_admin" do token = User.generate_token(role_id: "provider_admin", provider_id: "datacite") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(:provider_id=>"datacite") + expect(subject.filter_doi_by_role(subject)).to eq(:provider_symbol=>"DATACITE") end it "provider_user" do token = User.generate_token(role_id: "provider_user", provider_id: "datacite") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(:provider_id=>"datacite") + expect(subject.filter_doi_by_role(subject)).to eq(:provider_symbol=>"DATACITE") end it "client_admin" do token = User.generate_token(role_id: "client_admin", client_id: "datacite.rph") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(client_id: "datacite.rph") + expect(subject.filter_doi_by_role(subject)).to eq(:client_symbol=>"DATACITE.RPH") end it "client_user" do token = User.generate_token(role_id: "client_user", client_id: "datacite.rph") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(client_id: "datacite.rph") + expect(subject.filter_doi_by_role(subject)).to eq(:client_symbol=>"DATACITE.RPH") end it "user" do token = User.generate_token(role_id: "user") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(:state=>"findable") + expect(subject.filter_doi_by_role(subject)).to eq(:aasm_state=>"findable") end it "temporary" do token = User.generate_token(role_id: "temporary") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(:state=>"findable") + expect(subject.filter_doi_by_role(subject)).to eq(:aasm_state=>"findable") end it "anonymous" do token = User.generate_token(role_id: "anonymous") subject = User.new(token) - expect(subject.filter_doi_by_role(subject)).to eq(:state=>"findable") + expect(subject.filter_doi_by_role(subject)).to eq(:aasm_state=>"findable") end end diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index bfa01641e..97354a40b 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -2818,15 +2818,14 @@ let(:bearer) { User.generate_token(role_id: "client_admin", client_id: client.symbol.downcase) } let(:headers) { { 'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Bearer ' + bearer } } - it 'returns without landing page results' do + it 'returns with landing page results' do get "/dois/#{doi.doi}", nil, headers expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi) - expect(json.dig('data', 'attributes', 'landingPage')).to be_nil + expect(json.dig('data', 'attributes', 'landingPage')).to eq(landing_page) end end - context 'client authorised try get diff dois landing data' do let(:bearer) { User.generate_token(role_id: "client_admin", client_id: client.symbol.downcase) } let(:headers) { { 'HTTP_ACCEPT'=>'application/vnd.api+json', 'HTTP_AUTHORIZATION' => 'Bearer ' + bearer } }