Class: Aclatraz::ACL

Inherits:
Object
  • Object
show all
Defined in:
lib/aclatraz/acl.rb

Defined Under Namespace

Classes: Action

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionsObject (readonly)

All actions defined in current ACL.



62
63
64
# File 'lib/aclatraz/acl.rb', line 62

def actions
  @actions
end

#suspectObject

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

Raises:

  • (ArgumentError)


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

#permissionsObject

List of permissions defined in default action.



87
88
89
# File 'lib/aclatraz/acl.rb', line 87

def permissions
  @actions[:_] ? @actions[:_].permissions : Dictionary.new {|h,k| h[k] = false}
end