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).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, predicates) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • :predicates (Hash)

    matchers for this resource

  • :options (Hash)

    a customizable set of options

Raises:

  • (TypeError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rails/auth/acl/resource.rb', line 20

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

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

  @http_methods = extract_methods(options["method"])
  @path         = /\A#{options.fetch("path")}\z/
  @predicates   = predicates.freeze
end

Instance Attribute Details

#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

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



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

def predicates
  @predicates
end

Instance Method Details

#match(env) ⇒ Boolean

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

Parameters:

  • :env (Hash)

    Rack environment

Returns:

  • (Boolean)

    resource and predicates match the given request



40
41
42
43
# File 'lib/rails/auth/acl/resource.rb', line 40

def match(env)
  return false unless match_method_and_path(env)
  @predicates.any? { |_name, predicate| predicate.match(env) }
end

#match_method_and_path(env) ⇒ Boolean

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

Parameters:

  • :env (Hash)

    Rack environment

Returns:

  • (Boolean)

    method and path only match the given environment



52
53
54
55
56
# File 'lib/rails/auth/acl/resource.rb', line 52

def match_method_and_path(env)
  return false unless @http_methods.nil? || @http_methods.include?(env["REQUEST_METHOD".freeze])
  return false unless @path =~ env["REQUEST_PATH".freeze]
  true
end