From 48ef471b753b28b15479883c68db9285479ee43a Mon Sep 17 00:00:00 2001 From: Rich Manalang Date: Wed, 4 Nov 2009 11:34:23 +0800 Subject: [PATCH 1/3] typo --- generators/devise_install/templates/devise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/devise_install/templates/devise.rb b/generators/devise_install/templates/devise.rb index e910d4a5..2617c97d 100644 --- a/generators/devise_install/templates/devise.rb +++ b/generators/devise_install/templates/devise.rb @@ -22,7 +22,7 @@ Devise.setup do |config| # you can configure them inside the config.warden block. The example below # allows you to setup OAuth, using http://github.com/roman/warden_oauth # - # config.manager do |manager| + # config.warden do |manager| # manager.oauth(:twitter) do |twitter| # twitter.consumer_secret = # twitter.consumer_key = From 1db50dee36f84bc28fae24eb6f0353a689fc4967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 6 Nov 2009 09:33:18 -0200 Subject: [PATCH 2/3] Skip authentication filters by default on Devise controllers and add devise_controller? to select/reject other filters. --- CHANGELOG.rdoc | 5 +++++ README.rdoc | 2 +- generators/devise/templates/README | 9 +++++---- lib/devise/controllers/filters.rb | 11 ++++++++++- lib/devise/controllers/helpers.rb | 12 +++++++++--- test/controllers/filters_test.rb | 4 ++++ test/controllers/helpers_test.rb | 6 +++++- test/integration/authenticatable_test.rb | 2 +- 8 files changed, 40 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index da89ea56..fc752d00 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,8 @@ +* enhancements + * skip_before_filter added in Devise controllers + * Use home_or_root_path on require_no_authentication as well + * Added devise_controller?, useful to select or reject filters in ApplicationController + == 0.4.1 * bug fix diff --git a/README.rdoc b/README.rdoc index 0d0762e7..d34e65a6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -154,7 +154,7 @@ After signing in a user, confirming it's account or updating it's password, devi You also need to setup default url options for the mailer, if you are using confirmable or recoverable. Here's is the configuration for development: DeviseMailer.sender = "no-reply@yourapp.com" - ActionMailer::Base.default_url_options = { :host => 'localhost:3000' } + config.action_mailer.default_url_options = { :host => 'localhost:3000' } == Tidying up diff --git a/generators/devise/templates/README b/generators/devise/templates/README index bb46514d..c748d5fa 100644 --- a/generators/devise/templates/README +++ b/generators/devise/templates/README @@ -7,12 +7,13 @@ Some setup you must do manually if you haven't yet: config.action_mailer.default_url_options = { :host => 'localhost:3000' } -It's a Rails required configuration. -In production it must be the actual host your application is deployed to. +It's a Rails required configuration. In production it must be the actual host your application is deployed to. -2. Setup default sender for mails.In config/environment.rb: +2. Setup default sender for mails. In config/environment.rb: - Notifier.sender = "test@example.com" + DeviseMailer.sender = "test@example.com" + +You can also configure this value by running script/generate devise_install and setting config.mailer_sender, 3. Ensure you have defined root_url to *something* in your config/routes.rb: diff --git a/lib/devise/controllers/filters.rb b/lib/devise/controllers/filters.rb index c95c87d6..8157b400 100644 --- a/lib/devise/controllers/filters.rb +++ b/lib/devise/controllers/filters.rb @@ -6,7 +6,7 @@ module Devise def self.included(base) base.class_eval do - helper_method :warden, :signed_in?, + helper_method :warden, :signed_in?, :devise_controller?, *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten end end @@ -16,6 +16,15 @@ module Devise request.env['warden'] end + # Return true if it's a devise_controller. false to all controllers unless + # the controllers defined inside devise. Useful if you want to apply a before + # filter to all controller, except the ones in devise: + # + # before_filter :my_filter, :unless => { |c| c.devise_controller? } + def devise_controller? + false + end + # Attempts to authenticate the given scope by running authentication hooks, # but does not redirect in case of failures. def authenticate(scope) diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index ce8ae09d..0b21a10f 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -7,9 +7,10 @@ module Devise def self.included(base) base.class_eval do - helper_method :resource, :resource_name, :resource_class, :devise_mapping - hide_action :resource, :resource_name, :resource_class, :devise_mapping + helper_method :resource, :resource_name, :resource_class, :devise_mapping, :devise_controller? + hide_action :resource, :resource_name, :resource_class, :devise_mapping, :devise_controller? + skip_before_filter *Devise.mappings.keys.map { |m| :"authenticate_#{m}!" } before_filter :is_devise_resource? end end @@ -34,6 +35,11 @@ module Devise @devise_mapping ||= Devise.find_mapping_by_path(request.path) end + # Overwrites devise_controller? to return true + def devise_controller? + true + end + protected # Redirects to stored uri before signing in or the default path and clear @@ -91,7 +97,7 @@ module Devise # Example: # before_filter :require_no_authentication, :only => :new def require_no_authentication - redirect_to root_path if warden.authenticated?(resource_name) + redirect_to home_or_root_path if warden.authenticated?(resource_name) end # Sets the flash message with :key, using I18n. By default you are able diff --git a/test/controllers/filters_test.rb b/test/controllers/filters_test.rb index 15139607..aff45bb6 100644 --- a/test/controllers/filters_test.rb +++ b/test/controllers/filters_test.rb @@ -87,4 +87,8 @@ class ControllerAuthenticableTest < ActionController::TestCase @mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true) @controller.sign_in(:user, user) end + + test 'is not a devise controller' do + assert_not @controller.devise_controller? + end end diff --git a/test/controllers/helpers_test.rb b/test/controllers/helpers_test.rb index 02756525..66ff85e9 100644 --- a/test/controllers/helpers_test.rb +++ b/test/controllers/helpers_test.rb @@ -43,9 +43,13 @@ class HelpersTest < ActionController::TestCase end test 'require no authentication tests current mapping' do - @controller.expects(:resource_name).returns(:user) + @controller.expects(:resource_name).returns(:user).twice @mock_warden.expects(:authenticated?).with(:user).returns(true) @controller.expects(:redirect_to).with(root_path) @controller.send :require_no_authentication end + + test 'is a devise controller' do + assert @controller.devise_controller? + end end diff --git a/test/integration/authenticatable_test.rb b/test/integration/authenticatable_test.rb index f58ecc39..11a8200c 100644 --- a/test/integration/authenticatable_test.rb +++ b/test/integration/authenticatable_test.rb @@ -114,7 +114,7 @@ class AuthenticationTest < ActionController::IntegrationTest get new_admin_session_path assert_response :redirect - assert_redirected_to root_path + assert_redirected_to admin_root_path assert warden.authenticated?(:admin) end From 0c088a7420634291d0115bd12fb1d427e89899c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 6 Nov 2009 09:40:38 -0200 Subject: [PATCH 3/3] Renamed mail_sender to mailer_sender. --- CHANGELOG.rdoc | 3 +++ generators/devise_install/templates/devise.rb | 2 +- lib/devise.rb | 11 ++++++++--- test/devise_test.rb | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index fc752d00..712e140f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ +* deprecations + * Renamed mail_sender to mailer_sender + * enhancements * skip_before_filter added in Devise controllers * Use home_or_root_path on require_no_authentication as well diff --git a/generators/devise_install/templates/devise.rb b/generators/devise_install/templates/devise.rb index 2617c97d..08c4dfe2 100644 --- a/generators/devise_install/templates/devise.rb +++ b/generators/devise_install/templates/devise.rb @@ -16,7 +16,7 @@ Devise.setup do |config| # config.remember_for = 2.weeks # Configure the e-mail address which will be shown in DeviseMailer. - # config.mail_sender = "foo.bar@yourapp.com" + # config.mailer_sender = "foo.bar@yourapp.com" # If you want to use other strategies, that are not (yet) supported by Devise, # you can configure them inside the config.warden block. The example below diff --git a/lib/devise.rb b/lib/devise.rb index c3c6644c..51fe2cce 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -21,11 +21,16 @@ module Devise yield self end - # Sets the sender in DeviseMailer. - def mail_sender=(value) + def mail_sender=(value) #:nodoc: + ActiveSupport::Deprecation.warn "Devise.mail_sender= is deprecated, use Devise.mailer_sender instead" DeviseMailer.sender = value end - alias :sender= :mail_sender= + + # Sets the sender in DeviseMailer. + def mailer_sender=(value) + DeviseMailer.sender = value + end + alias :sender= :mailer_sender= # Sets warden configuration using a block that will be invoked on warden # initialization. diff --git a/test/devise_test.rb b/test/devise_test.rb index 4b7c7ba1..baf6f787 100644 --- a/test/devise_test.rb +++ b/test/devise_test.rb @@ -27,7 +27,7 @@ class DeviseTest < ActiveSupport::TestCase test 'DeviseMailer.sender can be configured through Devise' do swap DeviseMailer, :sender => "foo@bar" do assert_equal "foo@bar", DeviseMailer.sender - Devise.mail_sender = "bar@foo" + Devise.mailer_sender = "bar@foo" assert_equal "bar@foo", DeviseMailer.sender end end