Skip to content

Commit

Permalink
Revert "Bump Blacklight-access_controls to 6.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar committed Aug 19, 2019
1 parent c662924 commit 2d0de57
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 30 deletions.
4 changes: 2 additions & 2 deletions hydra-access-controls/hydra-access-controls.gemspec
Expand Up @@ -20,8 +20,8 @@ Gem::Specification.new do |gem|

gem.add_dependency 'activesupport', '>= 4', '< 6'
gem.add_dependency "active-fedora", '>= 10.0.0', '< 13'
gem.add_dependency "blacklight", '>= 5.16', '< 7'
gem.add_dependency "blacklight-access_controls", '~> 6.0'
gem.add_dependency "blacklight", '>= 5.16'
gem.add_dependency "blacklight-access_controls", '~> 0.6.0'
gem.add_dependency 'cancancan', '~> 1.8'
gem.add_dependency 'deprecation', '~> 1.0'

Expand Down
6 changes: 4 additions & 2 deletions hydra-access-controls/lib/active_fedora/accessible_by.rb
@@ -1,5 +1,8 @@
ActiveFedora::QueryMethods.module_eval do
extend ActiveSupport::Concern
included do
include Hydra::AccessControlsEnforcement
end

def accessible_by(ability, action = :index)
permission_types = case action
Expand All @@ -8,8 +11,7 @@ def accessible_by(ability, action = :index)
when :update, :edit, :create, :new, :destroy then [:edit]
end

builder = Hydra::SearchBuilder.new(nil, ability: ability, permission_types: permission_types)
filters = builder.send(:gated_discovery_filters).join(" OR ")
filters = gated_discovery_filters(permission_types, ability).join(" OR ")
spawn.where!(filters)
end
end
Expand Down
@@ -1,5 +1,6 @@
module Hydra::AccessControlsEnforcement
extend ActiveSupport::Concern
include Blacklight::AccessControls::Enforcement

protected

Expand Down
Expand Up @@ -32,7 +32,7 @@ def policies_with_access
# @param [Array{String,#to_sym}] permission_types symbols (or equivalent) from Hydra.config.permissions.inheritable
def apply_policy_group_permissions(permission_types = discovery_permissions)
user_access_filters = []
ability.user_groups.each_with_index do |group, i|
current_ability.user_groups.each_with_index do |group, i|
permission_types.each do |type|
user_access_filters << escape_filter(Hydra.config.permissions.inheritable[type.to_sym].group, group)
end
Expand All @@ -43,7 +43,7 @@ def apply_policy_group_permissions(permission_types = discovery_permissions)
# for individual user access
# @param [Array{String,#to_sym}] permission_types
def apply_policy_user_permissions(permission_types = discovery_permissions)
user = ability.current_user
user = current_ability.current_user
return [] unless user && user.user_key.present?
permission_types.map do |type|
escape_filter(Hydra.config.permissions.inheritable[type.to_sym].individual, user.user_key)
Expand Down
@@ -1,15 +1,28 @@
require 'spec_helper'

RSpec.describe Hydra::PolicyAwareAccessControlsEnforcement do
describe Hydra::PolicyAwareAccessControlsEnforcement do
before do
allow(Devise).to receive(:authentication_keys).and_return(['uid'])

class PolicyMockSearchBuilder < Hydra::SearchBuilder
class PolicyMockSearchBuilder < Blacklight::SearchBuilder
include Blacklight::Solr::SearchBuilderBehavior
include Hydra::AccessControlsEnforcement
include Hydra::PolicyAwareAccessControlsEnforcement
attr_accessor :params

def initialize(current_ability)
@current_ability = current_ability
end

def current_ability
@current_ability
end

def session
end

delegate :logger, to: :Rails
end

@sample_policies = []
# user discover
policy1 = Hydra::AdminPolicy.create(id: "test-policy1")
Expand Down Expand Up @@ -78,7 +91,7 @@ class PolicyMockSearchBuilder < Hydra::SearchBuilder
end

let(:current_ability) { Ability.new(user) }
subject { PolicyMockSearchBuilder.new(nil, ability: current_ability) }
subject { PolicyMockSearchBuilder.new(current_ability) }
let(:user) { FactoryBot.build(:sara_student) }

before do
Expand Down Expand Up @@ -121,15 +134,15 @@ class PolicyMockSearchBuilder < Hydra::SearchBuilder

context "when policies are included" do
before { subject.apply_gated_discovery(@solr_parameters) }

it "builds a query that includes all the policies" do
skip if ActiveFedora.version.split('.').first.to_i < 11
(1..11).each do |p|
expect(policy_queries).to include(/_query_:\"{!raw f=#{governed_field}}test-policy#{p}\"/)
end
end
end

context "when policies are not included" do
before do
allow(subject).to receive(:policy_clauses).and_return(nil)
Expand Down
5 changes: 0 additions & 5 deletions hydra-core/app/controllers/concerns/hydra/catalog.rb
Expand Up @@ -20,9 +20,4 @@ def enforce_show_permissions(opts={})

permissions_doc
end

# This overrides the method in Blacklight::AccessControls::Catalog
def search_builder
Hydra::SearchBuilder.new(self, ability: current_ability)
end
end
6 changes: 0 additions & 6 deletions hydra-core/app/models/hydra/search_builder.rb

This file was deleted.

12 changes: 10 additions & 2 deletions hydra-core/lib/generators/hydra/head_generator.rb
Expand Up @@ -49,6 +49,14 @@ def inject_hydra_controller_behavior
end
end

# Add Hydra to the SearchBuilder
def inject_hydra_search_builder_behavior
insert_into_file "app/models/search_builder.rb", after: "include Blacklight::Solr::SearchBuilderBehavior\n" do
" # Add a filter query to restrict the search to documents the current user has access to\n" \
" include Hydra::AccessControlsEnforcement\n"
end
end

# Copy all files in templates/config directory to host config
def create_configuration_files

Expand All @@ -71,7 +79,7 @@ def create_configuration_files
def create_conneg_configuration
file_path = "config/initializers/mime_types.rb"
append_to_file file_path do
"Mime::Type.register \"application/n-triples\", :nt\n" +
"Mime::Type.register \"application/n-triples\", :nt\n" +
"Mime::Type.register \"application/ld+json\", :jsonld\n" +
"Mime::Type.register \"text/turtle\", :ttl"
end
Expand All @@ -81,7 +89,7 @@ def inject_solr_document_conneg
file_path = "app/models/solr_document.rb"
if File.exists?(file_path)
inject_into_file file_path, :before => /end\Z/ do
"\n # Do content negotiation for AF models. \n" +
"\n # Do content negotiation for AF models. \n" +
"\n use_extension( Hydra::ContentNegotiation )\n"
end
end
Expand Down
9 changes: 5 additions & 4 deletions hydra-core/spec/search_builders/search_builder_spec.rb
@@ -1,13 +1,14 @@
require 'spec_helper'

RSpec.describe Hydra::SearchBuilder do
let(:context) { CatalogController.new }
describe SearchBuilder do
let(:processor_chain) { [:add_access_controls_to_solr_params] }
let(:context) { double('context') }
let(:user) { double('user', user_key: 'joe') }
let(:current_ability) { double('ability', user_groups: [], current_user: user) }
let(:search_builder) { described_class }

subject do
search_builder.new(context, ability: current_ability)
search_builder.new(processor_chain, context)
end

it "extends classes with the necessary Hydra modules" do
Expand All @@ -16,7 +17,7 @@

context "when a query is generated" do
it "triggers add_access_controls_to_solr_params" do
expect(subject).to receive(:apply_gated_discovery)
expect(subject).to receive(:add_access_controls_to_solr_params)
subject.query
end
end
Expand Down
Expand Up @@ -5,7 +5,6 @@ class TestAppGenerator < Rails::Generators::Base
def install_blacklight
# we can skip solr because the activefedora installer will also do it (in the hydra:head generator)
generate 'blacklight:install --devise --skip-solr'
gem 'rsolr', '>= 1.0', '< 3'
end

# if you need to generate any additional configuration
Expand Down

0 comments on commit 2d0de57

Please sign in to comment.