Class: ActiveSecurity::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/active_security/configuration.rb

Overview

The configuration parameters passed to Base#active_security will be stored in this object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, values = nil) ⇒ Configuration

Returns a new instance of Configuration.



21
22
23
24
25
26
27
28
# File 'lib/active_security/configuration.rb', line 21

def initialize(model_class, values = nil)
  @model_class = model_class
  @defaults = {}
  @logger = ActiveRecord::Base.logger
  @modules = []
  @finder_methods = ActiveSecurity::FinderMethods
  set(values)
end

Instance Attribute Details

#defaultsObject (readonly)

The default configuration options.



6
7
8
# File 'lib/active_security/configuration.rb', line 6

def defaults
  @defaults
end

#finder_methodsObject

The module to use for finders



16
17
18
# File 'lib/active_security/configuration.rb', line 16

def finder_methods
  @finder_methods
end

#loggerObject

Where logs will be sent



19
20
21
# File 'lib/active_security/configuration.rb', line 19

def logger
  @logger
end

#model_classObject

The model class that this configuration belongs to.

Returns:

  • ActiveRecord::Base



13
14
15
# File 'lib/active_security/configuration.rb', line 13

def model_class
  @model_class
end

#modulesObject (readonly)

The modules in use



9
10
11
# File 'lib/active_security/configuration.rb', line 9

def modules
  @modules
end

Instance Method Details

#_handle_hash(hash) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/active_security/configuration.rb', line 72

def _handle_hash(hash)
  hash.each do |mod_key, attrs|
    mod = get_module(mod_key)
    _use(mod, 0) do |config|
      config.send(:set, attrs)
    end
  end
end

#_use(mod, idx) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



81
82
83
84
85
86
87
88
89
90
# File 'lib/active_security/configuration.rb', line 81

def _use(mod, idx, &block)
  mod.setup(@model_class) if mod.respond_to?(:setup)
  @model_class.send(:include, mod) unless uses?(mod)
  # Only yield on the first module, so as to not run the block multiple times,
  # and because later modules may require the attributes of a prior module to exist.
  # The block structure won't work for more complex config than that.
  # For more complex configuration pass a Hash where the keys are the "modules"
  yield self if block_given? && idx.zero?
  mod.after_config(@model_class) if mod.respond_to?(:after_config)
end

#use(*modules, &block) ⇒ Object

Lets you specify the addon modules to use with ActiveSecurity.

This method is invoked by active_security when passing the ‘:use` option, or when using active_security with a block.

Examples:

class Book < ActiveRecord::Base
  extend ActiveSecurity
  active_security use: :finders
end

Parameters:

  • modules (#to_s, Module, Hash[[#to_s, Module], Array[Hash[#to_s, any]]])

    Arguments should be Modules, or symbols or strings that correspond with the name of an addon to use with ActiveSecurity, or a hash/array of hashes where the keys are Modules, symbols, or strings corresponding as previously described, and the values are the Hashes of key value pairs of configuration attributes and their assigned values. By default ActiveSecurity provides ‘:finders`, `:privileged`, `:restricted` and `:scoped`.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_security/configuration.rb', line 48

def use(*modules, &block)
  mods = modules.to_a.compact
  mods.map.with_index do |object, idx|
    case object
    when Array
      object.each do |obj|
        if obj.is_a?(Hash)
          _handle_hash(obj)
        else
          mod = get_module(obj)
          _use(mod, idx, &block)
        end
      end
    when Hash
      _handle_hash(object)
    when String, Symbol, Module
      mod = get_module(object)
      _use(mod, idx, &block)
    else
      raise InvalidConfig, "Unknown Argument Type #{object.class}: #{object.inspect}"
    end
  end
end

#uses?(mod) ⇒ Boolean

Returns whether the given module is in use.

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_security/configuration.rb', line 93

def uses?(mod)
  @model_class < get_module(mod)
end