Removing rails_warden dependency.

This commit is contained in:
Carlos A. da Silva 2009-10-10 13:52:14 -03:00
parent aefa857ab2
commit 11715cac1f
5 changed files with 106 additions and 19 deletions

View File

@ -5,7 +5,6 @@ Flexible authentication solution for Rails with Warden.
== Dependencies
http://github.com/hassox/warden
http://github.com/hassox/rails_warden
== License

View File

@ -5,13 +5,6 @@ rescue
require 'warden'
end
begin
require 'rails_warden'
rescue
gem 'hassox-rails_warden'
require 'rails_warden'
end
require 'devise/initializers/warden'
module Devise
@ -25,6 +18,8 @@ module Devise
@as = options[:as] || resource.to_s.pluralize
end
# Reload mapped class each time when cache_classes is false
#
def to
return @to if @to
to = resource.to_s.classify.constantize
@ -32,6 +27,8 @@ module Devise
to
end
# Acts as hash
#
def [](key)
send(key)
end

View File

@ -1,20 +1,67 @@
module Devise
module Controllers
# Some helpers taken from RailsWarden.
module Authenticable
# def self.included(base)
# base.class_eval do
def self.included(base)
base.class_eval do
helper_method :warden, :user, :logged_in?
# helper_method :session_path, :session_url,
# :new_session_path, :new_session_url,
# :password_path, :password_url,
# :new_password_path, :new_password_url,
# :confirmation_path, :confirmation_url,
# :new_confirmation_path, :new_confirmation_url
# end
# end
end
end
protected
# The main accessor for the warden proxy instance
#
def warden
request.env['warden']
end
# Proxy to the authenticated? method on warden
#
def authenticated?(*args)
warden.authenticated?(*args)
end
alias_method :logged_in?, :authenticated?
# Access the currently logged in user
#
def user(*args)
warden.user(*args)
end
alias_method :current_user, :user
def user=(user)
warden.set_user user
end
alias_method :current_user=, :user=
# Logout the current user
#
def logout(*args)
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
warden.logout(*args)
end
# Proxy to the authenticate method on warden
#
def authenticate(*args)
warden.authenticate(*args)
end
# Proxy to the authenticate method on warden
#
def authenticate!(*args)
warden.authenticate!(*args)
end
# Helper for use in before_filters where no authentication is required:
# Example:
# before_filter :require_no_authentication, :only => :new

View File

@ -27,7 +27,7 @@ module Devise
private
def resource_name_or_request_path(object=nil)
object ? object.class.name : request.path.split('/').second
object ? object.class.name : request.path
end
end
end

View File

@ -1,16 +1,60 @@
# Taken from RailsWarden, thanks to Hassox. http://github.com/hassox/rails_warden
#
module Warden::Mixins::Common
# Gets the rails request object by default if it's available
def request
return @request if @request
if env['action_controller.rescue.request']
@request = env['action_controller.rescue.request']
else
Rack::Request.new(env)
end
end
def raw_session
request.session
end
def reset_session!
raw_session.inspect # why do I have to inspect it to get it to clear?
raw_session.clear
end
end
# Rails needs the action to be passed in with the params
Warden::Manager.before_failure do |env, opts|
env['warden'].request.params['action'] = 'new'
if request = env["action_controller.rescue.request"]
request.params["action"] = 'new'
end
end
# Session Serialization in. This block determines how the user will
# be stored in the session. If you're using a complex object like an
# ActiveRecord model, it is not a good idea to store the complete object.
# An ID is sufficient
Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
# Session Serialization out. This block gets the user out of the session.
# It should be the reverse of serializing the object into the session
Warden::Manager.serialize_from_session do |klass, id|
klass = case klass
when Class
klass
when String, Symbol
klass.to_s.classify.constantize
end
klass.find(id)
end
# Adds RailsWarden Manager to Rails middleware stack, configuring default devise
# strategy and also the controller who will manage not authenticated users.
#
Rails.configuration.middleware.use RailsWarden::Manager do |manager|
Rails.configuration.middleware.use Warden::Manager do |manager|
manager.default_strategies :devise
manager.failure_app = SessionsController
end
# Configure RailsWarden to call new action inside failure controller when no
# user is authenticated.
#
RailsWarden.unauthenticated_action = 'new'
# Default strategy for signing in a user, based on his email and password.
# If no email and no password are present, no authentication is tryed.
#