Skip to content

Commit a9a7a80

Browse files
authored
Merge pull request #192 from nebulab/kennyadsl/redirect-back-if-not-authorized
Redirect back if not authorized
2 parents b90a7f8 + ff57ff5 commit a9a7a80

3 files changed

Lines changed: 149 additions & 4 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
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Spree::Admin::BaseController, type: :controller do
6+
describe '#unauthorized_redirect' do
7+
controller(described_class) do
8+
def index; authorize!(:read, :something); end
9+
end
10+
11+
before do
12+
stub_spree_preferences(Spree::Config, redirect_back_on_unauthorized: true)
13+
end
14+
15+
context "when user is logged in" do
16+
before { sign_in(create(:user)) }
17+
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
32+
end
33+
end
34+
35+
context "when user is not logged in" do
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
50+
end
51+
end
52+
end
53+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Spree::BaseController, type: :controller do
6+
describe '#unauthorized_redirect' do
7+
controller(described_class) do
8+
def index; authorize!(:read, :something); end
9+
end
10+
11+
before do
12+
stub_spree_preferences(Spree::Config, redirect_back_on_unauthorized: true)
13+
end
14+
15+
context "when user is logged in" do
16+
before { sign_in(create(:user)) }
17+
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
32+
end
33+
end
34+
35+
context "when user is not logged in" do
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
50+
end
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)