Sha256: ca2fe748c3fd03bc17a1f1c0b47c56a8184543ad1f293d511dbbe2d2ac02739e

Contents?: true

Size: 1.48 KB

Versions: 45

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
module Hyrax
  ##
  # Applies a `PermissionTemplate` to a given model object by adding the
  # template's manage and view users to the model's permissions.
  #
  # @example applying a template
  #   applicator = PermissionTemplateApplicator.new(template: my_template)
  #   applicator.apply_to(work)
  #
  # @example applying a template with fluent chaining syntax
  #   PermissionTemplateApplicator.apply(my_template).to work
  #
  # @since 2.4.0
  class PermissionTemplateApplicator
    ##
    # @!attribute [rw] template
    #   @return [Hyrax::PermissionTemplate]
    attr_accessor :template

    ##
    # @param template [Hyrax::PermissionTemplate]
    def initialize(template:)
      self.template = template
    end

    ##
    # @param template [Hyrax::PermissionTemplate]
    #
    # @return [PermissionTemplateApplicator]
    def self.apply(template)
      new(template: template)
    end

    ##
    # @param model [Hydra::PCDM::Object, Hydra::PCDM::Collection]
    # @return [Boolean] true if the permissions have been successfully applied
    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
    alias to apply_to
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
hyrax-2.5.0 app/services/hyrax/permission_template_applicator.rb
hyrax-3.0.0.pre.beta2 app/services/hyrax/permission_template_applicator.rb
hyrax-2.4.1 app/services/hyrax/permission_template_applicator.rb
hyrax-3.0.0.pre.beta1 app/services/hyrax/permission_template_applicator.rb
hyrax-2.4.0 app/services/hyrax/permission_template_applicator.rb