Sha256: 2d9009589cf35ce0f327fc7ed2daee45637f1a7756de18dd74b545c8ce63e0f7

Contents?: true

Size: 1.75 KB

Versions: 31

Compression:

Stored size: 1.75 KB

Contents

module Ddr::Models
  #
  # Blacklight SearchBuilder methods.
  #
  # Include in controller search builder class:
  #
  #   class SearchBuilder < Blacklight::Solr::SearchBuilder
  #     include Ddr::Models::SearchBuilder
  #   end
  #
  module SearchBuilder

    # @note Copied from Hydra::AccessControlsEnforcement
    def apply_gated_discovery(solr_parameters)
      solr_parameters[:fq] ||= []
      solr_parameters[:fq] << gated_discovery_filters.join(" OR ")
    end

    def current_ability
      # :scope is assumed here to be a controller which responds to :current_ability
      scope.current_ability
    end

    def gated_discovery_filters
      [resource_role_filters, policy_role_filters].compact
    end

    # List of IDs for policies on which any of the current user's agent has a role in policy scope
    def policy_role_policies
      @policy_role_policies ||= Array.new.tap do |uris|
        filters = current_ability.agents.map do |agent|
          "#{Ddr::Index::Fields::POLICY_ROLE}:\"#{agent}\""
        end.join(" OR ")
        query = "#{Ddr::Index::Fields::ACTIVE_FEDORA_MODEL}:Collection AND (#{filters})"
        results = ActiveFedora::SolrService.query(query, rows: Collection.count, fl: Ddr::Index::Fields::ID)
        results.each_with_object(uris) { |r, memo| memo << r[Ddr::Index::Fields::ID] }
      end
    end

    def policy_role_filters
      if policy_role_policies.present?
        rels = policy_role_policies.map { |pid| [:isGovernedBy, pid] }
        ActiveFedora::SolrQueryBuilder.construct_query_for_rel(rels, " OR ")
      end
    end

    def resource_role_filters
      current_ability.agents.map do |agent|
        ActiveFedora::SolrQueryBuilder.raw_query(Ddr::Index::Fields::RESOURCE_ROLE, agent)
      end.join(" OR ")
    end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ddr-models-3.0.8 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.7 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.6 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.5 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.4 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.3 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.2 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.1 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc6 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc5 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc4 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc3 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc2 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.rc1 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.beta.22 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.beta.21 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.beta.20 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.beta.18 lib/ddr/models/search/search_builder.rb
ddr-models-3.0.0.beta.17 lib/ddr/models/search/search_builder.rb