From 1924a915a82b7933b7a527dafdabeff7b0e3a664 Mon Sep 17 00:00:00 2001 From: Denis Lifanov Date: Thu, 24 Jun 2010 03:03:27 +0800 Subject: [PATCH] sign_out_everybody helper (as a convenient proxy to warden) --- lib/devise/controllers/helpers.rb | 17 +++++++++++++++++ test/controllers/helpers_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 7e743344..d4b2f8a2 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -64,6 +64,13 @@ module Devise warden.logout(scope) end + def sign_out_everybody + # Not "warden.logout" since we need to sign_out only devise-defined scopes. + Devise.mappings.keys.each { |scope| warden.user(scope) } + warden.raw_session.inspect + warden.logout(*Devise.mappings.keys) + end + # Returns and delete the url stored in the session for the given scope. Useful # for giving redirect backs after sign up: # @@ -148,6 +155,10 @@ module Devise root_path end + def after_sign_out_everybody_path_for(resource_or_scope) + after_sign_out_path_for(resource_or_scope) + end + # Sign in an user and tries to redirect first to the stored location and # then to the url specified by after_sign_in_path_for. # @@ -168,6 +179,12 @@ module Devise redirect_to after_sign_out_path_for(scope) end + def sign_out_everybody_and_redirect(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) # just to maintain sign_out paths + sign_out_everybody + redirect_to after_sign_out_everybody_path_for(scope) + end + # Define authentication filters and accessor helpers based on mappings. # These filters should be used inside the controllers as before_filters, # so you can control the scope of the user who should be signed in to diff --git a/test/controllers/helpers_test.rb b/test/controllers/helpers_test.rb index 18f9c412..6a48b871 100644 --- a/test/controllers/helpers_test.rb +++ b/test/controllers/helpers_test.rb @@ -126,6 +126,15 @@ class ControllerAuthenticableTest < ActionController::TestCase @controller.sign_out(User.new) end + test 'sign out everybody proxy to logout on warden' do + Devise.mappings.keys.each { |scope| + @mock_warden.expects(:user).with(scope).returns(true) + } + + @mock_warden.expects(:logout).with(*Devise.mappings.keys).returns(true) + @controller.sign_out_everybody + end + test 'stored location for returns the location for a given scope' do assert_nil @controller.stored_location_for(:user) @controller.session[:"user_return_to"] = "/foo.bar" @@ -165,6 +174,13 @@ class ControllerAuthenticableTest < ActionController::TestCase assert_equal root_path, @controller.after_sign_out_path_for(:user) end + test 'after sign out everybody path defaults to the sign out path' do + @controller.expects(:after_sign_out_path_for).with(:admin).returns(:custom_admin_path) + @controller.expects(:after_sign_out_path_for).with(:user).returns(:custom_user_path) + assert_equal :custom_admin_path, @controller.after_sign_out_everybody_path_for(:admin) + assert_equal :custom_user_path, @controller.after_sign_out_everybody_path_for(:user) + end + test 'sign in and redirect uses the stored location' do user = User.new @controller.session[:"user_return_to"] = "/foo.bar" @@ -198,6 +214,13 @@ class ControllerAuthenticableTest < ActionController::TestCase @controller.sign_out_and_redirect(:admin) end + test 'sign out everybody and redirect uses the configured after sign out everybody path' do + @controller.expects(:sign_out_everybody).returns(true) # since we're know that it's a proxy + @controller.expects(:redirect_to).with(admin_root_path) + @controller.instance_eval "def after_sign_out_everybody_path_for(resource); admin_root_path; end" + @controller.sign_out_everybody_and_redirect(:admin) + end + test 'is not a devise controller' do assert_not @controller.devise_controller? end