Skip to content

Commit 0022e8f

Browse files
committed
Fix authorize_show_related_resource
1 parent 555e907 commit 0022e8f

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def authorize_show_relationship
7272
related_resource =
7373
case relationship
7474
when JSONAPI::Relationship::ToOne
75-
parent_resource.public_send(params[:relationship_type].to_sym)
75+
resources_from_relationship(source_klass, source_id, relationship.type, context).first
7676
when JSONAPI::Relationship::ToMany
7777
# Do nothing — already covered by policy scopes
7878
else
@@ -91,10 +91,13 @@ def authorize_show_related_resource
9191

9292
source_resource = source_klass.find_by_key(source_id, context: context)
9393

94-
related_resource = source_resource.public_send(relationship_type)
94+
related_resource = resources_from_relationship(
95+
source_klass, source_id, relationship_type, context
96+
)&.first
9597

9698
source_record = source_resource._model
9799
related_record = related_resource._model unless related_resource.nil?
100+
98101
authorizer.show_related_resource(
99102
source_record: source_record, related_record: related_record
100103
)
@@ -282,6 +285,18 @@ def authorizer
282285
@authorizer ||= ::JSONAPI::Authorization.configuration.authorizer.new(context: context)
283286
end
284287

288+
def resources_from_relationship(source_klass, source_id, relationship_type, context)
289+
rid = source_klass.find_related_fragments(
290+
[JSONAPI::ResourceIdentity.new(source_klass, source_id)],
291+
relationship_type,
292+
context: context
293+
).keys.first
294+
295+
return nil if rid.nil?
296+
297+
rid.resource_klass.find_to_populate_by_keys(rid.id)
298+
end
299+
285300
# TODO: Communicate with upstream to fix this nasty hack
286301
def operation_resource_id
287302
case operation_type

lib/jsonapi/authorization/pundit_scoped_resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module PunditScopedResource
88
module ClassMethods
99
def records(options = {})
1010
user_context = JSONAPI::Authorization.configuration.user_context(options[:context])
11-
::Pundit.policy_scope!(user_context, _model_class)
11+
::Pundit.policy_scope!(user_context, super)
1212
end
1313
end
1414

spec/requests/custom_name_relationship_operations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
RSpec.describe 'including custom name relationships', type: :request, pending: 'compatibility with JR 0.10' do
3+
RSpec.describe 'including custom name relationships', type: :request do
44
include AuthorizationStubs
55
fixtures :all
66

spec/requests/included_resources_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@
531531
include_examples :scope_limited_directive_tests
532532
end
533533

534-
describe 'GET /articles/:id/article', pending: true do
534+
describe 'GET /articles/:id/article' do
535535
let(:article) {
536536
Article.create(
537537
external_id: "indifferent_external_id",

spec/requests/related_resources_operations_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@
5858
subject(:last_response) { get("/articles/#{article.external_id}/author") }
5959
let(:article) { articles(:article_with_author) }
6060
let(:policy_scope) { Article.all }
61+
let(:user_policy_scope) { User.all }
6162

62-
context 'unauthorized for show_related_resource', pending: 'Compatibility with JR 0.10' do
63+
before do
64+
allow_any_instance_of(UserPolicy::Scope).to receive(:resolve).and_return(user_policy_scope)
65+
end
66+
67+
context 'unauthorized for show_related_resource' do
6368
before { disallow_operation('show_related_resource', source_record: article, related_record: article.author) }
6469
it { is_expected.to be_forbidden }
6570
end
6671

6772
context 'authorized for show_related_resource' do
6873
before { allow_operation('show_related_resource', source_record: article, related_record: article.author) }
74+
it { is_expected.to be_ok }
6975

7076
# If this happens in real life, it's mostly a bug. We want to document the
7177
# behaviour in that case anyway, as it might be surprising.
@@ -74,5 +80,13 @@
7480
it { is_expected.to be_not_found }
7581
end
7682
end
83+
84+
context 'authorized for show_related_resource while related resource is limited by policy scope' do
85+
before { allow_operation('show_related_resource', source_record: article, related_record: nil) }
86+
87+
let(:user_policy_scope) { User.where.not(id: article.author.id) }
88+
89+
it { is_expected.to be_ok }
90+
end
7791
end
7892
end

0 commit comments

Comments
 (0)