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).freeze
Instance Attribute Summary collapse
-
#http_methods ⇒ Object
readonly
Returns the value of attribute http_methods.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#predicates ⇒ Object
readonly
Returns the value of attribute predicates.
Instance Method Summary collapse
-
#initialize(options, predicates) ⇒ Resource
constructor
A new instance of Resource.
-
#match(env) ⇒ Boolean
Match this resource against the given Rack environment, checking all predicates to ensure at least one of them matches.
-
#match_method_and_path(env) ⇒ Boolean
Match only the request method/path against the given Rack environment.
Constructor Details
#initialize(options, predicates) ⇒ Resource
Returns a new instance of Resource.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rails/auth/acl/resource.rb', line 20 def initialize(, predicates) fail TypeError, "expected Hash for options" unless .is_a?(Hash) fail TypeError, "expected Hash for predicates" unless predicates.is_a?(Hash) unless (extra_keys = .keys - VALID_OPTIONS).empty? fail ParseError, "unrecognized key in ACL resource: #{extra_keys.first}" end @http_methods = extract_methods(["method"]) @path = /\A#{.fetch("path")}\z/ @predicates = predicates.freeze end |
Instance Attribute Details
#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 |
#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 |
#predicates ⇒ Object (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
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.
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 |