Class: Hydra::Datastream::RightsMetadata

Inherits:
ActiveFedora::OmDatastream
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/hydra/datastream/rights_metadata.rb

Overview

Implements Hydra RightsMetadata XML terminology for asserting access permissions

Direct Known Subclasses

InheritableRightsMetadata

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.date_indexerObject



247
248
249
# File 'lib/hydra/datastream/rights_metadata.rb', line 247

def self.date_indexer
  @date_indexer ||= Solrizer::Descriptor.new(:date, :stored, :indexed)
end

.indexerObject



239
240
241
# File 'lib/hydra/datastream/rights_metadata.rb', line 239

def self.indexer
  @indexer ||= Solrizer::Descriptor.new(:string, :stored, :indexed, :multivalued)
end

.xml_templateObject

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hydra/datastream/rights_metadata.rb', line 78

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(:type=>'title')
        xml.human(:type=>'description')
        xml.machine(:type=>'uri')
        
      }
      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
      }
      xml.lease{
        xml.human
        xml.machine
      }
    }
  end
  return builder.doc
end

Instance Method Details

#active_lease?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/hydra/datastream/rights_metadata.rb', line 207

def active_lease?
  lease_expiration_date.present? && Date.today < lease_expiration_date.first
end

#clear_permissions!Object

Completely clear the permissions



252
253
254
255
# File 'lib/hydra/datastream/rights_metadata.rb', line 252

def clear_permissions!
  remove_all_permissions({:person=>true})
  remove_all_permissions({:group=>true})
end

#date_indexerObject



243
244
245
# File 'lib/hydra/datastream/rights_metadata.rb', line 243

def date_indexer
  self.class.date_indexer
end

#groupsObject

Reports on which groups have which permissions

Returns:

  • Hash in format => group_permissions, group_name => group_permissions



153
154
155
# File 'lib/hydra/datastream/rights_metadata.rb', line 153

def groups
  return quick_search_by_type(:group)
end

#indexerObject



235
236
237
# File 'lib/hydra/datastream/rights_metadata.rb', line 235

def indexer
  self.class.indexer
end

#individualsObject



157
158
159
160
# File 'lib/hydra/datastream/rights_metadata.rb', line 157

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) ⇒ Object

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 ie. permissions(:person=>“person123”)

> “person123”=>“edit”

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

> “person123”=>“read”

permissions(:person=>“person123”)

> “person123”=>“read”

Parameters:

  • selector (Hash)

    hash in format => identifier

  • new_access_level (default nil) (defaults to: nil)

Returns:

  • Hash in format => access_level.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hydra/datastream/rights_metadata.rb', line 126

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 = 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)
    if new_access_level == "none" 
      self.content = self.to_xml
    else
      access_type_symbol = "#{new_access_level}_access".to_sym
      current_values = term_values(access_type_symbol, type)
      self.update_values([access_type_symbol, type] => current_values + [actor] )
    end
    return new_access_level
  end
    
end

#permissions=(params) ⇒ Object

Updates all permissions Restricts actor type to group or person. Any others will be ignored

Parameters:

  • params

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



179
180
181
182
183
184
185
186
# File 'lib/hydra/datastream/rights_metadata.rb', line 179

def permissions= (params)
  groups_for_update = params['group'] ? params['group'].keys : []
  group_ids = groups.keys | groups_for_update
  group_ids.each {|group_id| self.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| self.permissions({"person"=>person_id}, params['person'].fetch(person_id, 'none'))}
end

#quick_search_by_type(type) ⇒ Object

This method limits the response to known access levels. Probably runs a bit faster than .permissions().

Parameters:

  • type (Symbol)

    (either :group or :person)

Returns:



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hydra/datastream/rights_metadata.rb', line 191

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/hydra/datastream/rights_metadata.rb', line 211

def to_solr(solr_doc=Hash.new)
  [: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?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/hydra/datastream/rights_metadata.rb', line 203

def under_embargo?
  (embargo_release_date.present? && Date.today < embargo_release_date.first) ? 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

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



171
172
173
174
# File 'lib/hydra/datastream/rights_metadata.rb', line 171

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 {|person_id, access_level| self.permissions({"person"=>person_id}, access_level)}
end

#usersObject

Reports on which users have which permissions

Returns:

  • Hash in format => user_permissions, user_name => user_permissions



164
165
166
# File 'lib/hydra/datastream/rights_metadata.rb', line 164

def users
  return quick_search_by_type(:person)
end