class Six
Attributes
Public Class Methods
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
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
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
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 159 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
Parameters:¶ ↑
- name
-
A Symbol declaring the key name of stored pack
Returns:¶ ↑
true or false
# File lib/six.rb, line 138 def pack_exist?(name) rules_packs.has_key?(name.to_sym) end
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
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 172 def reset_use @current_rule_pack = nil end
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
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
Protected Instance Methods
# File lib/six.rb, line 178 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 190 def raise_incorrect_pack_object raise Six::InvalidPackPassed.new end
# File lib/six.rb, line 186 def raise_no_such_pack raise Six::NoPackError.new end