class FedoraMigrate::RightsMetadata

Implements Hydra RightsMetadata XML terminology for asserting access permissions

Public Class Methods

date_indexer() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 242
def self.date_indexer
  @date_indexer ||= Solrizer::Descriptor.new(:date, :stored, :indexed)
end
indexer() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 234
def self.indexer
  @indexer ||= Solrizer::Descriptor.new(:string, :stored, :indexed, :multivalued)
end
xml_template() click to toggle source

Generates an empty Mods Article (used when you call ModsArticle.new without passing in existing xml)

# File lib/fedora_migrate/rights_metadata.rb, line 77
def self.xml_template
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.rightsMetadata(:version => "0.1", "xmlns" => "http://hydra-collab.stanford.edu/schemas/rightsMetadata/v1") do
      xml.copyright do
        xml.human(type: 'title')
        xml.human(type: 'description')
        xml.machine(type: 'uri')
      end
      xml.access(type: "discover") do
        xml.human
        xml.machine
      end
      xml.access(type: "read") do
        xml.human
        xml.machine
      end
      xml.access(type: "edit") do
        xml.human
        xml.machine
      end
      xml.embargo do
        xml.machine
      end
      xml.lease do
        xml.machine
      end
    end
  end
  builder.doc
end

Public Instance Methods

active_lease?() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 202
def active_lease?
  lease_expiration_date.present? && Date.today < lease_expiration_date.first
end
clear_permissions!() click to toggle source

Completely clear the permissions

# File lib/fedora_migrate/rights_metadata.rb, line 247
def clear_permissions!
  remove_all_permissions(person: true)
  remove_all_permissions(group: true)
end
date_indexer() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 238
def date_indexer
  self.class.date_indexer
end
groups() click to toggle source

Reports on which groups have which permissions @return Hash in format {group_name => group_permissions, group_name => group_permissions}

# File lib/fedora_migrate/rights_metadata.rb, line 148
def groups
  quick_search_by_type(:group)
end
indexer() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 230
def indexer
  self.class.indexer
end
individuals() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 152
def individuals
  Deprecation.warn(RightsMetadata, "The method `individuals' is deprecated and will be removed from Hydra::Datastream::RightsMetadata in hydra-head 8.0.  Use `users' instead.", caller)
  users
end
permissions(selector, new_access_level = nil) click to toggle source

Returns the permissions for the selected person/group If new_access_level is provided, updates the selected person/group access_level to the one specified A new_access_level of “none” will remove all access_levels for the selected person/group @param [Hash] selector hash in format {type => identifier} @param new_access_level (default nil) @return Hash in format {type => access_level}.

ie. permissions({:person=>“person123”})

> {“person123”=>“edit”}

permissions({:person=>“person123”}, “read”)

> {“person123”=>“read”}

permissions({:person=>“person123”})

> {“person123”=>“read”}

# File lib/fedora_migrate/rights_metadata.rb, line 122
def permissions(selector, new_access_level = nil)
  type = selector.keys.first.to_sym
  actor = selector.values.first
  if new_access_level.nil?
    xpath = xpath(type, actor)
    nodeset = find_by_terms(xpath)
    if nodeset.empty?
      return "none"
    else
      return nodeset.first.ancestors("access").first.attributes["type"].text
    end
  else
    remove_all_permissions(selector)
    if new_access_level == "none"
      self.content = to_xml
    else
      access_type_symbol = "#{new_access_level}_access".to_sym
      current_values = term_values(access_type_symbol, type)
      update_values([access_type_symbol, type] => current_values + [actor])
    end
    return new_access_level
  end
end
permissions=(params) click to toggle source

Updates all permissions @param params ex. {“group”=>{“group1”=>“discover”,“group2”=>“edit”}, “person”=>{“person1”=>“read”,“person2”=>“discover”}} Restricts actor type to group or person. Any others will be ignored

# File lib/fedora_migrate/rights_metadata.rb, line 174
def permissions=(params)
  groups_for_update = params['group'] ? params['group'].keys : []
  group_ids = groups.keys | groups_for_update
  group_ids.each { |group_id| permissions({ "group" => group_id }, params['group'].fetch(group_id, 'none')) }
  users_for_update = params['person'] ? params['person'].keys : []
  user_ids = users.keys | users_for_update
  user_ids.each { |person_id| permissions({ "person" => person_id }, params['person'].fetch(person_id, 'none')) }
end
quick_search_by_type(type) click to toggle source

@param [Symbol] type (either :group or :person) @return This method limits the response to known access levels. Probably runs a bit faster than .permissions().

# File lib/fedora_migrate/rights_metadata.rb, line 186
def quick_search_by_type(type)
  result = {}
  [{ discover_access: "discover" }, { read_access: "read" }, { edit_access: "edit" }].each do |access_levels_hash|
    access_level = access_levels_hash.keys.first
    access_level_name = access_levels_hash.values.first
    find_by_terms(*[access_level, type]).each do |entry|
      result[entry.text] = access_level_name
    end
  end
  result
end
to_solr(solr_doc = {}) click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 206
def to_solr(solr_doc = {})
  [:discover, :read, :edit].each do |access|
    vals = send("#{access}_access").machine.group
    solr_doc[Hydra.config.permissions[access].group] = vals unless vals.empty?
    vals = send("#{access}_access").machine.person
    solr_doc[Hydra.config.permissions[access].individual] = vals unless vals.empty?
  end
  if embargo_release_date.present?
    key = Hydra.config.permissions.embargo.release_date.sub(/_[^_]+$/, '') # Strip off the suffix
    ::Solrizer.insert_field(solr_doc, key, embargo_release_date, :stored_sortable)
  end
  if lease_expiration_date.present?
    key = Hydra.config.permissions.lease.expiration_date.sub(/_[^_]+$/, '') # Strip off the suffix
    ::Solrizer.insert_field(solr_doc, key, lease_expiration_date, :stored_sortable)
  end
  solr_doc[::Solrizer.solr_name("visibility_during_embargo", :symbol)] = visibility_during_embargo unless visibility_during_embargo.nil?
  solr_doc[::Solrizer.solr_name("visibility_after_embargo", :symbol)] = visibility_after_embargo unless visibility_after_embargo.nil?
  solr_doc[::Solrizer.solr_name("visibility_during_lease", :symbol)] = visibility_during_lease unless visibility_during_lease.nil?
  solr_doc[::Solrizer.solr_name("visibility_after_lease", :symbol)] = visibility_after_lease unless visibility_after_lease.nil?
  solr_doc[::Solrizer.solr_name("embargo_history", :symbol)] = embargo_history unless embargo_history.nil?
  solr_doc[::Solrizer.solr_name("lease_history", :symbol)] = lease_history unless lease_history.nil?
  solr_doc
end
under_embargo?() click to toggle source
# File lib/fedora_migrate/rights_metadata.rb, line 198
def under_embargo?
  (embargo_release_date.present? && Date.today < embargo_release_date.first) ? true : false
end
update_permissions(params) click to toggle source

Updates permissions for all of the persons and groups in a hash @param params ex. {“group”=>{“group1”=>“discover”,“group2”=>“edit”}, “person”=>{“person1”=>“read”,“person2”=>“discover”}} Currently restricts actor type to group or person. Any others will be ignored

# File lib/fedora_migrate/rights_metadata.rb, line 166
def update_permissions(params)
  params.fetch("group", {}).each_pair { |group_id, access_level| permissions({ "group" => group_id }, access_level) }
  params.fetch("person", {}).each_pair { |person_id, access_level| permissions({ "person" => person_id }, access_level) }
end
users() click to toggle source

Reports on which users have which permissions @return Hash in format {user_name => user_permissions, user_name => user_permissions}

# File lib/fedora_migrate/rights_metadata.rb, line 159
def users
  quick_search_by_type(:person)
end

Private Instance Methods

remove_all_permissions(selector) click to toggle source

Purge all access given group/person

# File lib/fedora_migrate/rights_metadata.rb, line 255
def remove_all_permissions(selector)
  return unless ng_xml
  type = selector.keys.first.to_sym
  actor = selector.values.first
  xpath = xpath(type, actor)
  nodes_to_purge = find_by_terms(xpath)
  nodes_to_purge.each(&:remove)
end
xpath(type, actor) click to toggle source

@param [Symbol] type (:group, :person) @param [String,TrueClass] actor the user we want to find. If actor is true, then don't query.

# File lib/fedora_migrate/rights_metadata.rb, line 266
def xpath(type, actor)
  raise ArgumentError, "Type must either be ':group' or ':person'. You provided: '#{type.inspect}'" unless [:group, :person].include?(type)
  path = "//oxns:access/oxns:machine/oxns:#{type}"
  if actor.is_a? String
    clean_actor = actor.delete("'")
    path += "[text() = '#{clean_actor}']"
  end
  path
end