Class: SimpleAccessControl::AccessControlHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_access_control/access_control_handler.rb

Instance Method Summary collapse

Instance Method Details

#check(role, user) ⇒ Object

Check if a role applies to a user

check(‘admin’, user)

Returns:

  • Boolean



34
35
36
37
38
39
40
41
42
# File 'lib/simple_access_control/access_control_handler.rb', line 34

def check(role, user)
  if user.nil? || !user.respond_to?(:roles)
    false
  elsif (role.nil? || role.empty?)
    true
  else
    user.roles.map{ |r| r.title.downcase }.include? role.downcase
  end
end

#parse(string) ⇒ Object

Parse a string and do some horrific ruby magic to instance eval it! : (



25
26
27
# File 'lib/simple_access_control/access_control_handler.rb', line 25

def parse(string)
  string.gsub(/(\|+|\&+)/) { $1[0,1]*2 }.gsub(/([^()|&! ]+)/) { "check('#{$1}', user)" }
end

#process(string, user) ⇒ Object

Process a string rule and a user



13
14
15
16
17
18
19
20
21
# File 'lib/simple_access_control/access_control_handler.rb', line 13

def process(string, user)
  return check('', user) if (string.nil? || string.empty?)
  if simple_rule?(string)
    check(string, user)
  else
    # This is awful!
    instance_eval("!! (#{parse(string)})") # give it the going-over
  end
end

#simple_rule?(string) ⇒ Boolean

Is this is a simple or compound rule?

Returns:

  • (Boolean)

    Boolean



7
8
9
# File 'lib/simple_access_control/access_control_handler.rb', line 7

def simple_rule? string
  string =~ /^([^()\|&!]+)$/
end