Skip to content

Commit

Permalink
Access-granting workflow services should work on member file sets
Browse files Browse the repository at this point in the history
Fixes #1477
  • Loading branch information
mjgiarlo committed Aug 10, 2017
1 parent 99c36b3 commit fab61e0
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/jobs/hyrax/grant_edit_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Hyrax
# Grants the user's edit access on the provided FileSet
class GrantEditJob < ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [String] file_set_id - the identifier of the object to grant access to
# @param [String] user_key - the user to add
def perform(file_set_id, user_key)
file_set = ::FileSet.find(file_set_id)
file_set.edit_users += [user_key]
file_set.save!
end
end
end
24 changes: 24 additions & 0 deletions app/jobs/hyrax/grant_edit_to_members_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Hyrax
# Grants edit access for the supplied user for the members attached to a work
class GrantEditToMembersJob < ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [ActiveFedora::Base] work - the work object
# @param [String] user_key - the user to add
def perform(work, user_key)
# Iterate over ids because reifying objects is slow.
file_set_ids(work).each do |file_set_id|
# Call this synchronously, since we're already in a job
GrantEditJob.perform_now(file_set_id, user_key)
end
end

private

# 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)
end
end
end
14 changes: 14 additions & 0 deletions app/jobs/hyrax/grant_read_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Hyrax
# Grants the user's read access on the provided FileSet
class GrantReadJob < ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [String] file_set_id - the identifier of the object to grant access to
# @param [String] user_key - the user to add
def perform(file_set_id, user_key)
file_set = ::FileSet.find(file_set_id)
file_set.read_users += [user_key]
file_set.save!
end
end
end
24 changes: 24 additions & 0 deletions app/jobs/hyrax/grant_read_to_members_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Hyrax
# Grants read access for the supplied user for the members attached to a work
class GrantReadToMembersJob < ApplicationJob
queue_as Hyrax.config.ingest_queue_name

# @param [ActiveFedora::Base] work - the work object
# @param [String] user_key - the user to add
def perform(work, user_key)
# Iterate over ids because reifying objects is slow.
file_set_ids(work).each do |file_set_id|
# Call this synchronously, since we're already in a job
GrantReadJob.perform_now(file_set_id, user_key)
end
end

private

# 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)
end
end
end
5 changes: 5 additions & 0 deletions app/services/hyrax/workflow/grant_edit_to_depositor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ module Workflow
# This is a built in function for workflow, so that a workflow action can be created that
# grants the creator the ability to alter it.
module GrantEditToDepositor
# @param [#read_users=, #read_users] target (likely an ActiveRecord::Base) to which we are adding edit_users for the depositor
# @return void
def self.call(target:, **)
target.edit_users += [target.depositor]
# If there are a lot of members, granting access to each could take a
# long time. Do this work in the background.
GrantEditToMembersJob.perform_later(target, target.depositor)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/services/hyrax/workflow/grant_read_to_depositor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module GrantReadToDepositor
# @return void
def self.call(target:, **)
target.read_users += [target.depositor]
# If there are a lot of members, granting access to each could take a
# long time. Do this work in the background.
GrantReadToMembersJob.perform_later(target, target.depositor)
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/services/hyrax/workflow/grant_edit_to_depositor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,21 @@
expect(work).to be_valid
end
end

context "with attached FileSets" do
let(:work) { create(:work_with_one_file, user: depositor) }
let(:file_set) do
work.members.first.tap do |file_set|
# Manually remove edit_users to satisfy the pre-condition
file_set.update(edit_users: [])
end
end

it "grants edit access" do
# We need to reload, because this work happens in a background job
expect { subject }.to change { file_set.reload.edit_users }.from([]).to([depositor.user_key])
expect(work).to be_valid
end
end
end
end
17 changes: 17 additions & 0 deletions spec/services/hyrax/workflow/grant_read_to_depositor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
require 'hyrax/specs/shared_specs'

RSpec.describe Hyrax::Workflow::GrantReadToDepositor do
let(:depositor) { create(:user) }
let(:user) { User.new }

let(:workflow_method) { described_class }

it_behaves_like 'a Hyrax workflow method'

describe ".call" do
subject do
described_class.call(target: work,
Expand All @@ -27,5 +33,16 @@
expect(work).to be_valid
end
end

context "with attached FileSets" do
let(:work) { create(:work_with_one_file, user: depositor) }
let(:file_set) { work.members.first }

it "grants read access" do
# We need to reload, because this work happens in a background job
expect { subject }.to change { file_set.reload.read_users }.from([]).to([depositor.user_key])
expect(work).to be_valid
end
end
end
end

0 comments on commit fab61e0

Please sign in to comment.