Class: ActionGuard::Guard
- Inherits:
-
Object
- Object
- ActionGuard::Guard
- Defined in:
- lib/action-guard/base.rb
Instance Method Summary collapse
- #allow_rule(path_matcher, &block) ⇒ Object
- #authorized?(person, request_params) ⇒ Boolean
- #define_role(role, level) ⇒ Object
- #exact_role_rule(path_matcher, role_value) ⇒ Object
-
#initialize ⇒ Guard
constructor
A new instance of Guard.
- #leveled_rule(path_matcher, from_role_value, to_role_value = nil, &block) ⇒ Object
- #load_from_string(configuration, name = nil) ⇒ Object
- #refuse_rule(path_matcher) ⇒ Object
- #role(role_value) ⇒ Object
- #valid_role?(role) ⇒ Boolean
- #valid_roles ⇒ Object
Constructor Details
#initialize ⇒ Guard
Returns a new instance of Guard.
3 4 5 6 7 8 |
# File 'lib/action-guard/base.rb', line 3 def initialize @rules = {} @rules.default = DisallowRule.new @roles = {} @roles.default = Role.new(:illegal_role, 100000) end |
Instance Method Details
#allow_rule(path_matcher, &block) ⇒ Object
36 37 38 |
# File 'lib/action-guard/base.rb', line 36 def allow_rule(path_matcher, &block) rules[path_matcher] = AllowRule.new(&block) end |
#authorized?(person, request_params) ⇒ Boolean
48 49 50 51 52 53 |
# File 'lib/action-guard/base.rb', line 48 def (person, request_params) raise Error.new("no configuration loaded") if rules.empty? path = "#{request_params['controller']}##{request_params['action']}" rule_key = rules.keys.sort{|x,y| y <=> x }.select {|k| path =~ /^#{k}/}.first rules[rule_key].allows?(person,request_params) end |
#define_role(role, level) ⇒ Object
14 15 16 |
# File 'lib/action-guard/base.rb', line 14 def define_role(role, level) @roles[role.to_sym] = Role.new(role, level) end |
#exact_role_rule(path_matcher, role_value) ⇒ Object
44 45 46 |
# File 'lib/action-guard/base.rb', line 44 def exact_role_rule(path_matcher, role_value) rules[path_matcher] = ExactRoleRule.new(role_value) end |
#leveled_rule(path_matcher, from_role_value, to_role_value = nil, &block) ⇒ Object
30 31 32 33 34 |
# File 'lib/action-guard/base.rb', line 30 def leveled_rule(path_matcher, from_role_value, to_role_value = nil, &block) raise Error.new("undefined role '#{from_role_value}'") unless valid_role?(from_role_value) raise Error.new("undefined role '#{to_role_value}'") if to_role_value && !valid_role?(to_role_value) rules[path_matcher] = LevelRule.new(from_role_value, to_role_value, self, &block) end |
#load_from_string(configuration, name = nil) ⇒ Object
10 11 12 |
# File 'lib/action-guard/base.rb', line 10 def load_from_string(configuration, name = nil) Syntax.new(self).instance_eval(configuration, name || 'unknown') end |
#refuse_rule(path_matcher) ⇒ Object
40 41 42 |
# File 'lib/action-guard/base.rb', line 40 def refuse_rule(path_matcher) rules[path_matcher] = DisallowRule.new end |
#role(role_value) ⇒ Object
18 19 20 |
# File 'lib/action-guard/base.rb', line 18 def role(role_value) @roles[role_value.to_sym] end |
#valid_role?(role) ⇒ Boolean
22 23 24 |
# File 'lib/action-guard/base.rb', line 22 def valid_role?(role) @roles.has_key?(role.to_sym) end |
#valid_roles ⇒ Object
26 27 28 |
# File 'lib/action-guard/base.rb', line 26 def valid_roles @roles.keys.map { |r| r.to_s } end |