Rely on duck type instead of mappings settings.
This commit is contained in:
parent
1cf4dc798d
commit
60fd9d26ea
|
@ -1,3 +1,6 @@
|
|||
* enhancements
|
||||
* Added Http Basic Authentication support
|
||||
|
||||
== 0.9.2
|
||||
|
||||
* bug fix
|
||||
|
|
1
TODO
1
TODO
|
@ -1,4 +1,3 @@
|
|||
* Make test run with DataMapper
|
||||
* Add Registerable support
|
||||
* Add http authentication support
|
||||
* Extract Activatable tests from Confirmable
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
@mapping ||= begin
|
||||
mapping = Devise.mappings[scope]
|
||||
raise "Could not find mapping for #{scope}" unless mapping
|
||||
mapping
|
||||
end
|
||||
|
||||
# Store this class type.
|
||||
def klass_type
|
||||
@klass_type ||= self.class.name.split("::").last.underscore.to_sym
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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!
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue