Module: Simple::Authorisation

Defined in:
lib/simple-authorisation/authorisation.rb,
lib/simple-authorisation/route_rule_finder.rb,
lib/simple-authorisation/no_rules_for_method.rb,
lib/simple-authorisation/no_setting_for_route.rb,
lib/simple-authorisation/exact_route_rule_finder.rb

Defined Under Namespace

Classes: ExactRouteRuleFinder, NoRulesForMethod, NoSettingsForRoute, RouteRuleFinder

Constant Summary collapse

@@match_style =
:default

Class Method Summary collapse

Class Method Details

.clearObject



37
38
39
# File 'lib/simple-authorisation/authorisation.rb', line 37

def self.clear
  @@routes = {}
end

.delete(name, options) ⇒ Object



24
25
26
27
# File 'lib/simple-authorisation/authorisation.rb', line 24

def self.delete(name, options)
  options[:method] = :delete
  self.route(name, options)
end

.get(name, options) ⇒ Object



14
15
16
17
# File 'lib/simple-authorisation/authorisation.rb', line 14

def self.get(name, options)
  options[:method] = :get
  self.route(name, options)
end

.is_allowed?(route_name, options) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/simple-authorisation/authorisation.rb', line 42

def self.is_allowed?(route_name, options)
  match_styles = {
      :default  => RouteRuleFinder,
      :exact    => ExactRouteRuleFinder
  }
  route_matcher = match_styles[match_style].new(@@routes)
  route_settings = route_matcher.find(route_name)

  method = options.fetch(:method, :any)
  route_rules = route_settings[method] || route_settings[:any]
  raise NoRulesForMethod.new(route_name, method) if route_rules.nil?

  allow = route_rules.fetch(:allow, [])
  deny = route_rules.fetch(:deny, [])
  user = options.fetch(:user, nil)

  anonymous_user_class = options.fetch(:anonymous_user_class, NilClass)

  return true   if allow.index('?')

  user = user.call if user.is_a? Proc
  return false  if deny.index('?')  and     user.is_a? anonymous_user_class
  return true   if allow.index('*') and not user.is_a? anonymous_user_class
  allow.each do | allowed |
    return true if user.actions.include?(allowed)
  end if user.respond_to? :actions

  false
end

.match_styleObject



76
77
78
# File 'lib/simple-authorisation/authorisation.rb', line 76

def self.match_style
  @@match_style
end

.match_style=(style) ⇒ Object



72
73
74
# File 'lib/simple-authorisation/authorisation.rb', line 72

def self.match_style=(style)
  @@match_style = style
end

.post(name, options) ⇒ Object



9
10
11
12
# File 'lib/simple-authorisation/authorisation.rb', line 9

def self.post(name, options)
  options[:method] = :post
  self.route(name, options)
end

.put(name, options) ⇒ Object



19
20
21
22
# File 'lib/simple-authorisation/authorisation.rb', line 19

def self.put(name, options)
  options[:method] = :put
  self.route(name, options)
end

.route(name, options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/simple-authorisation/authorisation.rb', line 29

def self.route(name, options)
  @@routes ||= {}
  @@routes[name] = {} unless @@routes.has_key?(name)

  route_settings  = @@routes[name]
  route_settings[options.delete(:method) || :any] = options
end