Class: Rails::Auth::ACL::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/auth/acl/resource.rb

Overview

Rules for a particular route

Constant Summary collapse

HTTP_METHODS =

Valid HTTP methods

%w[GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK].freeze
VALID_OPTIONS =

Options allowed for resource matchers

%w[method path host].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, matchers) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • :matchers (Hash)

    which matchers are used for this resource

  • :options (Hash)

    a customizable set of options

Raises:

  • (TypeError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails/auth/acl/resource.rb', line 20

def initialize(options, matchers)
  raise TypeError, "expected Hash for options"  unless options.is_a?(Hash)
  raise TypeError, "expected Hash for matchers" unless matchers.is_a?(Hash)

  unless (extra_keys = options.keys - VALID_OPTIONS).empty?
    raise ParseError, "unrecognized key in ACL resource: #{extra_keys.first}"
  end

  methods = options["method"] || raise(ParseError, "no 'method' key in resource: #{options.inspect}")
  path    = options["path"]   || raise(ParseError, "no 'path' key in resource: #{options.inspect}")

  @http_methods = extract_methods(methods)
  @path         = /\A#{path}\z/
  @matchers     = matchers.freeze

  # Unlike method and path, host is optional
  host = options["host"]
  @host = /\A#{host}\z/ if host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/rails/auth/acl/resource.rb', line 8

def host
  @host
end

#http_methodsObject (readonly)

Returns the value of attribute http_methods.



8
9
10
# File 'lib/rails/auth/acl/resource.rb', line 8

def http_methods
  @http_methods
end

#matchersObject (readonly)

Returns the value of attribute matchers.



8
9
10
# File 'lib/rails/auth/acl/resource.rb', line 8

def matchers
  @matchers
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/rails/auth/acl/resource.rb', line 8

def path
  @path
end

Instance Method Details

#match(env) ⇒ String?

Match this resource against the given Rack environment, checking all matchers to ensure at least one of them matches

Parameters:

  • :env (Hash)

    Rack environment

Returns:

  • (String, nil)

    name of the matcher which matched, or nil if none matched



47
48
49
50
51
52
# File 'lib/rails/auth/acl/resource.rb', line 47

def match(env)
  return nil unless match!(env)

  name, = @matchers.find { |_name, matcher| matcher.match(env) }
  name
end

#match!(env) ⇒ Boolean

Match only the request method/path/host against the given Rack environment. matchers are NOT checked.

Parameters:

  • :env (Hash)

    Rack environment

Returns:

  • (Boolean)

    method and path only match the given environment



61
62
63
64
65
66
67
# File 'lib/rails/auth/acl/resource.rb', line 61

def match!(env)
  return false unless @http_methods.include?(env["REQUEST_METHOD"])
  return false unless @path =~ env["PATH_INFO"]
  return false unless @host.nil? || @host =~ env["HTTP_HOST"]

  true
end