Skip to content

Commit

Permalink
Valkyrizes jobs that revoke edit permissions
Browse files Browse the repository at this point in the history
Allows jobs that revoke edit permissions from file set objects and from file sets
that are members of a work to use Valkyrie.

Refs #4102
Refs #4103
  • Loading branch information
afred committed Oct 21, 2019
1 parent f70459e commit f81515c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
14 changes: 10 additions & 4 deletions app/jobs/hyrax/revoke_edit_from_members_job.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Hyrax
# Revokes edit access for the supplied user for the members attached to a work
class RevokeEditFromMembersJob < ApplicationJob
class RevokeEditFromMembersJob < Hyrax::ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [ActiveFedora::Base] work - the work object
# @param [String] user_key - the user to remove
def perform(work, user_key)
# @param [Boolean] use_valkyrie - use valkyrie objects for this operation?
def perform(work, user_key, use_valkyrie: Hyrax.config.use_valkyrie?)
# Iterate over ids because reifying objects is slow.
file_set_ids(work).each do |file_set_id|
RevokeEditJob.perform_now(file_set_id, user_key)
RevokeEditJob.perform_now(file_set_id, user_key, use_valkyrie: use_valkyrie)
end
end

Expand All @@ -17,7 +18,12 @@ def perform(work, user_key)
# Filter the member ids and return only the FileSet ids (filter out child works)
# @return [Array<String>] the file set ids
def file_set_ids(work)
::FileSet.search_with_conditions(id: work.member_ids).map(&:id)
case work
when ActiveFedora::Base
::FileSet.search_with_conditions(id: work.member_ids).map(&:id)
when Valkyrie::Resource
Hyrax.query_service.custom_queries.find_child_fileset_ids(resource: work)
end
end
end
end
17 changes: 12 additions & 5 deletions app/jobs/hyrax/revoke_edit_job.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
module Hyrax
# Revokes the user's edit access on the provided FileSet
class RevokeEditJob < ApplicationJob
class RevokeEditJob < Hyrax::ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [String] file_set_id - the identifier of the object to revoke access from
# @param [String] user_key - the user to remove
def perform(file_set_id, user_key)
file_set = ::FileSet.find(file_set_id)
file_set.edit_users -= [user_key]
file_set.save!
def perform(file_set_id, user_key, use_valkyrie: Hyrax.config.use_valkyrie?)
if use_valkyrie
file_set_resource = Hyrax.query_service.find_by(id: file_set_id)
acl = Hyrax::AccessControlList.new(resource: file_set_resource)
acl.revoke(:edit).from(::User.find_by_user_key(user_key))
acl.save
else
file_set = ::FileSet.find(file_set_id)
file_set.edit_users -= [user_key]
file_set.save!
end
end
end
end
33 changes: 25 additions & 8 deletions spec/jobs/hyrax/revoke_edit_from_members_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
RSpec.describe Hyrax::RevokeEditFromMembersJob do
let(:depositor) { create(:user) }
let(:work) { build(:work) }
let(:file_set_ids) { ['xyz123abc', 'abc789zyx'] }

before do
allow_any_instance_of(described_class).to receive(:file_set_ids).with(work).and_return(file_set_ids)

context "when use_valkyrie is false" do
let(:work) { build(:work) }
let(:file_set_ids) { ['xyz123abc', 'abc789zyx'] }

before do
allow_any_instance_of(described_class).to receive(:file_set_ids).with(work).and_return(file_set_ids)
end

it 'loops over FileSet IDs, spawning a job for each' do
file_set_ids.each do |file_set_id|
expect(Hyrax::GrantReadJob).to receive(:perform_now).with(file_set_id, depositor.user_key, use_valkyre: false).once
end
described_class.perform_now(work, depositor.user_key, use_valkyrie: false)
end
end

it 'loops over FileSet IDs, spawning a job for each' do
file_set_ids.each do |file_set_id|
expect(Hyrax::RevokeEditJob).to receive(:perform_now).with(file_set_id, depositor.user_key).once
context "when use_valkyrie is true" do
let(:file_set1) { valkyrie_create(:hyrax_file_set) }
let(:file_set2) { valkyrie_create(:hyrax_file_set) }
let(:work) { valkyrie_create(:hyrax_work, member_ids: [file_set1.id, file_set2.id]) }

it 'loops over FileSet IDs, spawning a job for each' do
work.member_ids.each do |file_set_id|
expect(Hyrax::RevokeEditJob).to receive(:perform_now).with(file_set_id, depositor.user_key, use_valkyre: true).once
end
described_class.perform_now(work, depositor.user_key, use_valkyrie: true)
end
described_class.perform_now(work, depositor.user_key)
end
end
24 changes: 18 additions & 6 deletions spec/jobs/hyrax/revoke_edit_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
RSpec.describe Hyrax::RevokeEditJob do
let(:depositor) { create(:user) }
let(:file_set) { build(:file_set, user: depositor) }

it 'revokes edit access from a FileSet' do
expect(FileSet).to receive(:find).with(file_set.id).and_return(file_set)
expect(file_set).to receive(:edit_users=).with([])
expect(file_set).to receive(:save!)
described_class.perform_now(file_set.id, depositor.user_key)
context 'when use_valkyire is false' do
let(:file_set) { create(:file_set, user: depositor) }

it "revokes a user's edit access to a FileSet" do
described_class.perform_now(file_set.id, depositor.user_key, use_valkyrie: false)
file_set.reload
expect(file_set.edit_users).not_to include depositor.user_key
end
end

context 'when use_valkyire is true' do
let(:valk_file_set) { valkyrie_create(:hyrax_file_set) }

it "revokes a user's edit access to a FileSet" do
described_class.perform_now(valk_file_set.id.to_s, depositor.user_key, use_valkyrie: true)
reloaded_file_set = Hyrax.query_service.find_by(id: valk_file_set.id)
expect(reloaded_file_set.edit_users.to_a).not_to include depositor.user_key
end
end
end

0 comments on commit f81515c

Please sign in to comment.