Class: OodCore::Acl::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_core/acl/factory.rb,
lib/ood_core/acl/adapters/group.rb

Overview

A factory that builds acl adapter objects from a configuration.

Class Method Summary collapse

Class Method Details

.build(config) ⇒ Acl::Adapter

Build an acl adapter from a configuration

Parameters:

  • config (#to_h)

    configuration describing acl adapter

Options Hash (config):

  • :adapter (#to_s)

    The acl adapter to use

Returns:

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

Build the group acl adapter from a configuration

Parameters:

  • config (#to_h)

    the configuration for an acl adapter

Options Hash (config):

  • :groups (Array<#to_s>)

    The list of groups

  • :type (#to_s) — default: 'whitelist'

    The type of ACL ('whitelist' or 'blacklist')



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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