Class: Fortress::ControllerInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/fortress/controller_interface.rb

Overview

Object to easily use the Mechanism stored rules. It’s a kind of helper class

Author:

  • zedtux

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_instance) ⇒ ControllerInterface

Returns a new instance of ControllerInterface.



11
12
13
# File 'lib/fortress/controller_interface.rb', line 11

def initialize(controller_instance)
  self.instance = controller_instance
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



9
10
11
# File 'lib/fortress/controller_interface.rb', line 9

def instance
  @instance
end

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/fortress/controller_interface.rb', line 9

def params
  @params
end

Instance Method Details

#action_allowed_from_only?(name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/fortress/controller_interface.rb', line 71

def action_allowed_from_only?(name)
  Array(params[:only]).include?(name.to_sym)
end

#action_forbidden?(name) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/fortress/controller_interface.rb', line 67

def action_forbidden?(name)
  Array(params[:except]).include?(name.to_sym)
end

#allow_action?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fortress/controller_interface.rb', line 31

def allow_action?(name)
  return false if action_forbidden?(name.to_sym)

  if conditionnal_method_with_action?(name.to_sym)
    return call_allow_method == true
  end

  return true if action_allowed_from_only?(name.to_sym)

  allow_all?
end

#allow_all?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fortress/controller_interface.rb', line 23

def allow_all?
  params[:all] == true
end

#allow_all_without_except?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fortress/controller_interface.rb', line 27

def allow_all_without_except?
  allow_all? && params.key?(:except) == false
end

#allow_method?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/fortress/controller_interface.rb', line 43

def allow_method?
  params.key?(:if) && params[:if].key?(:method)
end

#blocked?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fortress/controller_interface.rb', line 19

def blocked?
  params.nil?
end

#call_allow_methodObject



52
53
54
# File 'lib/fortress/controller_interface.rb', line 52

def call_allow_method
  instance.send(params[:if][:method])
end

#conditionally_allowed?(action_name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/fortress/controller_interface.rb', line 56

def conditionally_allowed?(action_name)
  return unless allow_method?
  return unless needs_to_check_action?(action_name.to_sym)
  call_allow_method
end

#conditionnal_method_with_action?(name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/fortress/controller_interface.rb', line 62

def conditionnal_method_with_action?(name)
  return false unless params.key?(:if) && params[:if].key?(:actions)
  return true if params[:if][:actions].include?(name)
end

#needs_to_check_action?(name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/fortress/controller_interface.rb', line 47

def needs_to_check_action?(name)
  params.key?(:if) && params[:if].key?(:actions) &&
    Array(params[:if][:actions]).include?(name)
end