Module: Hadley::Authz::StrategyBuilder

Included in:
Strategy
Defined in:
lib/hadley/authz/strategy_builder.rb

Overview

This mixin module provides helpful utilties for generating new authorization strategies based on configuration provided when the strategy is being registered with Warden.

Instance Method Summary collapse

Instance Method Details

#build(name, config) ⇒ Object

Builds a new authorization strategy class based on the provided configuration.

Parameters:

  • name (Symbol)

    The unqualified name of the authorization strategy to be built.

  • config (Hadley::Config)

    The configuration for the authorization strategy to be built.



9
10
11
12
13
# File 'lib/hadley/authz/strategy_builder.rb', line 9

def build(name, config)
  strategy = self.create_strategy(name)
  self.register_strategy(name, strategy)
  self.set_config(strategy, config)
end

#create_strategy(name) ⇒ Class (protected)

Creates the strategy class based on the provided name. The class will be namespaced under the class that these methods are mixed into.

Parameters:

  • name (Symbol)

    The unqualified name of the authorization strategy to be built.

Returns:

  • (Class)

    The new strategy class.



23
24
25
26
27
28
29
30
# File 'lib/hadley/authz/strategy_builder.rb', line 23

def create_strategy(name)
  class_name = Hadley::Utils.camelize(name.to_s)
  if self.const_defined?(class_name)
    self.const_get(class_name) 
  else
    self.const_set(class_name, Class.new(self))
  end
end

#register_strategy(name, strategy) ⇒ Object (protected)

Registers the new authorization strategy with Warden under the specified name prefixed by ‘afid_’.

Parameters:

  • name (Symbol)

    The unqualified name of the authorization strategy to be built.

  • strategy (Class)

    The newly created strategy class.



36
37
38
39
40
41
# File 'lib/hadley/authz/strategy_builder.rb', line 36

def register_strategy(name, strategy)
  full_name = "afid_#{name}".to_sym
  if Warden::Strategies[full_name].nil?
    Warden::Strategies.add(full_name, strategy) 
  end
end

#set_config(strategy, config) ⇒ Object (protected)

Binds the configuration information to the newly created strategy class.

Parameters:

  • strategy (Class)

    The newly created strategy class.

  • config (Hadley::Config)

    The configuration to be bound to the authorization strategy.



47
48
49
# File 'lib/hadley/authz/strategy_builder.rb', line 47

def set_config(strategy, config)
  strategy.const_set("CONFIG", config) unless strategy.const_defined?("CONFIG")
end