Skip to content
This repository

All my 3 pulls merged into one #54

Merged
merged 5 commits into from almost 2 years ago

2 participants

Michael Grosser Daniel Neighman
Michael Grosser

There was a merge conflict, so if you want all of them, just merge this and you'll avoid the conflict :)

Daniel Neighman hassox merged commit 02d4ed5 into from June 06, 2012
Daniel Neighman hassox closed this June 06, 2012
Daniel Neighman
Owner
hassox commented June 06, 2012

Great thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
20  Gemfile.lock
... ...
@@ -1,25 +1,25 @@
1 1
 PATH
2 2
   remote: .
3 3
   specs:
4  
-    warden (1.1.1)
  4
+    warden (1.2.0)
5 5
       rack (>= 1.0)
6 6
 
7 7
 GEM
8 8
   remote: http://rubygems.org/
9 9
   specs:
10  
-    diff-lcs (1.1.2)
  10
+    diff-lcs (1.1.3)
11 11
     rack (1.3.0)
12 12
     rack-test (0.6.0)
13 13
       rack (>= 1.0)
14 14
     rake (0.8.7)
15  
-    rspec (2.5.0)
16  
-      rspec-core (~> 2.5.0)
17  
-      rspec-expectations (~> 2.5.0)
18  
-      rspec-mocks (~> 2.5.0)
19  
-    rspec-core (2.5.1)
20  
-    rspec-expectations (2.5.0)
21  
-      diff-lcs (~> 1.1.2)
22  
-    rspec-mocks (2.5.0)
  15
+    rspec (2.10.0)
  16
+      rspec-core (~> 2.10.0)
  17
+      rspec-expectations (~> 2.10.0)
  18
+      rspec-mocks (~> 2.10.0)
  19
+    rspec-core (2.10.1)
  20
+    rspec-expectations (2.10.0)
  21
+      diff-lcs (~> 1.1.3)
  22
+    rspec-mocks (2.10.1)
23 23
 
24 24
 PLATFORMS
25 25
   ruby
28  lib/warden/hooks.rb
@@ -117,6 +117,34 @@ def _before_failure
117 117
       @_before_failure ||= []
118 118
     end
119 119
 
  120
+    # A callback that runs after any user was fetched, which could be a user or no user.
  121
+    #
  122
+    # Parameters:
  123
+    # <options> Some options which specify when the callback should be executed
  124
+    #   scope  - Executes the callback only if it maches the scope(s) given
  125
+    # <block> A block to contain logic for the callback
  126
+    #   Block Parameters: |user, auth, scope|
  127
+    #     user - The authenticated user for the current scope
  128
+    #     auth - The warden proxy object
  129
+    #     opts - any options passed into the authenticate call including :scope
  130
+    #
  131
+    # Example:
  132
+    #   Warden::Manager.after_any_fetch do |user, auth, opts|
  133
+    #     user.forget_me!
  134
+    #   end
  135
+    #
  136
+    # :api: public
  137
+    def after_any_fetch(options = {}, method = :push, &block)
  138
+      raise BlockNotGiven unless block_given?
  139
+      _after_any_fetch.send(method, [block, options])
  140
+    end
  141
+
  142
+    # Provides access to the callback array for after_any_fetch
  143
+    # :api: private
  144
+    def _after_any_fetch
  145
+      @_after_any_fetch ||= []
  146
+    end
  147
+
120 148
     # A callback that runs just prior to the logout of each scope.
121 149
     #
122 150
     # Parameters:
12  lib/warden/proxy.rb
@@ -192,10 +192,16 @@ def user(argument = {})
192 192
       opts  = argument.is_a?(Hash) ? argument : { :scope => argument }
193 193
       scope = (opts[:scope] ||= @config.default_scope)
194 194
 
195  
-      @users[scope] ||= begin
  195
+      if @users.has_key?(scope)
  196
+        @users[scope]
  197
+      else
196 198
         user = session_serializer.fetch(scope)
197 199
         opts[:event] = :fetch
198  
-        set_user(user, opts) if user
  200
+
  201
+        run_callbacks = opts.fetch(:run_callbacks, true)
  202
+        manager._run_callbacks(:after_any_fetch, user, self, :scope => scope) if run_callbacks
  203
+
  204
+        @users[scope] = user ? set_user(user, opts) : nil
199 205
       end
200 206
     end
201 207
 
@@ -296,7 +302,7 @@ def _perform_authentication(*args)
296 302
 
297 303
       # Look for an existing user in the session for this scope.
298 304
       # If there was no user in the session. See if we can get one from the request.
299  
-      return user, opts if user = user(scope)
  305
+      return user, opts if user = user(opts.merge(:scope => scope))
300 306
       _run_strategies_for(scope, args)
301 307
 
302 308
       if winning_strategy && winning_strategy.user
1  spec/spec_helper.rb
@@ -14,6 +14,7 @@
14 14
 
15 15
 RSpec.configure do |config|
16 16
   config.include(Warden::Spec::Helpers)
  17
+  config.include(Warden::Test::Helpers)
17 18
 
18 19
   def load_strategies
19 20
     Dir[File.join(File.dirname(__FILE__), "helpers", "strategies", "**/*.rb")].each do |f|
28  spec/warden/proxy_spec.rb
@@ -377,7 +377,7 @@
377 377
 
378 378
     it "should run the callbacks when :run_callbacks is true" do
379 379
       app = lambda do |env|
380  
-        env['warden'].manager.should_receive(:_run_callbacks)
  380
+        env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
381 381
         env['warden'].authenticate(:pass)
382 382
         valid_response
383 383
       end
@@ -386,7 +386,7 @@
386 386
 
387 387
     it "should run the callbacks by default" do
388 388
       app = lambda do |env|
389  
-        env['warden'].manager.should_receive(:_run_callbacks)
  389
+        env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
390 390
         env['warden'].authenticate(:pass)
391 391
         valid_response
392 392
       end
@@ -417,6 +417,16 @@
417 417
       setup_rack(app).call(@env)
418 418
     end
419 419
 
  420
+    it "should cache unfound user" do
  421
+      Warden::SessionSerializer.any_instance.should_receive(:fetch).once
  422
+      app = lambda do |env|
  423
+        env['warden'].user.should be_nil
  424
+        env['warden'].user.should be_nil
  425
+        valid_response
  426
+      end
  427
+      setup_rack(app).call(@env)
  428
+    end
  429
+
420 430
     describe "previously logged in" do
421 431
       before(:each) do
422 432
         @env['rack.session']['warden.user.default.key'] = "A Previous User"
@@ -431,6 +441,16 @@
431 441
         setup_rack(app).call(@env)
432 442
       end
433 443
 
  444
+      it "should cache found user" do
  445
+        Warden::SessionSerializer.any_instance.should_receive(:fetch).once.and_return "A Previous User"
  446
+        app = lambda do |env|
  447
+          env['warden'].user.should == "A Previous User"
  448
+          env['warden'].user.should == "A Previous User"
  449
+          valid_response
  450
+        end
  451
+        setup_rack(app).call(@env)
  452
+      end
  453
+
434 454
       it "should not run strategies when the user exists in the session" do
435 455
         app = lambda do |env|
436 456
           env['warden'].authenticate!(:pass)
@@ -452,7 +472,7 @@
452 472
 
453 473
         it "should call run_callbacks when we pass a :run_callback => true" do
454 474
           app = lambda do |env|
455  
-            env['warden'].manager.should_receive(:_run_callbacks)
  475
+            env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
456 476
             env['warden'].user(:run_callbacks => true)
457 477
             valid_response
458 478
           end
@@ -461,7 +481,7 @@
461 481
 
462 482
         it "should call run_callbacks by default" do
463 483
           app = lambda do |env|
464  
-            env['warden'].manager.should_receive(:_run_callbacks)
  484
+            env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
465 485
             env['warden'].user
466 486
             valid_response
467 487
           end
2  spec/warden/test/helpers_spec.rb
@@ -2,8 +2,6 @@
2 2
 require 'spec_helper'
3 3
 
4 4
 describe Warden::Test::Helpers do
5  
-  include Warden::Test::Helpers
6  
-
7 5
   before{ $captures = [] }
8 6
   after{ Warden.test_reset! }
9 7
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.