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

:nodoc:



69
70
71
72
73
# File 'lib/aclatraz/acl.rb', line 69

def initialize(suspect, &block) # :nodoc:
  @actions = {}
  @suspect = suspect
  evaluate(&block) if block_given?
end

Instance Attribute Details

#actionsObject (readonly)

All actions defined in current ACL.



64
65
66
# File 'lib/aclatraz/acl.rb', line 64

def actions
  @actions
end

#suspectObject

Current suspected object.



67
68
69
# File 'lib/aclatraz/acl.rb', line 67

def suspect
  @suspect
end

Instance Method Details

#[](action) ⇒ Object

Syntactic sugar for actions actions[action].



94
95
96
# File 'lib/aclatraz/acl.rb', line 94

def [](action)
  actions[action]
end

#clone(&block) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
# File 'lib/aclatraz/acl.rb', line 120

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


84
85
86
# File 'lib/aclatraz/acl.rb', line 84

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)


111
112
113
114
115
116
117
118
# File 'lib/aclatraz/acl.rb', line 111

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.



89
90
91
# File 'lib/aclatraz/acl.rb', line 89

def permissions
  @actions[:_] ? @actions[:_].permissions : Dictionary.new
end