Skip to content

Commit 577a8a5

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 14a4adb commit 577a8a5

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-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: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -247,36 +247,7 @@ def authorize_remove_to_one_relationship
247247
end
248248

249249
def authorize_replace_polymorphic_to_one_relationship
250-
return authorize_remove_to_one_relationship if params[:key_value].nil?
251-
252-
source_resource = @resource_klass.find_by_key(
253-
params[:resource_id],
254-
context: context
255-
)
256-
source_record = source_resource._model
257-
258-
# Fetch the name of the new class based on the incoming polymorphic
259-
# "type" value. This will fail if there is no associated resource for the
260-
# incoming "type" value so this shouldn't leak constants
261-
related_record_class_name = source_resource
262-
.send(:_model_class_name, params[:key_type])
263-
264-
# Fetch the underlying Resource class for the new record to-be-associated
265-
related_resource_klass = @resource_klass.resource_for(related_record_class_name)
266-
267-
new_related_resource = related_resource_klass
268-
.find_by_key(
269-
params[:key_value],
270-
context: context
271-
)
272-
new_related_record = new_related_resource._model unless new_related_resource.nil?
273-
274-
relationship_type = params[:relationship_type].to_sym
275-
authorizer.replace_to_one_relationship(
276-
source_record: source_record,
277-
new_related_record: new_related_record,
278-
relationship_type: relationship_type
279-
)
250+
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"
280251
end
281252

282253
private

spec/requests/relationship_operations_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@
279279
end
280280

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

285286
let!(:old_taggable) { Comment.create }
@@ -306,7 +307,7 @@
306307
EOS
307308
end
308309

309-
context 'unauthorized for replace_to_one_relationship', pending: 'Compatibility with JR 0.10' do
310+
context 'unauthorized for replace_to_one_relationship' do
310311
before {
311312
disallow_operation(
312313
'replace_to_one_relationship',
@@ -318,7 +319,7 @@
318319
it { is_expected.to be_forbidden }
319320
end
320321

321-
context 'authorized for replace_to_one_relationship', pending: 'Compatibility with JR 0.10' do
322+
context 'authorized for replace_to_one_relationship' do
322323
before {
323324
allow_operation(
324325
'replace_to_one_relationship',
@@ -336,7 +337,7 @@
336337

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

0 commit comments

Comments
 (0)