From 13e8bc22e34e9a991dfd9c91069011004bddf89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 20 Dec 2010 10:02:27 +0100 Subject: [PATCH] Move duplicate sign_in logic to sign_in(). --- lib/devise/controllers/helpers.rb | 10 +++------- test/controllers/helpers_test.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 834226e5..51b01cf9 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -110,6 +110,8 @@ module Devise if options[:bypass] warden.session_serializer.store(resource, scope) + elsif warden.user(scope) == resource && !options.delete(:force) + # Do nothing. User already signed in and we are not forcing it. else warden.set_user(resource, options.merge!(:scope => scope)) end @@ -199,13 +201,7 @@ module Devise options = args.extract_options! scope = Devise::Mapping.find_scope!(resource_or_scope) resource = args.last || resource_or_scope - - if warden.user(scope) == resource - expire_session_data_after_sign_in! - else - sign_in(scope, resource, options) - end - + sign_in(scope, resource, options) redirect_for_sign_in(scope, resource) end diff --git a/test/controllers/helpers_test.rb b/test/controllers/helpers_test.rb index 83578a6a..ea93ec68 100644 --- a/test/controllers/helpers_test.rb +++ b/test/controllers/helpers_test.rb @@ -90,16 +90,32 @@ class ControllerAuthenticableTest < ActionController::TestCase test 'sign in proxy to set_user on warden' do user = User.new + @mock_warden.expects(:user).returns(nil) @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true) @controller.sign_in(:user, user) end test 'sign in accepts a resource as argument' do user = User.new + @mock_warden.expects(:user).returns(nil) @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true) @controller.sign_in(user) end + test 'does not sign in again if the user is already in' do + user = User.new + @mock_warden.expects(:user).returns(user) + @mock_warden.expects(:set_user).never + @controller.sign_in(user) + end + + test 'sign in again when the user is already in only if force is given' do + user = User.new + @mock_warden.expects(:user).returns(user) + @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true) + @controller.sign_in(user, :force => true) + end + test 'sign in accepts bypass as option' do user = User.new @mock_warden.expects(:session_serializer).returns(serializer = mock())