In Files

Parent

Class/Module Index [+]

Quicksearch

Six

Attributes

current_rule_pack[R]
rules_packs[R]

Public Class Methods

new(packs={}) click to toggle source

Initialize ability object

Parameters:

packs

A Hash or rules to add with initializtion

Returns:

self

# File lib/six.rb, line 32
def initialize(packs={})
  raise InitializeArgumentError.new unless packs.kind_of?(Hash)

  @rules_packs = {}
  @current_rule_pack = nil

  packs.each { |key, pack| add_pack!(key, pack) }
end

Public Instance Methods

<<(pack) click to toggle source

Add pack to authorization class w/o key

Parameters:

pack

Any kind of object responding to allowed method

Returns:

true or raise exception

# File lib/six.rb, line 91
def <<(pack)
  add_pack!(pack.object_id.to_s, pack)
end
add_pack(name, pack) click to toggle source

Add pack to authorization class

Parameters:

name

A Symbol declaring the key name of stored pack

pack

Any kind of object responding to allowed method

Returns:

true or false

# File lib/six.rb, line 73
def add_pack(name, pack)
  rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
end
Also aliased as: add
add_pack!(name, pack) click to toggle source

Same as add_pack but raise exception if pack is invalid

# File lib/six.rb, line 78
def add_pack!(name, pack)
  add_pack(name, pack) || raise_incorrect_pack_object
end
Also aliased as: add!
allowed?(object, actions, subject) click to toggle source

Check if authorization class allow access for object to subject using selected pack or all stored. Basically this method

  1. send :allowed for every stored object in packs and pass object & subject

  2. check if any of results include allowed action

Parameters:

action

Action name to check for access

object

object trying to access resource

subject

resource

Returns:

true or false

# File lib/six.rb, line 162
def allowed?(object, actions, subject)
  # if multiple actions passed
  # check all actions to be allowed
  if actions.respond_to?(:each) 
    actions.all? { |action| action_included?(object, action, subject) }
  else
    # single action check
    action_included?(object, actions, subject)
  end
end
pack_exist?(name) click to toggle source

Check if authorization class has pack with such name

Parameters:

name

A Symbol declaring the key name of stored pack

Returns:

true or false

# File lib/six.rb, line 141
def pack_exist?(name)
  rules_packs.has_key?(name.to_sym)
end
Also aliased as: exist?
remove_pack(name) click to toggle source

Remove pack from authorization class

Parameters:

name

A Symbol declaring the key name of stored pack

Returns:

true or false

# File lib/six.rb, line 104
def remove_pack(name)
  if pack_exist?(name)
    @current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
    rules_packs.delete(name.to_sym)
  end
end
Also aliased as: remove
remove_pack!(name) click to toggle source

Same as remove_pack but raise exception if pack wasnt found

# File lib/six.rb, line 112
def remove_pack!(name)
  remove_pack(name) || raise_no_such_pack
end
Also aliased as: remove!
reset_use() click to toggle source

Reset current used rule pack so auth class use global allowed? for new request

# File lib/six.rb, line 175
def reset_use
  @current_rule_pack = nil
end
Also aliased as: reset
use_pack(name) click to toggle source

Set current pack from stored packs by key

Parameters:

name

A Symbol declaring the key name of stored pack

Returns:

self or false

# File lib/six.rb, line 50
def use_pack(name)
  if pack_exist?(name)
    @current_rule_pack = name.to_sym
    self
  end
end
Also aliased as: use
use_pack!(name) click to toggle source

Same as use but raise exception if no pack found

# File lib/six.rb, line 58
def use_pack!(name)
  use_pack(name) ? self : raise_no_such_pack
end
Also aliased as: use!
valid_rules_object?(object) click to toggle source

Check if object for rule pack is valid

Parameters:

pack

Any kind of object responding to allowed method

Returns:

true or false

# File lib/six.rb, line 125
def valid_rules_object?(object)
  object.respond_to?(:allowed) &&
    object.send(:allowed, nil, nil).kind_of?(Array)
rescue 
  false
end

Protected Instance Methods

action_included?(object, action, subject) click to toggle source
# File lib/six.rb, line 181
def action_included?(object, action, subject)
  if current_rule_pack
    rules_packs[current_rule_pack].allowed(object, subject).include?(action)
  else 
    rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
  end
end
add(name, pack) click to toggle source
Alias for: add_pack
add!(name, pack) click to toggle source
Alias for: add_pack!
exist?(name) click to toggle source
Alias for: pack_exist?
raise_incorrect_pack_object() click to toggle source
# File lib/six.rb, line 193
def raise_incorrect_pack_object
  raise Six::InvalidPackPassed.new
end
raise_no_such_pack() click to toggle source
# File lib/six.rb, line 189
def raise_no_such_pack
  raise Six::NoPackError.new
end
remove(name) click to toggle source
Alias for: remove_pack
remove!(name) click to toggle source
Alias for: remove_pack!
reset() click to toggle source
Alias for: reset_use
use(name) click to toggle source

shotcuts for long methods

Alias for: use_pack
use!(name) click to toggle source
Alias for: use_pack!

[Validate]

Generated with the Darkfish Rdoc Generator 2.