Skip to content

Commit a7031c7

Browse files
committed
Remove code for authorizing replace of polymorphic associations
There is no way jsonapi-authorization can work if the code upstream isn't fixed. The specs will fail because of the way jsonapi-resources handles polymorphic association loading.
1 parent 7c31a6e commit a7031c7

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Or install it yourself as:
5858

5959
We aim to support the same Ruby and Ruby on Rails versions as `jsonapi-resources` does. If that's not the case, please [open an issue][issues].
6060

61+
> ### NOTE: Replacing polymorphic associations is BROKEN
62+
>
63+
> This is because of an issue in `jsonapi-resources` gem itself: https://github.com/cerebris/jsonapi-resources/issues/1305
64+
>
65+
> This gem will always raise an error if an operation is tried which would replace a polymorphic association as allowing the operation to continue would not be possible to authorize against.
66+
6167
## Versioning and changelog
6268

6369
`jsonapi-authorization` follows [Semantic Versioning](https://semver.org/). We prefer to make more major version bumps when we do changes that are likely to be backwards incompatible. That holds true even when it's likely the changes would be backwards compatible for a majority of our users.

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -250,36 +250,9 @@ def authorize_remove_to_one_relationship
250250
end
251251

252252
def authorize_replace_polymorphic_to_one_relationship
253-
return authorize_remove_to_one_relationship if params[:key_value].nil?
254-
255-
source_resource = @resource_klass.find_by_key(
256-
params[:resource_id],
257-
context: context
258-
)
259-
source_record = source_resource._model
260-
261-
# Fetch the name of the new class based on the incoming polymorphic
262-
# "type" value. This will fail if there is no associated resource for the
263-
# incoming "type" value so this shouldn't leak constants
264-
related_record_class_name = source_resource
265-
.send(:_model_class_name, params[:key_type])
266-
267-
# Fetch the underlying Resource class for the new record to-be-associated
268-
related_resource_klass = @resource_klass.resource_for(related_record_class_name)
269-
270-
new_related_resource = related_resource_klass
271-
.find_by_key(
272-
params[:key_value],
273-
context: context
274-
)
275-
new_related_record = new_related_resource._model unless new_related_resource.nil?
276-
277-
relationship_type = params[:relationship_type].to_sym
278-
authorizer.replace_to_one_relationship(
279-
source_record: source_record,
280-
new_related_record: new_related_record,
281-
relationship_type: relationship_type
282-
)
253+
# rubocop:disable Layout/LineLength
254+
raise NotImplementedError, "Finding polymorphic associations is broken in jsonapi-resources and thus jsonapi-authorization can't work: https://github.com/cerebris/jsonapi-resources/issues/1305"
255+
# rubocop:enable Layout/LineLength
283256
end
284257

285258
private

spec/requests/relationship_operations_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@
281281
end
282282

283283
# Polymorphic has-one relationship replacing
284-
describe 'PATCH /tags/:id/relationships/taggable' do
284+
# Polymorphic associations are broken: https://github.com/cerebris/jsonapi-resources/issues/1305
285+
describe 'PATCH /tags/:id/relationships/taggable', pending: 'Broken upstream' do
285286
subject(:last_response) { patch("/tags/#{tag.id}/relationships/taggable", json) }
286287

287288
let!(:old_taggable) { Comment.create }
@@ -308,7 +309,7 @@
308309
JSON
309310
end
310311

311-
context 'unauthorized for replace_to_one_relationship', pending: 'Compatibility with JR 0.10' do
312+
context 'unauthorized for replace_to_one_relationship' do
312313
before do
313314
disallow_operation(
314315
'replace_to_one_relationship',
@@ -320,7 +321,7 @@
320321
it { is_expected.to be_forbidden }
321322
end
322323

323-
context 'authorized for replace_to_one_relationship', pending: 'Compatibility with JR 0.10' do
324+
context 'authorized for replace_to_one_relationship' do
324325
before do
325326
allow_operation(
326327
'replace_to_one_relationship',
@@ -338,7 +339,7 @@
338339

339340
# If this happens in real life, it's mostly a bug. We want to document the
340341
# behaviour in that case anyway, as it might be surprising.
341-
context 'limited by policy scope on tag', pending: false do
342+
context 'limited by policy scope on tag' do
342343
let(:tag_policy_scope) { Tag.where.not(id: tag.id) }
343344
it { is_expected.to be_not_found }
344345
end

0 commit comments

Comments
 (0)