Skip to content

Commit

Permalink
Merge pull request #6502 from samvera/valk-abilities
Browse files Browse the repository at this point in the history
Add support for valkyrie resources to Ability
  • Loading branch information
abelemlih authored Dec 5, 2023
2 parents 2314c7c + 51f18cf commit 5b2dc51
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
2 changes: 2 additions & 0 deletions app/models/concerns/hyrax/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module Ability
include Hyrax::Ability::CollectionAbility
include Hyrax::Ability::CollectionTypeAbility
include Hyrax::Ability::PermissionTemplateAbility
include Hyrax::Ability::ResourceAbility
include Hyrax::Ability::SolrDocumentAbility

class_attribute :admin_group_name, :registered_group_name, :public_group_name
Expand All @@ -76,6 +77,7 @@ module Ability
:collection_abilities,
:collection_type_abilities,
:permission_template_abilities,
:resource_abilities,
:solr_document_abilities,
:trophy_abilities]
end
Expand Down
19 changes: 19 additions & 0 deletions app/models/concerns/hyrax/ability/resource_ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true
module Hyrax
module Ability
module ResourceAbility
def resource_abilities
if admin?
can [:manage], ::Hyrax::Resource
else
can [:edit, :update, :destroy], ::Hyrax::Resource do |res|
test_edit(res.id)
end
can :read, ::Hyrax::Resource do |res|
test_read(res.id)
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/abilities/file_set_abilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
let(:creating_user) { create(:user) }
let(:user) { create(:user) }
let(:current_user) { user }
let(:generic_work) { create(:generic_work, visibility: visibility, user: creating_user) }
let(:file_set) { create(:file_set, visibility: visibility, user: creating_user) }
let(:generic_work) { valkyrie_create(:hyrax_work, visibility_setting: visibility, edit_users: [creating_user]) }
let(:file_set) { valkyrie_create(:hyrax_file_set, visibility_setting: visibility, edit_users: [creating_user]) }

describe 'without embargo' do
describe 'creator of object' do
Expand Down
49 changes: 11 additions & 38 deletions spec/abilities/permission_template_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
let(:user) { create(:user) }
let(:current_user) { user }
let(:collection_type) { FactoryBot.create(:collection_type) }
let!(:collection) { create(:collection_lw, with_permission_template: true, collection_type: collection_type) }
let(:permission_template) { collection.permission_template }
let!(:permission_template_access) do
create(:permission_template_access,
:manage,
permission_template: permission_template,
agent_type: 'group',
agent_id: 'manage_group')
let!(:collection) { valkyrie_create(:hyrax_collection, collection_type: collection_type, access_grants: [access_grant]) }
let(:access_grant) { { agent_type: 'group', access: 'manage', agent_id: 'manage_group' } }
let(:permission_template) { Hyrax::PermissionTemplate.find_by(source_id: collection.id) }
let(:permission_template_access) do
Hyrax::PermissionTemplateAccess.find_by(permission_template_id: permission_template.id.to_s,
agent_type: access_grant[:agent_type],
agent_id: access_grant[:agent_id],
access: access_grant[:access])
end

context 'when admin user' do
Expand All @@ -37,16 +37,7 @@
end

context 'when user has manage access for the source' do
before do
create(:permission_template_access,
:manage,
permission_template: permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.permission_template.reset_access_controls_for(
collection: collection, interpret_visibility: true
)
end
let(:access_grant) { { agent_type: 'user', access: 'manage', agent_id: user.user_key } }

it 'allows most template abilities' do
is_expected.to be_able_to(:create, permission_template)
Expand All @@ -72,16 +63,7 @@
end

context 'when user has deposit access for the source' do
before do
create(:permission_template_access,
:deposit,
permission_template: permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.permission_template.reset_access_controls_for(
collection: collection, interpret_visibility: true
)
end
let(:access_grant) { { agent_type: 'user', access: 'deposit', agent_id: user.user_key } }

it 'denies all template abilities' do
is_expected.not_to be_able_to(:manage, Hyrax::PermissionTemplate)
Expand All @@ -101,16 +83,7 @@
end

context 'when user has view access for the source' do
before do
create(:permission_template_access,
:view,
permission_template: permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.permission_template.reset_access_controls_for(
collection: collection, interpret_visibility: true
)
end
let(:access_grant) { { agent_type: 'user', access: 'view', agent_id: user.user_key } }

it 'denies all template abilities' do
is_expected.not_to be_able_to(:manage, Hyrax::PermissionTemplate)
Expand Down

0 comments on commit 5b2dc51

Please sign in to comment.