Class: Aclatraz::ACL
- Inherits:
-
Object
- Object
- Aclatraz::ACL
- Defined in:
- lib/aclatraz/acl.rb
Defined Under Namespace
Classes: Action
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
All actions defined in current ACL.
-
#suspect ⇒ Object
Current suspected object.
Instance Method Summary collapse
-
#[](action) ⇒ Object
Syntactic sugar for actions
actions[action]
. -
#clone(&block) ⇒ Object
:nodoc:.
-
#evaluate(&block) ⇒ Object
Evaluates given block in default action.
-
#initialize(suspect, &block) ⇒ ACL
constructor
A new instance of ACL.
-
#on(action, &block) ⇒ Object
Defines given action with permissions described in evaluated block.
-
#permissions ⇒ Object
List of permissions defined in default action.
Constructor Details
#initialize(suspect, &block) ⇒ ACL
Returns a new instance of ACL.
67 68 69 70 71 |
# File 'lib/aclatraz/acl.rb', line 67 def initialize(suspect, &block) @actions = {} @suspect = suspect evaluate(&block) if block_given? end |
Instance Attribute Details
#actions ⇒ Object (readonly)
All actions defined in current ACL.
62 63 64 |
# File 'lib/aclatraz/acl.rb', line 62 def actions @actions end |
#suspect ⇒ Object
Current suspected object.
65 66 67 |
# File 'lib/aclatraz/acl.rb', line 65 def suspect @suspect end |
Instance Method Details
#[](action) ⇒ Object
Syntactic sugar for actions actions[action]
.
92 93 94 |
# File 'lib/aclatraz/acl.rb', line 92 def [](action) actions[action] end |
#clone(&block) ⇒ Object
:nodoc:
118 119 120 121 122 123 124 |
# File 'lib/aclatraz/acl.rb', line 118 def clone(&block) # :nodoc: actions = Hash[*self.actions.map {|k,v| [k, v.clone(self)] }.flatten] cloned = self.class.new(suspect) cloned.instance_variable_set("@actions", actions) cloned.evaluate(&block) cloned end |
#evaluate(&block) ⇒ Object
Evaluates given block in default action.
Example
evaluate do
allow :foo
deny :bar
...
end
82 83 84 |
# File 'lib/aclatraz/acl.rb', line 82 def evaluate(&block) on(:_, &block) end |
#on(action, &block) ⇒ Object
Defines given action with permissions described in evaluated block.
Examples
suspects do
on :create do
deny all
allow :admin
end
on :delete do
allow :owner_of => "object"
end
end
109 110 111 112 113 114 115 116 |
# File 'lib/aclatraz/acl.rb', line 109 def on(action, &block) raise ArgumentError, "No block given!" unless block_given? if @actions.key?(action) @actions[action].instance_eval(&block) else @actions[action] = Action.new(self, &block) end end |
#permissions ⇒ Object
List of permissions defined in default action.
87 88 89 |
# File 'lib/aclatraz/acl.rb', line 87 def @actions[:_] ? @actions[:_]. : Dictionary.new {|h,k| h[k] = false} end |