Permalink
Browse files

Merge pull request #42 from rodrigoflores/master

Adding run_callbacks option to user.
  • Loading branch information...
josevalim committed Feb 15, 2012
2 parents fb74780 + f79fdf2 commit 3d9aaa32c978e8536447460c6d83b68c696db39b
Showing with 57 additions and 7 deletions.
  1. +25 −4 lib/warden/proxy.rb
  2. +32 −3 spec/warden/proxy_spec.rb
View
@@ -144,7 +144,7 @@ def unauthenticated?(scope = @config.default_scope)
#
# Parameters:
# user - An object that has been setup to serialize into and out of the session.
# opts - An options hash. Use the :scope option to set the scope of the user, set the :store option to false to skip serializing into the session.
# opts - An options hash. Use the :scope option to set the scope of the user, set the :store option to false to skip serializing into the session, set the :run_callbacks to false to skip running the callbacks (the default is true).
#
# :api: public
def set_user(user, opts = {})
@@ -178,11 +178,32 @@ def set_user(user, opts = {})
# # with scope
# env['warden'].user(:admin)
#
# # as a Hash
# env['warden'].user(:scope => :admin)
#
# # with default scope and run_callbacks option
# env['warden'].user(:run_callbacks => false)
#
# # with a scope and run_callbacks option
# env['warden'].user(:scope => :admin, :run_callbacks => true)
#
# :api: public
def user(scope = @config.default_scope)
def user(argument = nil)
scope = case argument
when NilClass then @config.default_scope
when Hash then (argument[:scope] || @config.default_scope)
when Symbol then argument
else argument
end
opts = argument.is_a?(Hash) ? argument : { :run_callbacks => true }
@users[scope] ||= begin
user = session_serializer.fetch(scope)
set_user(user, :scope => scope, :event => :fetch) if user
opts.merge!({:scope => scope, :event => :fetch })
set_user(user, opts) if user
end
end
@@ -325,6 +346,6 @@ def _fetch_strategy(name, scope)
raise "Invalid strategy #{name}"
end
end
end # Proxy
end # Warden
View
@@ -369,7 +369,7 @@
it "should not run the callbacks when :run_callbacks is false" do
app = lambda do |env|
env['warden'].manager.should_not_receive(:_run_callbacks)
env['warden'].authenticate(:pass, :run_callbacks => false)
env['warden'].authenticate(:run_callbacks => false, :scope => :pass)
valid_response
end
setup_rack(app).call(@env)
@@ -378,7 +378,7 @@
it "should run the callbacks when :run_callbacks is true" do
app = lambda do |env|
env['warden'].manager.should_receive(:_run_callbacks)
env['warden'].authenticate(:pass, :run_callbacks => true)
env['warden'].authenticate(:pass)
valid_response
end
setup_rack(app).call(@env)
@@ -439,6 +439,35 @@
setup_rack(app).call(@env)
@env['warden.spec.strategies'].should_not include(:pass)
end
describe "run callback option" do
it "should not call run_callbacks when we pass a :run_callback => false" do
app = lambda do |env|
env['warden'].manager.should_not_receive(:_run_callbacks)
env['warden'].user(:run_callbacks => false)
valid_response
end
setup_rack(app).call(@env)
end
it "should call run_callbacks when we pass a :run_callback => true" do
app = lambda do |env|
env['warden'].manager.should_receive(:_run_callbacks)
env['warden'].user(:run_callbacks => true)
valid_response
end
setup_rack(app).call(@env)
end
it "should call run_callbacks by default" do
app = lambda do |env|
env['warden'].manager.should_receive(:_run_callbacks)
env['warden'].user
valid_response
end
setup_rack(app).call(@env)
end
end
end
end
@@ -927,4 +956,4 @@ def wrap_app(app, &blk)
session['warden.user.baz.key'].should == "User"
end
end
end
end

0 comments on commit 3d9aaa3

Please sign in to comment.