From 11715cac1f714e5ce7613dbc67eba8272545191e Mon Sep 17 00:00:00 2001 From: "Carlos A. da Silva" Date: Sat, 10 Oct 2009 13:52:14 -0300 Subject: [PATCH] Removing rails_warden dependency. --- README.rdoc | 1 - lib/devise.rb | 11 ++--- lib/devise/controllers/authenticable.rb | 55 ++++++++++++++++++++++-- lib/devise/controllers/resources.rb | 2 +- lib/devise/initializers/warden.rb | 56 ++++++++++++++++++++++--- 5 files changed, 106 insertions(+), 19 deletions(-) diff --git a/README.rdoc b/README.rdoc index d5caced1..d2202650 100644 --- a/README.rdoc +++ b/README.rdoc @@ -5,7 +5,6 @@ Flexible authentication solution for Rails with Warden. == Dependencies http://github.com/hassox/warden -http://github.com/hassox/rails_warden == License diff --git a/lib/devise.rb b/lib/devise.rb index 41b9bcaf..fcb78874 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -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 diff --git a/lib/devise/controllers/authenticable.rb b/lib/devise/controllers/authenticable.rb index 067585b0..65dea976 100644 --- a/lib/devise/controllers/authenticable.rb +++ b/lib/devise/controllers/authenticable.rb @@ -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 diff --git a/lib/devise/controllers/resources.rb b/lib/devise/controllers/resources.rb index 45db81ee..7f82965f 100644 --- a/lib/devise/controllers/resources.rb +++ b/lib/devise/controllers/resources.rb @@ -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 diff --git a/lib/devise/initializers/warden.rb b/lib/devise/initializers/warden.rb index 24cd4c5a..56b465f9 100644 --- a/lib/devise/initializers/warden.rb +++ b/lib/devise/initializers/warden.rb @@ -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. #