Skip to content

Commit

Permalink
use activerecord to look up single dois. #386
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 17, 2020
1 parent 1a4bac1 commit faba1aa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
19 changes: 11 additions & 8 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
20 changes: 5 additions & 15 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/models/concerns/authenticable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions spec/concerns/authenticable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down

0 comments on commit faba1aa

Please sign in to comment.