Class: SimpleAccessControl::AccessControlHandler
- Inherits:
-
Object
- Object
- SimpleAccessControl::AccessControlHandler
- Defined in:
- lib/simple_access_control.rb
Overview
A dramatically simpler version than that found in acl_system2 It is SLOWER because it uses instance_eval to analyse the conditional, but it’s DRY.
Instance Method Summary collapse
-
#check(role, user) ⇒ Object
The heart of the system, all credit to Ezra for the original algorithm Defaults to false if there is no user or that user does not have a roles association Defaults to true if the role is blank.
-
#parse(string) ⇒ Object
Super-simple parsing, turning single or multiple & and | into && and ||.
-
#process(string, user) ⇒ Object
Takes a string (which may be a complex conditional string or a single word as a string or symbol) and checks if the user has those roles.
Instance Method Details
#check(role, user) ⇒ Object
The heart of the system, all credit to Ezra for the original algorithm Defaults to false if there is no user or that user does not have a roles association Defaults to true if the role is blank
120 121 122 123 124 |
# File 'lib/simple_access_control.rb', line 120 def check(role, user) return(false) if user.blank? || !user.respond_to?(:roles) return(true) if role.blank? user.roles.map{ |r| r.title.downcase }.include? role.downcase end |
#parse(string) ⇒ Object
Super-simple parsing, turning single or multiple & and | into && and ||. Wraps all the roles in a check call to be evaluated.
113 114 115 |
# File 'lib/simple_access_control.rb', line 113 def parse(string) string.gsub(/(\|+|\&+)/) { $1[0,1]*2 }.gsub(/([^()|&! ]+)/) { "check('#{$1}', user)" } end |
#process(string, user) ⇒ Object
Takes a string (which may be a complex conditional string or a single word as a string or symbol) and checks if the user has those roles
104 105 106 107 108 109 |
# File 'lib/simple_access_control.rb', line 104 def process(string, user) return(check('', user)) if string.blank? if string =~ /^([^()\|&!]+)$/ then check($1, user) # it is simple enough to just pump through else instance_eval("!! (#{parse(string)})") # give it the going-over end end |