Class: Hyrax::PermissionTemplateApplicator

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/permission_template_applicator.rb

Overview

Applies a ‘PermissionTemplate` to a given model object by adding the template’s manage and view users to the model’s permissions.

Examples:

applying a template

applicator = PermissionTemplateApplicator.new(template: my_template)
applicator.apply_to(work)

applying a template with fluent chaining syntax

PermissionTemplateApplicator.apply(my_template).to work

Since:

  • 2.4.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:) ⇒ PermissionTemplateApplicator

Returns a new instance of PermissionTemplateApplicator.

Parameters:

Since:

  • 2.4.0



23
24
25
# File 'app/services/hyrax/permission_template_applicator.rb', line 23

def initialize(template:)
  self.template = template
end

Instance Attribute Details

#templateHyrax::PermissionTemplate



19
20
21
# File 'app/services/hyrax/permission_template_applicator.rb', line 19

def template
  @template
end

Class Method Details

.apply(template) ⇒ PermissionTemplateApplicator

Parameters:

Returns:

Since:

  • 2.4.0



31
32
33
# File 'app/services/hyrax/permission_template_applicator.rb', line 31

def self.apply(template)
  new(template: template)
end

Instance Method Details

#apply_to(model:) ⇒ Boolean Also known as: to

Returns true if the permissions have been successfully applied.

Parameters:

  • model (Hydra::PCDM::Object, Hydra::PCDM::Collection)

Returns:

  • (Boolean)

    true if the permissions have been successfully applied

Since:

  • 2.4.0



38
39
40
41
42
43
44
45
# File 'app/services/hyrax/permission_template_applicator.rb', line 38

def apply_to(model:)
  model.edit_groups += template.agent_ids_for(agent_type: 'group', access: 'manage')
  model.edit_users  += template.agent_ids_for(agent_type: 'user',  access: 'manage')
  model.read_groups += template.agent_ids_for(agent_type: 'group', access: 'view')
  model.read_users  += template.agent_ids_for(agent_type: 'user',  access: 'view')

  true
end