Class: Rails::Auth::ACL::Resource
- Inherits:
-
Object
- Object
- Rails::Auth::ACL::Resource
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http_methods ⇒ Object
readonly
Returns the value of attribute http_methods.
-
#matchers ⇒ Object
readonly
Returns the value of attribute matchers.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(options, matchers) ⇒ Resource
constructor
A new instance of Resource.
-
#match(env) ⇒ String?
Match this resource against the given Rack environment, checking all matchers to ensure at least one of them matches.
-
#match!(env) ⇒ Boolean
Match only the request method/path/host against the given Rack environment.
Constructor Details
#initialize(options, matchers) ⇒ Resource
Returns a new instance of Resource.
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(, matchers) raise TypeError, "expected Hash for options" unless .is_a?(Hash) raise TypeError, "expected Hash for matchers" unless matchers.is_a?(Hash) unless (extra_keys = .keys - VALID_OPTIONS).empty? raise ParseError, "unrecognized key in ACL resource: #{extra_keys.first}" end methods = ["method"] || raise(ParseError, "no 'method' key in resource: #{.inspect}") path = ["path"] || raise(ParseError, "no 'path' key in resource: #{.inspect}") @http_methods = extract_methods(methods) @path = /\A#{path}\z/ @matchers = matchers.freeze # Unlike method and path, host is optional host = ["host"] @host = /\A#{host}\z/ if host end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/rails/auth/acl/resource.rb', line 8 def host @host end |
#http_methods ⇒ Object (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 |
#matchers ⇒ Object (readonly)
Returns the value of attribute matchers.
8 9 10 |
# File 'lib/rails/auth/acl/resource.rb', line 8 def matchers @matchers end |
#path ⇒ Object (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
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.
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 |