Permalink
Browse files

Skip authentication filters by default on Devise controllers and add …

…devise_controller? to select/reject other filters.
  • Loading branch information...
1 parent 48ef471 commit 1db50dee36f84bc28fae24eb6f0353a689fc4967 José Valim committed Nov 6, 2009
View
@@ -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
View
@@ -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
@@ -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:
@@ -6,7 +6,7 @@ module Filters
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 @@ def warden
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)
@@ -7,9 +7,10 @@ module Helpers
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 @@ def devise_mapping
@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 @@ def build_resource
# 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
@@ -87,4 +87,8 @@ def setup
@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
@@ -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
@@ -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

0 comments on commit 1db50de

Please sign in to comment.