New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding run_callbacks option to user. #42

Merged
merged 3 commits into from Feb 15, 2012
Jump to file or symbol
Failed to load files and symbols.
+57 −7
Diff settings

Always

Just for now

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
ProTip! Use n and p to navigate between commits in a pull request.