Permalink
Browse files

Merge pull request #56 from grosser/after_failed_fetch

convert after_any_fetch to after_failed_fetch so it is the exact opposite of after_set_user
  • Loading branch information...
2 parents 02d4ed5 + 896d5db commit 68612411ad3ff5c4feb87d439280f7af110fac48 @josevalim josevalim committed Jun 8, 2012
Showing with 41 additions and 14 deletions.
  1. +8 −8 lib/warden/hooks.rb
  2. +5 −6 lib/warden/proxy.rb
  3. +28 −0 spec/warden/hooks_spec.rb
View
@@ -117,7 +117,7 @@ def _before_failure
@_before_failure ||= []
end
- # A callback that runs after any user was fetched, which could be a user or no user.
+ # A callback that runs if no user could be fetched, meaning there is now no user logged in.
#
# Parameters:
# <options> Some options which specify when the callback should be executed
@@ -129,20 +129,20 @@ def _before_failure
# opts - any options passed into the authenticate call including :scope
#
# Example:
- # Warden::Manager.after_any_fetch do |user, auth, opts|
- # user.forget_me!
+ # Warden::Manager.after_failed_fetch do |user, auth, opts|
+ # I18n.locale = :en
# end
#
# :api: public
- def after_any_fetch(options = {}, method = :push, &block)
+ def after_failed_fetch(options = {}, method = :push, &block)
raise BlockNotGiven unless block_given?
- _after_any_fetch.send(method, [block, options])
+ _after_failed_fetch.send(method, [block, options])
end
- # Provides access to the callback array for after_any_fetch
+ # Provides access to the callback array for after_failed_fetch
# :api: private
- def _after_any_fetch
- @_after_any_fetch ||= []
+ def _after_failed_fetch
+ @_after_failed_fetch ||= []
end
# A callback that runs just prior to the logout of each scope.
View
@@ -195,13 +195,12 @@ def user(argument = {})
if @users.has_key?(scope)
@users[scope]
else
- user = session_serializer.fetch(scope)
- opts[:event] = :fetch
+ unless user = session_serializer.fetch(scope)
+ run_callbacks = opts.fetch(:run_callbacks, true)
+ manager._run_callbacks(:after_failed_fetch, user, self, :scope => scope) if run_callbacks
+ end
- run_callbacks = opts.fetch(:run_callbacks, true)
- manager._run_callbacks(:after_any_fetch, user, self, :scope => scope) if run_callbacks
-
- @users[scope] = user ? set_user(user, opts) : nil
+ @users[scope] = user ? set_user(user, opts.merge(:event => :fetch)) : nil
end
end
View
@@ -177,7 +177,35 @@
env['warden.spec.order'].should == [1,2,3]
end
end
+ end
+
+
+ describe "after_failed_fetch" do
+ before(:each) do
+ RAM = Warden::Manager unless defined?(RAM)
+ RAM._after_failed_fetch.clear
+ end
+ after(:each) do
+ RAM._after_failed_fetch.clear
+ end
+
+ it "should not be called when user is fetched" do
+ RAM.after_failed_fetch{|u,a,o| fail }
+ env = env_with_params
+ setup_rack(lambda { |e| valid_response }).call(env)
+ env['rack.session']['warden.user.default.key'] = "Foo"
+ env['warden'].user.should == "Foo"
+ end
+
+ it "should be called if fetched user is nil" do
+ calls = 0
+ RAM.after_failed_fetch{|u,a,o| calls += 1 }
+ env = env_with_params
+ setup_rack(lambda { |e| valid_response }).call(env)
+ env['warden'].user.should be_nil
+ calls.should == 1
+ end
end
describe "before_failure" do

0 comments on commit 6861241

Please sign in to comment.