Object
Initialize ability object
packs |
A Hash or rules to add with initializtion |
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
Add pack to authorization class w/o key
pack |
Any kind of object responding to allowed method |
true or raise exception
# File lib/six.rb, line 91 def <<(pack) add_pack!(pack.object_id.to_s, pack) end
Add pack to authorization class
name |
A Symbol declaring the key name of stored pack |
pack |
Any kind of object responding to allowed method |
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
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
Check if authorization class allow access for object to subject using selected pack or all stored. Basically this method
send :allowed for every stored object in packs and pass object & subject
check if any of results include allowed action
action |
Action name to check for access |
object |
object trying to access resource |
subject |
resource |
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
Check if authorization class has pack with such name
name |
A Symbol declaring the key name of stored pack |
true or false
# File lib/six.rb, line 141 def pack_exist?(name) rules_packs.has_key?(name.to_sym) end
Remove pack from authorization class
name |
A Symbol declaring the key name of stored pack |
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
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
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
Set current pack from stored packs by key
name |
A Symbol declaring the key name of stored pack |
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
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
Check if object for rule pack is valid
pack |
Any kind of object responding to allowed method |
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
# 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
# File lib/six.rb, line 193 def raise_incorrect_pack_object raise Six::InvalidPackPassed.new end
Generated with the Darkfish Rdoc Generator 2.