Skip to content
This repository

Add a callback that gets called after any successful and failed fetch #52

Merged
merged 1 commit into from about 2 years ago

2 participants

Michael Grosser Daniel Neighman
Michael Grosser

We use this to set the I18n.locale, because we have to also set it for not-logged-in users,
we need a callback that gets called if a user is found or not.

Daniel Neighman hassox merged commit 3a1d2ff into from
Daniel Neighman hassox closed this
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.
2  Gemfile.lock
... ... @@ -1,7 +1,7 @@
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
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:
4 lib/warden/proxy.rb
@@ -195,6 +195,8 @@ def user(argument = {})
195 195 @users[scope] ||= begin
196 196 user = session_serializer.fetch(scope)
197 197 opts[:event] = :fetch
  198 + run_callbacks = opts.fetch(:run_callbacks, true)
  199 + manager._run_callbacks(:after_any_fetch, user, self, :scope => scope) if run_callbacks
198 200 set_user(user, opts) if user
199 201 end
200 202 end
@@ -296,7 +298,7 @@ def _perform_authentication(*args)
296 298
297 299 # Look for an existing user in the session for this scope.
298 300 # 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)
  301 + return user, opts if user = user(opts.merge(:scope => scope))
300 302 _run_strategies_for(scope, args)
301 303
302 304 if winning_strategy && winning_strategy.user
8 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
@@ -452,7 +452,7 @@
452 452
453 453 it "should call run_callbacks when we pass a :run_callback => true" do
454 454 app = lambda do |env|
455   - env['warden'].manager.should_receive(:_run_callbacks)
  455 + env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
456 456 env['warden'].user(:run_callbacks => true)
457 457 valid_response
458 458 end
@@ -461,7 +461,7 @@
461 461
462 462 it "should call run_callbacks by default" do
463 463 app = lambda do |env|
464   - env['warden'].manager.should_receive(:_run_callbacks)
  464 + env['warden'].manager.should_receive(:_run_callbacks).at_least(:once)
465 465 env['warden'].user
466 466 valid_response
467 467 end

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.