mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Simplify validation logic inside strategies
This commit is contained in:
parent
d67d992749
commit
d4e5424360
5 changed files with 23 additions and 9 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
* deprecations
|
||||||
|
* Strategy#validate() no longer validates nil resources
|
||||||
|
|
||||||
== 2.1.0
|
== 2.1.0
|
||||||
|
|
||||||
Notes: https://github.com/plataformatec/devise/wiki/How-to:-upgrade-to-devise-2.1
|
Notes: https://github.com/plataformatec/devise/wiki/How-to:-upgrade-to-devise-2.1
|
||||||
|
|
|
@ -18,13 +18,24 @@ module Devise
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Simply invokes valid_for_authentication? with the given block and deal with the result.
|
# Receives a resource and check if it is valid by calling valid_for_authentication?
|
||||||
|
# An optional block that will be triggered while validating can be optionally
|
||||||
|
# given as parameter. Check Devise::Models::Authenticable.valid_for_authentication?
|
||||||
|
# for more information.
|
||||||
|
#
|
||||||
|
# In case the resource can't be validated, it will fail with the given
|
||||||
|
# unauthenticated_message.
|
||||||
def validate(resource, &block)
|
def validate(resource, &block)
|
||||||
|
unless resource
|
||||||
|
ActiveSupport::Depreation.warn "an empty resource was given to #{self.class.name}#validate. " \
|
||||||
|
"Please ensure the resource is not nil", caller
|
||||||
|
end
|
||||||
|
|
||||||
result = resource && resource.valid_for_authentication?(&block)
|
result = resource && resource.valid_for_authentication?(&block)
|
||||||
|
|
||||||
case result
|
case result
|
||||||
when Symbol, String
|
when Symbol, String
|
||||||
ActiveSupport::Deprecation.warn "valid_for_authentication should return a boolean value"
|
ActiveSupport::Deprecation.warn "valid_for_authentication? should return a boolean value"
|
||||||
fail!(result)
|
fail!(result)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,12 +6,11 @@ module Devise
|
||||||
class DatabaseAuthenticatable < Authenticatable
|
class DatabaseAuthenticatable < Authenticatable
|
||||||
def authenticate!
|
def authenticate!
|
||||||
resource = valid_password? && mapping.to.find_for_database_authentication(authentication_hash)
|
resource = valid_password? && mapping.to.find_for_database_authentication(authentication_hash)
|
||||||
|
return fail(:invalid) unless resource
|
||||||
|
|
||||||
if validate(resource){ resource.valid_password?(password) }
|
if validate(resource){ resource.valid_password?(password) }
|
||||||
resource.after_database_authentication
|
resource.after_database_authentication
|
||||||
success!(resource)
|
success!(resource)
|
||||||
elsif !halted?
|
|
||||||
fail(:invalid)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,11 +19,13 @@ module Devise
|
||||||
def authenticate!
|
def authenticate!
|
||||||
resource = mapping.to.serialize_from_cookie(*remember_cookie)
|
resource = mapping.to.serialize_from_cookie(*remember_cookie)
|
||||||
|
|
||||||
|
unless resource
|
||||||
|
cookies.delete(remember_key)
|
||||||
|
return pass
|
||||||
|
end
|
||||||
|
|
||||||
if validate(resource)
|
if validate(resource)
|
||||||
success!(resource)
|
success!(resource)
|
||||||
elsif !halted?
|
|
||||||
cookies.delete(remember_key)
|
|
||||||
pass
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,11 @@ module Devise
|
||||||
|
|
||||||
def authenticate!
|
def authenticate!
|
||||||
resource = mapping.to.find_for_token_authentication(authentication_hash)
|
resource = mapping.to.find_for_token_authentication(authentication_hash)
|
||||||
|
return fail(:invalid_token) unless resource
|
||||||
|
|
||||||
if validate(resource)
|
if validate(resource)
|
||||||
resource.after_token_authentication
|
resource.after_token_authentication
|
||||||
success!(resource)
|
success!(resource)
|
||||||
elsif !halted?
|
|
||||||
fail(:invalid_token)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue