-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete access control object when destroying a resource
- Loading branch information
Showing
8 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
require 'dry/monads' | ||
|
||
module Hyrax | ||
module Transactions | ||
module Steps | ||
## | ||
# Deletes the Hyrax::AccessControlList for any resource with a `#permission_manager`. | ||
# If `#permission_manager` is undefined, succeeds. | ||
# | ||
# @see https://dry-rb.org/gems/dry-monads/1.0/result/ | ||
class DeleteAccessControl | ||
include Dry::Monads[:result] | ||
|
||
## | ||
# @param [Valkyrie::Resource] obj | ||
# | ||
# @return [Dry::Monads::Result] | ||
def call(obj) | ||
return Success(obj) unless obj.respond_to?(:permission_manager) | ||
|
||
acl = obj.permission_manager&.acl | ||
return Success(obj) if acl.nil? | ||
|
||
acl.destroy || (return Failure[:failed_to_delete_acl, acl]) | ||
|
||
Success(obj) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
spec/hyrax/transactions/steps/delete_access_control_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'dry/monads' | ||
require 'hyrax/transactions' | ||
|
||
RSpec.describe Hyrax::Transactions::Steps::DeleteAccessControl, valkyrie_adapter: :test_adapter do | ||
subject(:step) { described_class.new } | ||
let(:work) { FactoryBot.valkyrie_create(:hyrax_work) } | ||
|
||
context 'when acl has not been persisted' do | ||
it 'gives Success(obj) in basic case' do | ||
expect(step.call(work).value!).to eql(work) | ||
end | ||
end | ||
|
||
context 'when existing permissions exist' do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
before do | ||
work.permission_manager.read_users = [user.user_key] | ||
work.permission_manager.acl.save | ||
end | ||
|
||
it 'deletes the access control resource' do | ||
expect { step.call(work) } | ||
.to change { Hyrax::AccessControl.for(resource: work).persisted? } | ||
.from(true) | ||
.to(false) | ||
end | ||
|
||
context 'when it fails to destroy' do | ||
before { allow_any_instance_of(Hyrax::AccessControlList).to receive(:destroy).and_return(false) } | ||
|
||
it 'returns a Failure' do | ||
result = step.call(work) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to contain_exactly(Symbol, Hyrax::AccessControlList) | ||
end | ||
end | ||
end | ||
|
||
context 'when the resource has no permission_manager' do | ||
before do | ||
module Hyrax | ||
module Test | ||
module DeleteAccessControlStep | ||
class SimpleResource < Valkyrie::Resource | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
after { Hyrax::Test.send(:remove_const, :DeleteAccessControlStep) } | ||
|
||
let(:resource) { Hyrax.persister.save(resource: Hyrax::Test::DeleteAccessControlStep::SimpleResource.new) } | ||
|
||
it 'succeeds happily' do | ||
expect(step.call(work).value!).to eql(work) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters