Class: Hydra::RightsMetadataDatastream

Inherits:
ActiveFedora::NokogiriDatastream show all
Defined in:
lib/active_fedora/samples/hydra-rights_metadata_datastream.rb

Overview

This is an example of a NokogiriDatastream that defines a terminology for Hydra rightsMetadata xml The documentation for Hydra rightsMetadata is on the Hydra wiki at wiki.duraspace.org/display/hydra/Hydra+rights+metadata The real version of this Terminology is part of the hydra-head plugin. See github.com/projecthydra/hydra-head/blob/master/lib/hydra/rights_metadata.rb

The purpose of rightsMetadata is to store information about access controls and licensing for the object that the rightsMetadata is attached to

The most interesting thing going on in this Class is the use of custom methods to access and update permissions in intuitive ways. These methods allow you to do things like

  • Get reports on which groups & individuals have which permissions

  • Update permissions for individuals & groups with straightforward syntax

Another interesting thing in this Class: it extends to_solr, first calling “super” (the default to_solr behavior) and then inserting an additional embargo_release_date_dt field It uses Solrizer::Extractor.insert_solr_field_value to do this. That method handles inserting new values into a Hash while ensuring that you don’t destroy or overwrite any existing values in the hash.

Instance Attribute Summary collapse

Attributes inherited from ActiveFedora::NokogiriDatastream

#internal_solr_doc, #ng_xml

Attributes included from ActiveFedora::MetadataDatastreamHelper

#fields

Attributes inherited from ActiveFedora::Datastream

#dirty, #fields, #last_modified

Attributes inherited from Fedora::BaseObject

#attributes, #blob, #errors, #new_object, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveFedora::NokogiriDatastream

#from_solr, from_xml, #generate_solr_symbol, #get_values, #get_values_from_solr, #has_solr_name?, #initialize, #is_hierarchical_term_pointer?, #om_term_values, #om_update_values, #term_values, #to_xml, #update_indexed_attributes, #update_values

Methods included from ActiveFedora::MetadataDatastreamHelper

#from_solr, included, #initialize, #save, #set_blob_for_save, #to_xml

Methods inherited from ActiveFedora::Datastream

#after_save, #before_save, #check_concurrency, #content, #content=, delete, #delete, #dirty?, #dsid=, from_xml, #initialize, #last_modified_in_repository, #pid, #pid=, #save, #size, #to_param

Methods inherited from Fedora::Datastream

#control_group, #control_group=, #dsid, #initialize, #label, #label=, #mime_type, #mime_type=, #pid, #uri, #url

Methods inherited from Fedora::BaseObject

#[], #initialize, #new_object?

Constructor Details

This class inherits a constructor from ActiveFedora::NokogiriDatastream

Instance Attribute Details

#embargo_release_date(opts = {}) ⇒ Object

Returns the value of attribute embargo_release_date.



164
165
166
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 164

def embargo_release_date
  @embargo_release_date
end

Class Method Details

.xml_templateObject

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 53

def self.xml_template
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.(:version=>"0.1", "xmlns"=>"http://hydra-collab.stanford.edu/schemas/rightsMetadata/v1") {
      xml.copyright {
        xml.human
        xml.machine {
          xml.uvalicense "no"
        }
      }
      xml.access(:type=>"discover") {
        xml.human
        xml.machine
      }
      xml.access(:type=>"read") {
        xml.human 
        xml.machine
      }
      xml.access(:type=>"edit") {
        xml.human
        xml.machine
      }
      xml.embargo{
        xml.human
        xml.machine
      }        
    }
  end
  return builder.doc
end

Instance Method Details

#groupsHash

Reports on which groups have which permissions

Examples:

sample_ds.permissions({"group"=>"group_zzz"}, "edit")
sample_ds.permissions({"group"=>"public"}, "discover")
sample_ds.groups #=>  {"public"=>"discover", "group_zzz"=>"edit"}

Returns:



127
128
129
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 127

def groups
  return quick_search_by_type(:group)
end

#individualsHash

Reports on which groups have which permissions

Examples:

sample_ds.permissions({"person"=>"person_123"}, "read")
sample_ds.permissions({""person"=>"person_456"}, "edit")
sample_ds.individuals #=>  {"person_123"=>"read", "person_456"=>"edit"}

Returns:



137
138
139
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 137

def individuals
  return quick_search_by_type(:person)
end

#permissions(selector, new_access_level = nil) ⇒ Hash

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

Examples:

Query permissions for person123, Set the permissions to “read”, then query again to see that they have changed.

permissions({:person=>"person123"})
=> {"person123"=>"edit"}
permissions({:person=>"person123"}, "read")
=> {"person123"=>"read"}
permissions({:person=>"person123"})
=> {"person123"=>"read"}

Parameters:

  • selector (Hash)
  • new_access_level (String) (defaults to: nil)

    (default nil)

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 97

def permissions(selector, new_access_level=nil)

  type = selector.keys.first.to_sym
  actor = selector.values.first
  if new_access_level.nil?
    xpath = self.class.terminology.xpath_for(:access, type, actor)
    nodeset = self.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)
    unless new_access_level == "none" 
      access_type_symbol = "#{new_access_level}_access".to_sym
      result = self.update_values([access_type_symbol, type] => {"-1"=>actor})
    end
    self.dirty = true
    return new_access_level
  end
  
end

#quick_search_by_type(type) ⇒ Hash

This method limits the response to known access levels (:discover, :read, :edit). Probably runs a bit faster than #permissions.

Parameters:

  • type (:group, :person)

Returns:



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 152

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
    self.find_by_terms(*[access_level, type]).each do |entry|
      result[entry.text] = access_level_name
    end
  end
  return result
end

#to_solr(solr_doc = Hash.new) ⇒ Object



185
186
187
188
189
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 185

def to_solr(solr_doc=Hash.new)
  super(solr_doc)
  ::Solrizer::Extractor.insert_solr_field_value(solr_doc, "embargo_release_date_dt", embargo_release_date(:format=>:solr_date)) if embargo_release_date
  solr_doc
end

#under_embargo?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 181

def under_embargo?
  (embargo_release_date && Date.today < embargo_release_date.to_date) ? true : false
end

#update_permissions(params) ⇒ Object

Updates permissions for all of the persons and groups in a hash Currently restricts actor type to group or person. Any others will be ignored

Parameters:

  • params (Hash)

    example: “group”=>{“group1”=>“discover”,“group2”=>“edit”, “person”=>“person1”=>“read”,“person2”=>“discover”}



144
145
146
147
# File 'lib/active_fedora/samples/hydra-rights_metadata_datastream.rb', line 144

def update_permissions(params)
  params.fetch("group", {}).each_pair {|group_id, access_level| self.permissions({"group"=>group_id}, access_level)}
  params.fetch("person", {}).each_pair {|group_id, access_level| self.permissions({"person"=>group_id}, access_level)}
end