Skip to content
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

Add configurable unauthorized path #2522

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Alchemy.signup_path = '/your/signup/path' # Defaults to '/signup'
Alchemy.login_path = '/your/login/path' # Defaults to '/login'
Alchemy.logout_path = '/your/logout/path' # Defaults to '/logout'
Alchemy.logout_method = 'http_verb_for_logout' # Defaults to 'delete'
Alchemy.unauthorized_path = '/some/public/page' # Defaults to '/'
```

The only thing Alchemy needs to know from your user class is the `alchemy_roles` method.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def handle_redirect_for_user
if can?(:index, :alchemy_admin_dashboard)
redirect_or_render_notice
else
redirect_to("/")
redirect_to Alchemy.unauthorized_path
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/alchemy/auth_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# +Alchemy.login_path defaults to +'/login'+
# +Alchemy.logout_path defaults to +'/logout'+
# +Alchemy.logout_method defaults to +'delete'+
# +Alchemy.unauthorized_path defaults to +'/'+
#
# Anyway, you can tell Alchemy about your authentication model configuration:
#
Expand All @@ -22,6 +23,7 @@
# 5. The path to the login form - @see: Alchemy.login_path
# 6. The path to the logout method - @see: Alchemy.logout_path
# 7. The http verb for the logout method - @see: Alchemy.logout_method
# 8. The path to the page showing the user she's unauthorized - @see: Alchemy.unauthorized_path
#
# == Example
#
Expand All @@ -33,6 +35,7 @@
# Alchemy.login_path = '/auth/login'
# Alchemy.logout_path = '/auth/logout'
# Alchemy.logout_method = 'get'
# Alchemy.unauthorized_path = '/home'
#
# If you don't have your own user model or don't want to provide one,
# add the `alchemy-devise` gem into your App's Gemfile.
Expand All @@ -49,7 +52,8 @@ module Alchemy
:signup_path,
:login_path,
:logout_path,
:logout_method
:logout_method,
:unauthorized_path

# Defaults
#
Expand All @@ -60,6 +64,7 @@ module Alchemy
@@login_path = "/login"
@@logout_path = "/logout"
@@logout_method = "delete"
@@unauthorized_path = "/"

# Returns the user class
#
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/alchemy/admin/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@
end
end

describe "#permission_denied" do
context "when called with an AccessDenied exception" do
before do
allow(controller).to receive(:redirect_to)
end

it "redirects to login_path if no user" do
controller.send(:permission_denied, CanCan::AccessDenied.new)
expect(controller).to have_received(:redirect_to).with(Alchemy.login_path)
end

it "redirects to unauthorized_path for a logged in user" do
authorize_user(build(:alchemy_dummy_user))
controller.send(:permission_denied, CanCan::AccessDenied.new)
expect(controller).to have_received(:redirect_to).with(Alchemy.unauthorized_path)
end
end
end

context "when current_alchemy_user is present" do
let!(:page_1) { create(:alchemy_page, name: "Page 1") }
let!(:page_2) { create(:alchemy_page, name: "Page 2") }
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/alchemy/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ module Alchemy
end
end

describe "#permission_denied" do
context "when called with an AccessDenied exception" do
before do
allow(controller).to receive(:redirect_to)
end

it "redirects to login_path if no user" do
controller.send(:permission_denied, CanCan::AccessDenied.new)
expect(controller).to have_received(:redirect_to).with(Alchemy.login_path)
end

it "redirects to unauthorized_path for a logged in user" do
authorize_user(build(:alchemy_dummy_user))
controller.send(:permission_denied, CanCan::AccessDenied.new)
expect(controller).to have_received(:redirect_to).with(Alchemy.unauthorized_path)
end
end
end

describe "#multi_language?" do
subject { controller.multi_language? }

Expand Down