Sha256: 0d018749ccf3c687bc29b63f30f9f04ae07552fe6d962bd3e390bf8d6b80b64b

Contents?: true

Size: 1.69 KB

Versions: 25

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true
module Hyrax
  # Responsible for registering roles critical for your application.
  # Registering a role is effectively saying "my application logic will not work if this role goes away".
  #
  # @see Sipity::Role
  class RoleRegistry
    # @api public
    # You may develop your application assuming that the 'managing' role will always be present and valid
    MANAGING = 'managing'

    # @api public
    # You may develop your application assuming that the 'approving' role will always be present and valid
    APPROVING = 'approving'

    # @api public
    # You may develop your application assuming that the 'depositing' role will always be present and valid
    DEPOSITING = 'depositing'

    # @api public
    #
    # It is a safe assumption that Hyrax has these magic roles.
    # While the descriptions may be mutable, the names are assumed to exist.
    #
    # @see Sipity::Role for data integrity enforcement
    MAGIC_ROLES = {
      MANAGING => 'Grants access to management tasks',
      APPROVING => 'Grants access to approval tasks',
      DEPOSITING => 'Grants access to depositing tasks'
    }.freeze

    def initialize
      @roles = MAGIC_ROLES.dup
    end

    def add(name:, description:)
      @roles[name.to_s] = description
    end

    def role_names
      @roles.keys.sort
    end

    def registered_role?(name:)
      @roles.key?(name.to_s)
    end

    # @api public
    #
    # Load the registered roles into Sipity::Role
    def persist_registered_roles!
      @roles.each do |name, description|
        Sipity::Role.find_or_create_by!(name: name).tap do |role|
          role.description = description
          role.save!
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
hyrax-5.0.1 lib/hyrax/role_registry.rb
hyrax-5.0.0 lib/hyrax/role_registry.rb
hyrax-5.0.0.rc3 lib/hyrax/role_registry.rb
hyrax-5.0.0.rc2 lib/hyrax/role_registry.rb
hyrax-5.0.0.rc1 lib/hyrax/role_registry.rb
hyrax-3.6.0 lib/hyrax/role_registry.rb
hyrax-4.0.0 lib/hyrax/role_registry.rb
hyrax-4.0.0.rc3 lib/hyrax/role_registry.rb
hyrax-4.0.0.rc2 lib/hyrax/role_registry.rb
hyrax-4.0.0.rc1 lib/hyrax/role_registry.rb
hyrax-3.5.0 lib/hyrax/role_registry.rb
hyrax-4.0.0.beta2 lib/hyrax/role_registry.rb
hyrax-3.4.2 lib/hyrax/role_registry.rb
hyrax-4.0.0.beta1 lib/hyrax/role_registry.rb
hyrax-3.4.1 lib/hyrax/role_registry.rb
hyrax-3.4.0 lib/hyrax/role_registry.rb
hyrax-3.3.0 lib/hyrax/role_registry.rb
hyrax-3.2.0 lib/hyrax/role_registry.rb
hyrax-3.1.0 lib/hyrax/role_registry.rb
hyrax-3.0.2 lib/hyrax/role_registry.rb