From 06643593811c48ffa6050a48530998f08e085f9a Mon Sep 17 00:00:00 2001 From: "Carlos A. da Silva" Date: Mon, 12 Oct 2009 19:20:12 -0300 Subject: [PATCH] Updating helpers to use sign_in/sign_out instead of authenticate/logout. --- app/controllers/sessions_controller.rb | 6 ++-- lib/devise/controllers/filters.rb | 31 +++++++++++-------- test/controllers/filters_test.rb | 12 +++---- .../app/controllers/admins_controller.rb | 2 +- .../app/controllers/users_controller.rb | 2 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 66fe2d62..b8af4261 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -9,7 +9,7 @@ class SessionsController < ApplicationController # POST /session/sign_in def create - if warden.authenticate(:scope => resource_name) + if sign_in(resource_name) set_flash_message :success, :signed_in redirect_to root_path else @@ -21,8 +21,8 @@ class SessionsController < ApplicationController # GET /session/sign_out # DELETE /session/sign_out def destroy - set_flash_message :success, :signed_out if authenticated?(resource_name) - logout(resource_name) + set_flash_message :success, :signed_out if signed_in?(resource_name) + sign_out(resource_name) redirect_to root_path end diff --git a/lib/devise/controllers/filters.rb b/lib/devise/controllers/filters.rb index b1864287..12d107f3 100644 --- a/lib/devise/controllers/filters.rb +++ b/lib/devise/controllers/filters.rb @@ -5,7 +5,7 @@ module Devise def self.included(base) base.class_eval do helper_method :warden, :signed_in?, :authenticated?, - *Devise.mappings.keys.map { |m| :"current_#{m}" } + *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten end end @@ -15,22 +15,27 @@ module Devise request.env['warden'] end + # Sign in a user through warden + # + def sign_in(scope) + warden.authenticate(:scope => scope) + end + # Check if a user is authenticated or not performing the proper action. # - def authenticate!(scope) + def sign_in!(scope) warden.authenticate!(:scope => scope) end # Proxy to the authenticated? method on warden # - def authenticated?(scope) + def signed_in?(scope) warden.authenticated?(scope) end - alias :signed_in? :authenticated? - # Logout based on scope + # Sign out based on scope # - def logout(scope, *args) + def sign_out(scope, *args) warden.raw_session.inspect # Without this inspect here. The session does not clear. warden.logout(scope, *args) end @@ -43,24 +48,24 @@ module Devise # Example: # # Maps: - # Devise.map :users, :for => [:authenticable] + # Devise.map :user, :for => [:authenticable] # Devise.map :admin, :for => [:authenticable] # # Generated Filters: - # user_authenticate! - # admin_authenticate! + # sign_in_user! + # sign_in_admin! # # Use: - # before_filter :user_authenticate! # Tell devise to use :user map - # before_filter :admin_authenticate! # Tell devise to use :admin map + # before_filter :sign_in_user! # Tell devise to use :user map + # before_filter :sign_in_admin! # Tell devise to use :admin map # Devise.mappings.each_key do |mapping| class_eval <<-METHODS, __FILE__, __LINE__ - def #{mapping}_authenticate! + def sign_in_#{mapping}! warden.authenticate!(:scope => :#{mapping}) end - def #{mapping}_authenticated? + def #{mapping}_signed_in? warden.authenticated?(:#{mapping}) end diff --git a/test/controllers/filters_test.rb b/test/controllers/filters_test.rb index cffa6cd4..6e8c06d8 100644 --- a/test/controllers/filters_test.rb +++ b/test/controllers/filters_test.rb @@ -31,7 +31,7 @@ class ControllerAuthenticableTest < ActionController::TestCase test 'run authenticate? with scope on warden' do @mock_warden.expects(:authenticated?).with(:my_scope) - @controller.authenticated?(:my_scope) + @controller.signed_in?(:my_scope) end test 'proxy signed_in? to authenticated' do @@ -49,27 +49,27 @@ class ControllerAuthenticableTest < ActionController::TestCase test 'proxy logout to warden' do @mock_warden.expects(:logout).with(:user).returns(true) - @controller.logout(:user) + @controller.sign_out(:user) end test 'proxy user_authenticate! to authenticate with user scope' do @mock_warden.expects(:authenticate!).with(:scope => :user) - @controller.user_authenticate! + @controller.sign_in_user! end test 'proxy admin_authenticate! to authenticate with admin scope' do @mock_warden.expects(:authenticate!).with(:scope => :admin) - @controller.admin_authenticate! + @controller.sign_in_admin! end test 'proxy user_authenticated? to authenticate with user scope' do @mock_warden.expects(:authenticated?).with(:user) - @controller.user_authenticated? + @controller.user_signed_in? end test 'proxy admin_authenticated? to authenticate with admin scope' do @mock_warden.expects(:authenticated?).with(:admin) - @controller.admin_authenticated? + @controller.admin_signed_in? end test 'require no authentication tests current mapping' do diff --git a/test/rails_app/app/controllers/admins_controller.rb b/test/rails_app/app/controllers/admins_controller.rb index c2d68742..debc30d6 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 :admin_authenticate! + before_filter :sign_in_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 881a860a..63a38e86 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 :user_authenticate! + before_filter :sign_in_user! def index end