class OodCore::Acl::Factory

A factory that builds acl adapter objects from a configuration.

Public Class Methods

build(config) click to toggle source

Build an acl adapter from a configuration @param config [#to_h] configuration describing acl adapter @option config [#to_s] :adapter The acl adapter to use @raise [AdapterNotSpecified] if no adapter is specified @raise [AdapterNotFound] if the specified adapter does not exist @return [Acl::Adapter] the acl adapter object

# File lib/ood_core/acl/factory.rb, line 16
def build(config)
  c = config.to_h.symbolize_keys

  adapter = c.fetch(:adapter) { raise AdapterNotSpecified, "acl configuration does not specify adapter" }.to_s

  path_to_adapter = "ood_core/acl/adapters/#{adapter}"
  begin
    require path_to_adapter
  rescue Gem::LoadError => e
    raise Gem::LoadError, "Specified '#{adapter}' for acl adapter, but the gem is not loaded."
  rescue LoadError => e
    raise LoadError, "Could not load '#{adapter}'. Make sure that the acl adapter in the configuration file is valid."
  end

  adapter_method = "build_#{adapter}"

  unless respond_to?(adapter_method)
    raise AdapterNotFound, "acl configuration specifies nonexistent #{adapter} adapter"
  end

  send(adapter_method, c)
end
build_group(config) click to toggle source

Build the group acl adapter from a configuration @param config [#to_h] the configuration for an acl adapter @option config [Array<#to_s>] :groups The list of groups @option config [#to_s] :type ('whitelist') The type of ACL ('whitelist' or 'blacklist')

# File lib/ood_core/acl/adapters/group.rb, line 12
def self.build_group(config)
  c = config.to_h.symbolize_keys

  groups = c.fetch(:groups) { raise ArgumentError, "No groups specified. Missing argument: groups" }.map(&:to_s)
  acl = OodSupport::ACL.new(entries: groups.map { |g| OodSupport::ACLEntry.new principle: g })

  type   = c.fetch(:type, "whitelist").to_s
  if type == "whitelist"
    allow = true
  elsif type == "blacklist"
    allow = false
  else
    raise ArgumentError, "Invalid type specified. Valid types: whitelist, blacklist"
  end

  Adapters::Group.new(acl: acl, allow: allow)
end