Skip to content

Commit 47dde82

Browse files
author
Alexander Wenzowski
committed
authentication redirection improvement #1724
- add tests for #1561 behaviour - use unscoped session key to enable store_location to be used for login #1724
1 parent ecfbfe8 commit 47dde82

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

Diff for: authentication/lib/refinery/authenticated_system.rb

+32-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,45 @@ def store_location
77
session[:return_to] = request.fullpath.sub("//", "/")
88
end
99

10+
# Clear and return the stored location
11+
def pop_stored_location
12+
session.delete(:return_to)
13+
end
14+
1015
# Redirect to the URI stored by the most recent store_location call or
1116
# to the passed default.
1217
def redirect_back_or_default(default)
13-
redirect_to(session[:return_to] || default)
14-
session[:return_to] = nil
18+
redirect_to(pop_stored_location || default)
1519
end
1620

17-
# This just defines the devise method for after sign in to support
18-
# extension namespace isolation...
19-
def after_sign_in_path_for(resource_or_scope)
21+
# Overrides default devise paths with refinery routes
22+
def signed_in_root_path(resource_or_scope)
2023
scope = Devise::Mapping.find_scope!(resource_or_scope)
2124
home_path = "#{scope}_root_path"
22-
respond_to?(home_path, true) ? refinery.send(home_path) : refinery.admin_root_path
25+
if respond_to?(home_path, true)
26+
refinery.send(home_path)
27+
elsif respond_to?(:admin_root_path)
28+
refinery.admin_root_path
29+
else
30+
"/"
31+
end
32+
end
33+
34+
# Trims the sneaky "//" from the popped stored url and returns it.
35+
#
36+
# Making sure bad urls aren't stored should probably be
37+
# a part of the Devise::FailureApp
38+
def sanitized_stored_location_for(resource_or_scope)
39+
location = stored_location_for(resource_or_scope)
40+
location.sub!("//", "/") if location.respond_to?(:sub!)
41+
location
42+
end
43+
44+
# Adds support for unscoped redirect_back_or_default key to devise default
45+
def after_sign_in_path_for(resource_or_scope)
46+
pop_stored_location ||
47+
sanitized_stored_location_for(resource_or_scope) ||
48+
signed_in_root_path(resource_or_scope)
2349
end
2450

2551
def after_sign_out_path_for(resource_or_scope)

Diff for: authentication/spec/requests/refinery/sessions_spec.rb

+42-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
module Refinery
44
describe "sign in" do
5+
let(:login_path) { refinery.new_refinery_user_session_path }
6+
let(:login_retry_path) { refinery.refinery_user_session_path }
7+
let(:admin_path) { refinery.admin_root_path }
8+
59
before(:each) do
610
FactoryGirl.create(:refinery_user, :username => "ugisozols",
711
:password => "123456",
812
:password_confirmation => "123456")
9-
visit refinery.new_refinery_user_session_path
13+
visit login_path
1014
end
1115

1216
it "shows login form" do
@@ -21,6 +25,7 @@ module Refinery
2125
fill_in "Password", :with => "123456"
2226
click_button "Sign in"
2327
page.should have_content("Signed in successfully.")
28+
current_path.should == admin_path
2429
end
2530
end
2631

@@ -30,6 +35,7 @@ module Refinery
3035
fill_in "Password", :with => "Hmmm"
3136
click_button "Sign in"
3237
page.should have_content("Sorry, your login or password was incorrect.")
38+
current_path.should == login_retry_path
3339
end
3440
end
3541
end
@@ -59,4 +65,39 @@ module Refinery
5965
end
6066
end
6167
end
68+
69+
describe 'redirects' do
70+
let(:protected_path) { refinery.new_admin_user_path }
71+
let(:login_path) { refinery.new_refinery_user_session_path }
72+
73+
before(:each) do
74+
FactoryGirl.create(:refinery_user,
75+
:username => "ugisozols",
76+
:password => "123456",
77+
:password_confirmation => "123456"
78+
)
79+
end
80+
81+
context "when visiting a protected path" do
82+
before(:each) { visit protected_path }
83+
84+
it "redirects to the login" do
85+
current_path.should == login_path
86+
end
87+
88+
it "shows login form" do
89+
page.should have_content("Hello! Please sign in.")
90+
page.should have_content("I forgot my password")
91+
page.should have_selector("a[href*='/refinery/users/password/new']")
92+
end
93+
94+
it "redirects to the protected path on login" do
95+
fill_in "Login", :with => "ugisozols"
96+
fill_in "Password", :with => "123456"
97+
page.click_button "Sign in"
98+
current_path.should == protected_path
99+
end
100+
end
101+
102+
end
62103
end

0 commit comments

Comments
 (0)