Skip to content

Commit ff57ff5

Browse files
committed
Try to redirect back on unauthorized accesses
Only when the `redirect_back_on_unauthorized` preference exists and is set to true. This preference has been introduced in core with solidusio/solidus#3118 and we can rely on that preference to drive the behavior change here as well. The extra if Spree::Config.respond_to?(:redirect_back_on_unauthorized) check might seem useless but it's needed to avoid printing this deprecation warning on Solidus versions that still do not have the preference. If the Solidus verion used does not have the preference yet, the old behavior will be preserved.
1 parent 1f329b5 commit ff57ff5

3 files changed

Lines changed: 107 additions & 16 deletions

File tree

lib/spree/auth/engine.rb

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,65 @@ class Engine < Rails::Engine
2626
ApplicationController.include Spree::AuthenticationHelpers
2727
end
2828

29+
def self.redirect_back_on_unauthorized?
30+
return false unless Spree::Config.respond_to?(:redirect_back_on_unauthorized)
31+
32+
if Spree::Config.redirect_back_on_unauthorized
33+
true
34+
else
35+
Spree::Deprecation.warn <<-WARN.strip_heredoc, caller
36+
Having Spree::Config.redirect_back_on_unauthorized set
37+
to `false` is deprecated and will not be supported in Solidus 3.0.
38+
Please change this configuration to `true` and be sure that your
39+
application does not break trying to redirect back when there is
40+
an unauthorized access.
41+
WARN
42+
43+
false
44+
end
45+
end
46+
2947
def self.prepare_backend
3048
Spree::Admin::BaseController.unauthorized_redirect = -> do
3149
if try_spree_current_user
3250
flash[:error] = I18n.t('spree.authorization_failure')
33-
redirect_to spree.admin_unauthorized_path
51+
52+
if Spree::Auth::Engine.redirect_back_on_unauthorized?
53+
redirect_back(fallback_location: spree.admin_unauthorized_path)
54+
else
55+
redirect_to spree.admin_unauthorized_path
56+
end
3457
else
3558
store_location
36-
redirect_to spree.admin_login_path
59+
60+
if Spree::Auth::Engine.redirect_back_on_unauthorized?
61+
redirect_back(fallback_location: spree.admin_login_path)
62+
else
63+
redirect_to spree.admin_login_path
64+
end
3765
end
3866
end
3967
end
4068

69+
4170
def self.prepare_frontend
4271
Spree::BaseController.unauthorized_redirect = -> do
4372
if try_spree_current_user
4473
flash[:error] = I18n.t('spree.authorization_failure')
45-
redirect_to spree.unauthorized_path
74+
75+
if Spree::Auth::Engine.redirect_back_on_unauthorized?
76+
redirect_back(fallback_location: spree.unauthorized_path)
77+
else
78+
redirect_to spree.unauthorized_path
79+
end
4680
else
4781
store_location
48-
redirect_to spree.login_path
82+
83+
if Spree::Auth::Engine.redirect_back_on_unauthorized?
84+
redirect_back(fallback_location: spree.login_path)
85+
else
86+
redirect_to spree.login_path
87+
end
4988
end
5089
end
5190
end

spec/controllers/spree/admin/base_controller_spec.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,45 @@
88
def index; authorize!(:read, :something); end
99
end
1010

11+
before do
12+
stub_spree_preferences(Spree::Config, redirect_back_on_unauthorized: true)
13+
end
14+
1115
context "when user is logged in" do
1216
before { sign_in(create(:user)) }
1317

14-
it "redirects to unauthorized path" do
15-
get :index
16-
expect(response).to redirect_to(spree.admin_unauthorized_path)
18+
context "when http_referrer is not present" do
19+
it "redirects to unauthorized path" do
20+
get :index
21+
expect(response).to redirect_to(spree.admin_unauthorized_path)
22+
end
23+
end
24+
25+
context "when http_referrer is present" do
26+
before { request.env['HTTP_REFERER'] = '/redirect' }
27+
28+
it "redirects back" do
29+
get :index
30+
expect(response).to redirect_to('/redirect')
31+
end
1732
end
1833
end
1934

2035
context "when user is not logged in" do
21-
it "redirects to login path" do
22-
get :index
23-
expect(response).to redirect_to(spree.admin_login_path)
36+
context "when http_referrer is not present" do
37+
it "redirects to login path" do
38+
get :index
39+
expect(response).to redirect_to(spree.admin_login_path)
40+
end
41+
end
42+
43+
context "when http_referrer is present" do
44+
before { request.env['HTTP_REFERER'] = '/redirect' }
45+
46+
it "redirects back" do
47+
get :index
48+
expect(response).to redirect_to('/redirect')
49+
end
2450
end
2551
end
2652
end

spec/controllers/spree/base_controller_spec.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,45 @@
88
def index; authorize!(:read, :something); end
99
end
1010

11+
before do
12+
stub_spree_preferences(Spree::Config, redirect_back_on_unauthorized: true)
13+
end
14+
1115
context "when user is logged in" do
1216
before { sign_in(create(:user)) }
1317

14-
it "redirects to unauthorized path" do
15-
get :index
16-
expect(response).to redirect_to(spree.unauthorized_path)
18+
context "when http_referrer is not present" do
19+
it "redirects to unauthorized path" do
20+
get :index
21+
expect(response).to redirect_to(spree.unauthorized_path)
22+
end
23+
end
24+
25+
context "when http_referrer is present" do
26+
before { request.env['HTTP_REFERER'] = '/redirect' }
27+
28+
it "redirects back" do
29+
get :index
30+
expect(response).to redirect_to('/redirect')
31+
end
1732
end
1833
end
1934

2035
context "when user is not logged in" do
21-
it "redirects to login path" do
22-
get :index
23-
expect(response).to redirect_to(spree.login_path)
36+
context "when http_referrer is not present" do
37+
it "redirects to login path" do
38+
get :index
39+
expect(response).to redirect_to(spree.login_path)
40+
end
41+
end
42+
43+
context "when http_referrer is present" do
44+
before { request.env['HTTP_REFERER'] = '/redirect' }
45+
46+
it "redirects back" do
47+
get :index
48+
expect(response).to redirect_to('/redirect')
49+
end
2450
end
2551
end
2652
end

0 commit comments

Comments
 (0)