From b0a2da72b5d3af247ab389bcd2d6a34e39dda0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 18 Oct 2009 15:01:56 -0200 Subject: [PATCH] Use sign_in and authenticate methods. --- app/controllers/confirmations_controller.rb | 2 +- app/controllers/passwords_controller.rb | 2 +- app/controllers/sessions_controller.rb | 2 +- lib/devise/controllers/filters.rb | 52 +++++++++---------- test/controllers/filters_test.rb | 9 ++-- .../app/controllers/admins_controller.rb | 2 +- .../app/controllers/users_controller.rb | 2 +- 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/app/controllers/confirmations_controller.rb b/app/controllers/confirmations_controller.rb index 51ebc389..3ef3a69a 100644 --- a/app/controllers/confirmations_controller.rb +++ b/app/controllers/confirmations_controller.rb @@ -22,7 +22,7 @@ class ConfirmationsController < ApplicationController self.resource = resource_class.confirm!(:confirmation_token => params[:confirmation_token]) if resource.errors.empty? - sign_in_automatically(resource, resource_name) + sign_in(resource_name, resource) set_flash_message :success, :confirmed redirect_to root_path else diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index d4e9129e..85bbe06c 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -28,7 +28,7 @@ class PasswordsController < ApplicationController self.resource = resource_class.reset_password!(params[resource_name]) if resource.errors.empty? - sign_in_automatically(resource, resource_name) + sign_in(resource_name, resource) set_flash_message :success, :updated redirect_to root_path else diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 7d9afe81..351f65c4 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -9,7 +9,7 @@ class SessionsController < ApplicationController # POST /resource/sign_in def create - if sign_in(resource_name) + if authenticate(resource_name) set_flash_message :success, :signed_in redirect_back_or_to root_path else diff --git a/lib/devise/controllers/filters.rb b/lib/devise/controllers/filters.rb index 11691eba..daed127b 100644 --- a/lib/devise/controllers/filters.rb +++ b/lib/devise/controllers/filters.rb @@ -4,7 +4,7 @@ module Devise def self.included(base) base.class_eval do - helper_method :warden, :signed_in?, :authenticated?, + helper_method :warden, :signed_in?, *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten end end @@ -14,29 +14,31 @@ module Devise request.env['warden'] end - # Sign in a user through warden, but does not take any action (like - # redirect). - def sign_in(scope) + # Attempts to authenticate the given scope by running authentication hooks, + # but does not redirect in case of failures. + def authenticate(scope) warden.authenticate(:scope => scope) end - # Check if a user is authenticated. - def sign_in!(scope) + # Attempts to authenticate the given scope by running authentication hooks, + # redirecting in case of failures. + def authenticate!(scope) warden.authenticate!(:scope => scope) end - # Proxy to the authenticated? method on warden. + # Check if the given scope is signed in session, without running + # authentication hooks. def signed_in?(scope) warden.authenticated?(scope) end - # Set the warden user with the scope, sign in the resource automatically - # (without credentials). - def sign_in_automatically(resource, scope) + # Set the warden user with the scope, signing in the resource automatically, + # without running hooks. + def sign_in(scope, resource) warden.set_user(resource, :scope => scope) end - # Sign out based on scope + # Sign out based on scope. def sign_out(scope, *args) warden.raw_session.inspect # Without this inspect here. The session does not clear. warden.logout(scope, *args) @@ -52,27 +54,23 @@ module Devise # User => :authenticable # Admin => :authenticable # - # Generated Filters: - # sign_in_user! - # sign_in_admin! + # Generated methods: + # authenticate_user! # Signs user in or redirect + # authenticate_admin! # Signs admin in or redirect + # user_signed_in? # Checks whether there is an user signed in or not + # admin_signed_in? # Checks whether there is an admin signed in or not + # current_user # Current signed in user + # current_admin # Currend signed in admin + # user_session # Session data available only to the user scope + # admin_session # Session data available only to the admin scope # # Use: - # before_filter :sign_in_user! # Tell devise to use :user map - # before_filter :sign_in_admin! # Tell devise to use :admin map - # - # Generated helpers: - # sign_in_user! # Checks whether there is an user signed in or not - # sign_in_admin! # Checks whether there is an admin signed in or not - # user_signed_in? # Checks whether there is an user signed in or not - # admin_signed_in? # Checks whether there is an admin signed in or not - # current_user # Current signed in user - # current_admin # Currend signed in admin - # user_session # Session data available only to the user scope - # admin_session # Session data available only to the admin scope + # before_filter :authenticate_user! # Tell devise to use :user map + # before_filter :authenticate_admin! # Tell devise to use :admin map # Devise.mappings.each_key do |mapping| class_eval <<-METHODS, __FILE__, __LINE__ - def sign_in_#{mapping}! + def authenticate_#{mapping}! warden.authenticate!(:scope => :#{mapping}) end diff --git a/test/controllers/filters_test.rb b/test/controllers/filters_test.rb index 07d243b3..f9107d74 100644 --- a/test/controllers/filters_test.rb +++ b/test/controllers/filters_test.rb @@ -54,12 +54,12 @@ class ControllerAuthenticableTest < ActionController::TestCase test 'proxy user_authenticate! to authenticate with user scope' do @mock_warden.expects(:authenticate!).with(:scope => :user) - @controller.sign_in_user! + @controller.authenticate_user! end test 'proxy admin_authenticate! to authenticate with admin scope' do @mock_warden.expects(:authenticate!).with(:scope => :admin) - @controller.sign_in_admin! + @controller.authenticate_admin! end test 'proxy user_authenticated? to authenticate with user scope' do @@ -90,8 +90,7 @@ class ControllerAuthenticableTest < ActionController::TestCase end test 'sign in automatically proxy to set user on warden' do - user = OpenStruct.new - @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true) - @controller.sign_in_automatically(user, :user) + @mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true) + @controller.sign_in(:user, user) end end diff --git a/test/rails_app/app/controllers/admins_controller.rb b/test/rails_app/app/controllers/admins_controller.rb index debc30d6..6c1fa3b5 100644 --- a/test/rails_app/app/controllers/admins_controller.rb +++ b/test/rails_app/app/controllers/admins_controller.rb @@ -1,5 +1,5 @@ class AdminsController < ApplicationController - before_filter :sign_in_admin! + before_filter :authenticate_admin! def index end diff --git a/test/rails_app/app/controllers/users_controller.rb b/test/rails_app/app/controllers/users_controller.rb index 1267d8b9..e9cc204f 100644 --- a/test/rails_app/app/controllers/users_controller.rb +++ b/test/rails_app/app/controllers/users_controller.rb @@ -1,5 +1,5 @@ class UsersController < ApplicationController - before_filter :sign_in_user! + before_filter :authenticate_user! def index user_session[:cart] = "Cart"