2009-10-18 11:15:23 -02:00
|
|
|
module Devise
|
|
|
|
module Strategies
|
|
|
|
# Default strategy for signing in a user, based on his email and password.
|
2009-10-18 11:20:46 -02:00
|
|
|
# Redirects to sign_in page if it's not authenticated
|
2009-10-18 11:15:23 -02:00
|
|
|
class Authenticable < Devise::Strategies::Base
|
|
|
|
|
|
|
|
# Authenticate a user based on email and password params, returning to warden
|
|
|
|
# success and the authenticated user if everything is okay. Otherwise redirect
|
|
|
|
# to sign in page.
|
|
|
|
def authenticate!
|
|
|
|
if valid_attributes? && resource = mapping.to.authenticate(attributes)
|
|
|
|
success!(resource)
|
|
|
|
else
|
|
|
|
store_location
|
2009-10-18 11:20:46 -02:00
|
|
|
redirect!(sign_in_path, :unauthenticated => true)
|
2009-10-18 11:15:23 -02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 00:31:33 -02:00
|
|
|
private
|
2009-10-18 11:15:23 -02:00
|
|
|
|
2009-10-20 00:31:33 -02:00
|
|
|
# Find the attributes for the current mapping.
|
|
|
|
def attributes
|
|
|
|
@attributes ||= params[scope]
|
|
|
|
end
|
2009-10-18 11:15:23 -02:00
|
|
|
|
2009-10-20 00:31:33 -02:00
|
|
|
# Check for the right keys.
|
|
|
|
def valid_attributes?
|
|
|
|
attributes && attributes[:email].present? && attributes[:password].present?
|
|
|
|
end
|
2009-10-18 11:20:46 -02:00
|
|
|
|
2009-10-20 00:31:33 -02:00
|
|
|
# Stores requested uri to redirect the user after signing in. We cannot use
|
|
|
|
# scoped session provided by warden here, since the user is not authenticated
|
|
|
|
# yet, but we still need to store the uri based on scope, so different scopes
|
|
|
|
# would never use the same uri to redirect.
|
|
|
|
def store_location
|
|
|
|
session[:"#{mapping.name}.return_to"] = request.request_uri if request.get?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create path to sign in the resource
|
|
|
|
def sign_in_path
|
|
|
|
"/#{mapping.as}/#{mapping.path_names[:sign_in]}"
|
|
|
|
end
|
2009-10-18 11:15:23 -02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-10-22 17:24:14 -02:00
|
|
|
|
|
|
|
Warden::Strategies.add(:authenticable, Devise::Strategies::Authenticable)
|