Class: Thwart::RoleBuilder

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/thwart/role_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_store) ⇒ RoleBuilder

Returns a new instance of RoleBuilder.



21
22
23
# File 'lib/thwart/role_builder.rb', line 21

def initialize(a_store)
  @actionables_store = a_store
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



103
104
105
106
# File 'lib/thwart/role_builder.rb', line 103

def method_missing(name, *args, &block)
  return define_permission(name, *args, &block) if self.respond_to?(name)
  super
end

Instance Attribute Details

#actionables_storeObject (readonly)

Returns the value of attribute actionables_store.



12
13
14
# File 'lib/thwart/role_builder.rb', line 12

def actionables_store
  @actionables_store
end

#last_built_roleObject

Returns the value of attribute last_built_role.



11
12
13
# File 'lib/thwart/role_builder.rb', line 11

def last_built_role
  @last_built_role
end

Class Method Details

.empty_roleObject



15
16
17
18
19
# File 'lib/thwart/role_builder.rb', line 15

def self.empty_role
  Class.new do
    include Thwart::Role
  end
end

Instance Method Details

#allow(&block) ⇒ Object



82
83
84
# File 'lib/thwart/role_builder.rb', line 82

def allow(&block)
  evaluate_with_response(true, &block)
end

#create_role(name, options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thwart/role_builder.rb', line 25

def create_role(name, options = {}, &block)
  @role = self.class.empty_role.new     # Start empty role
  @current_response = true              # Assume the first permission definitions are allows
  run_callbacks :build_role do
    # Add parents
    @role.name = name
    @role.parents = options[:parents] if options[:parents]
  
    # Run DSL block
    if block_given?
      @dsl ||= Thwart::Dsl.new
      @dsl.all = true
      @dsl.evaluate(self, &block)
    end
    self.last_built_role = @role
  end
  # Unset the @role instance variable to disable DSL methods and return the role
  r = @role
  @role = nil
  return r
end

#default(bool) ⇒ Object



72
73
74
75
# File 'lib/thwart/role_builder.rb', line 72

def default(bool)
  ensure_in_dsl!
  @role.default_response = bool
end

#define_permission(name, *resources, &block) ⇒ Object

Raises:

  • (ArgumentError)


47
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/thwart/role_builder.rb', line 47

def define_permission(name, *resources, &block)
  ensure_in_dsl! 
  # Shift off first argument sent from method missing and convert any action groups to an array of actions
  raise ArgumentError, "Unrecognized action or action group #{name}" if !self.actionables.has_key?(name)
  names = self.actionables[name]
  
  # Pop of last hash argument from method missing and merge in default options and :if block
  options = {:if => false, :unless => false}
  options.merge!(resources.pop) if resources.last.respond_to?(:keys)
  options[:if] = block if block_given?
  # Allow :all or blank resource specifiers
  if resources.nil? || resources.empty? || resources.any? {|r| r == :all}
    resources = [:_other]
  end
  
  # Generate response based on @current_response and optional procs
  generated_response = generate_response(options)
  response_hash = hash_with_value(resources, generated_response)
  
  # Merge into existing role definition
  @role.responses.deep_merge!(hash_with_value(names) do |k|
    response_hash
  end)
end

#deny(&block) ⇒ Object



86
87
88
# File 'lib/thwart/role_builder.rb', line 86

def deny(&block)
  evaluate_with_response(false, &block)
end

#evaluate_with_response(response, &block) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/thwart/role_builder.rb', line 90

def evaluate_with_response(response, &block)
  ensure_in_dsl!
  old = @current_response
  @current_response = response
  @dsl.evaluate(self, &block)
  @current_response = old
end

#include(*args) ⇒ Object



77
78
79
80
# File 'lib/thwart/role_builder.rb', line 77

def include(*args)
  ensure_in_dsl!
  @role.parents += args
end

#respond_to?(name, other = false) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/thwart/role_builder.rb', line 98

def respond_to?(name,  other = false)
  return true if self.actionables.has_key?(name)
  super
end