From 60fd9d26ea9072101474e70f40d1cb0dcae181a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 6 Feb 2010 10:06:22 +0100 Subject: [PATCH] Rely on duck type instead of mappings settings. --- CHANGELOG.rdoc | 3 +++ TODO | 1 - lib/devise/mapping.rb | 5 ++++- lib/devise/strategies/authenticatable.rb | 2 +- lib/devise/strategies/base.rb | 18 +++++------------- lib/devise/strategies/http_authenticatable.rb | 2 +- lib/devise/strategies/rememberable.rb | 2 +- lib/devise/strategies/token_authenticatable.rb | 2 +- 8 files changed, 16 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 396007e7..38cd60c5 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ +* enhancements + * Added Http Basic Authentication support + == 0.9.2 * bug fix diff --git a/TODO b/TODO index 96104125..71667727 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ * Make test run with DataMapper * Add Registerable support -* Add http authentication support * Extract Activatable tests from Confirmable \ No newline at end of file diff --git a/lib/devise/mapping.rb b/lib/devise/mapping.rb index 7a4a3ae7..1269f8b4 100644 --- a/lib/devise/mapping.rb +++ b/lib/devise/mapping.rb @@ -36,7 +36,10 @@ module Devise # Find a mapping by a given class. It takes into account single table inheritance as well. def self.find_by_class(klass) - Devise.mappings.values.find { |m| return m if klass <= m.to } + Devise.mappings.each_value do |mapping| + return mapping if klass <= mapping.to + end + nil end # Receives an object and find a scope for it. If a scope cannot be found, diff --git a/lib/devise/strategies/authenticatable.rb b/lib/devise/strategies/authenticatable.rb index b1217047..3b298e4b 100644 --- a/lib/devise/strategies/authenticatable.rb +++ b/lib/devise/strategies/authenticatable.rb @@ -6,7 +6,7 @@ module Devise # Redirects to sign_in page if it's not authenticated class Authenticatable < Base def valid? - super && params[scope] && params[scope][:password].present? + params[scope] && params[scope][:password].present? && mapping.to.respond_to?(:authenticate) end # Authenticate a user based on email and password params, returning to warden diff --git a/lib/devise/strategies/base.rb b/lib/devise/strategies/base.rb index f769a48f..43885d51 100644 --- a/lib/devise/strategies/base.rb +++ b/lib/devise/strategies/base.rb @@ -2,22 +2,14 @@ module Devise module Strategies # Base strategy for Devise. Responsible for verifying correct scope and mapping. class Base < ::Warden::Strategies::Base - # Validate strategy. By default will raise an error if no scope or an - # invalid mapping is found. - def valid? - raise "Could not find mapping for #{scope}" unless mapping - mapping.for.include?(klass_type) - end - # Checks if a valid scope was given for devise and find mapping based on # this scope. def mapping - Devise.mappings[scope] - end - - # Store this class type. - def klass_type - @klass_type ||= self.class.name.split("::").last.underscore.to_sym + @mapping ||= begin + mapping = Devise.mappings[scope] + raise "Could not find mapping for #{scope}" unless mapping + mapping + end end end end diff --git a/lib/devise/strategies/http_authenticatable.rb b/lib/devise/strategies/http_authenticatable.rb index d2eaef3b..31b55c3a 100644 --- a/lib/devise/strategies/http_authenticatable.rb +++ b/lib/devise/strategies/http_authenticatable.rb @@ -5,7 +5,7 @@ module Devise # Sign in an user using HTTP authentication. class HttpAuthenticatable < Base def valid? - http_authentication? + http_authentication? && mapping.to.respond_to?(:authenticate_with_http) end def authenticate! diff --git a/lib/devise/strategies/rememberable.rb b/lib/devise/strategies/rememberable.rb index cd87ae25..9b3a5382 100644 --- a/lib/devise/strategies/rememberable.rb +++ b/lib/devise/strategies/rememberable.rb @@ -10,7 +10,7 @@ module Devise # A valid strategy for rememberable needs a remember token in the cookies. def valid? - super && remember_me_cookie.present? + remember_me_cookie.present? && mapping.to.respond_to?(:serialize_from_cookie) end # To authenticate a user we deserialize the cookie and attempt finding diff --git a/lib/devise/strategies/token_authenticatable.rb b/lib/devise/strategies/token_authenticatable.rb index 3de18af9..d8baf545 100644 --- a/lib/devise/strategies/token_authenticatable.rb +++ b/lib/devise/strategies/token_authenticatable.rb @@ -6,7 +6,7 @@ module Devise # Redirects to sign_in page if it's not authenticated. class TokenAuthenticatable < Base def valid? - super && authentication_token(scope).present? + mapping.to.respond_to?(:authenticate_with_token) && authentication_token(scope).present? end # Authenticate a user based on authenticatable token params, returning to warden