Skip to content

Commit

Permalink
Transform and persist permissions passed as params from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar committed Sep 23, 2022
1 parent d03a1b7 commit b6726a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/hyrax/transactions/steps/save_access_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ class SaveAccessControl
# @param [Valkyrie::Resource] obj
#
# @return [Dry::Monads::Result]
def call(obj)
def call(obj, permissions_params: [])
return Success(obj) unless obj.respond_to?(:permission_manager)

acl = obj.permission_manager&.acl
# Translate step args into Hyrax::Permission objects before saving
Array(permissions_params).each do |param|
acl << param_to_permission(obj, param)
end

acl&.save || (return Failure[:failed_to_save_acl, acl])

Success(obj)
end

private

def param_to_permission(obj, param)
mode = param["access"].to_sym
agent = param["type"] == "group" ? "group/#{param['name']}" : param["name"]
Hyrax::Permission.new(access_to: obj.id, mode: mode, agent: agent)
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/hyrax/transactions/steps/save_access_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
expect(result.failure).to contain_exactly(Symbol, Hyrax::AccessControlList)
end
end

context 'when permissions params are passed' do
let(:params) { [{ "access" => "read", "type" => "group", "name" => "admin" }, { "access" => "edit", "type" => "person", "name" => user.user_key }] }

it 'transforms and persists the params' do
expect { step.call(work, permissions_params: params) }
.to change { Hyrax::AccessControlList.new(resource: work).permissions }
.to contain_exactly(have_attributes(mode: :read, agent: user.user_key),
have_attributes(mode: :edit, agent: user.user_key),
have_attributes(mode: :read, agent: 'group/admin'))
end
end
end

context 'when the resource has no permission_manager' do
Expand Down

0 comments on commit b6726a2

Please sign in to comment.