Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the location where the item is restricted to in the media auth… #1040

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/stacks_media_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def stacks_rights
end

delegate :rights, :cocina_rights, :restricted_by_location?, :stanford_restricted?, :embargoed?,
:embargo_release_date, to: :stacks_rights
:embargo_release_date, :location, to: :stacks_rights
end
4 changes: 4 additions & 0 deletions app/models/stacks_rights.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def cocina_rights
@cocina_rights ||= CocinaRights.new(cocina_file['access'])
end

def location
use_json? ? cocina_rights.location : dor_auth_rights.obj_lvl.location.keys.first
end

private

def cocina_file
Expand Down
16 changes: 14 additions & 2 deletions app/services/media_authentication_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def initialize(auth_url)
@result = { status: [] }
end

# Codes from https://github.com/sul-dlss/cocina-models/blob/8fc7b5b9b0e3592a5c81f4c0e4ebff5c926669c6/openapi.yml#L1330-L1339
# labels from https://consul.stanford.edu/display/chimera/Rights+Metadata+Locations
LOCATION_LABELS = {
'spec' => 'Special Collections reading room',
'music' => 'Music Library - main area',
'ars' => 'Archive of Recorded Sound listening room',
'art' => 'Art Library',
'hoover' => 'Hoover Library',
'm&m' => 'Media & Microtext'
}.freeze

attr_reader :result, :auth_url

def as_json
Expand All @@ -37,8 +48,9 @@ def add_stanford_restricted!
result[:service] = login_service
end

def add_location_restricted!
def add_location_restricted!(location)
add_status(:location_restricted)
result[:location] = { code: location, label: LOCATION_LABELS.fetch(location) }
end

def add_embargo!(embargo_release_date)
Expand All @@ -63,7 +75,7 @@ def login_service
def build_response
DenyResponse.new(auth_url).tap do |response|
response.add_stanford_restricted! if stanford_grants_access?
response.add_location_restricted! if location_grants_access?
response.add_location_restricted!(media.location) if location_grants_access?
response.add_embargo!(media.embargo_release_date) if embargoed?
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/requests/media_auth_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<rightsMetadata>
<access type="read">
<machine>
<location>location1</location>
<location>spec</location>
</machine>
</access>
</rightsMetadata>
Expand All @@ -73,6 +73,10 @@
it 'indicates that the object is location restricted in the json' do
get "/media/#{druid}/file.#{format}/auth_check"
expect(response.parsed_body['status']).to eq ['location_restricted']
expect(response.parsed_body).to eq(
'status' => %w[location_restricted],
'location' => { "code" => "spec", "label" => "Special Collections reading room" }
)
end
end

Expand All @@ -97,7 +101,6 @@
'embargo' => { 'release_date' => Time.parse('2099-05-15').getlocal.as_json },
'service' => { "@id" => "http://www.example.com/auth/iiif", "label" => "Stanford-affiliated? Login to play" }
)

end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/services/media_authentication_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
StacksMediaStream,
restricted_by_location?: false,
stanford_restricted?: false,
embargoed?: false
embargoed?: false,
location: 'm&m'
)
end
let(:ability) { Ability.new(user) }
let(:user) { double('User', locations: [], webauth_user: false, stanford?: false, app_user?: false, cdl_tokens: []) }
let(:user) { instance_double(User, locations: [], webauth_user: false, stanford?: false, cdl_tokens: []) }
subject { described_class.new(media:, user:, auth_url: '/the/auth/url', ability:) }

describe 'Location Restricted Media' do
Expand Down