-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5214 from solidusio/elia/admin/auth-config
[Admin] Add an auth configuration that can be attached to the existing one
- Loading branch information
Showing
8 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin::Auth | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
if ::SolidusAdmin::Config.authentication_adapter | ||
include ::SolidusAdmin::Config.authentication_adapter.constantize | ||
end | ||
|
||
before_action :authenticate_solidus_admin_user! | ||
|
||
helper_method :current_solidus_admin_user | ||
helper_method :solidus_admin_logout_path | ||
helper_method :solidus_admin_logout_method | ||
end | ||
|
||
private | ||
|
||
def authenticate_solidus_admin_user! | ||
send SolidusAdmin::Config.authentication_method if SolidusAdmin::Config.authentication_method | ||
end | ||
|
||
def current_solidus_admin_user | ||
send SolidusAdmin::Config.current_user_method if SolidusAdmin::Config.current_user_method | ||
end | ||
|
||
def solidus_admin_logout_path | ||
send SolidusAdmin::Config.logout_link_path | ||
end | ||
|
||
def solidus_admin_logout_method | ||
SolidusAdmin::Config.logout_link_method | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
admin/app/controllers/solidus_admin/auth_adapters/backend.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin::AuthAdapters::Backend | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
delegate :admin_logout_path, to: :spree | ||
helper_method :admin_logout_path | ||
end | ||
|
||
private | ||
|
||
def authenticate_solidus_backend_user! | ||
if respond_to?(:model_class, true) && model_class | ||
record = model_class | ||
else | ||
record = controller_name.to_sym | ||
end | ||
authorize! :admin, record | ||
authorize! action_name.to_sym, record | ||
rescue CanCan::AccessDenied | ||
instance_exec(&Spree::Admin::BaseController.unauthorized_redirect) | ||
end | ||
|
||
# Needs to be overriden so that we use Spree's Ability rather than anyone else's. | ||
def current_ability | ||
@current_ability ||= Spree::Ability.new(spree_current_user) | ||
end | ||
|
||
def store_location | ||
Spree::UserLastUrlStorer.new(self).store_location | ||
end | ||
|
||
def spree_current_user | ||
defined?(super) ? super : nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
admin/spec/controllers/solidus_admin/base_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe SolidusAdmin::BaseController, type: :controller do | ||
controller(SolidusAdmin::BaseController) do | ||
def index | ||
authorize! :update, Spree::Order | ||
render plain: 'test' | ||
end | ||
end | ||
|
||
context "unauthorized request" do | ||
before do | ||
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(nil) | ||
end | ||
|
||
it "redirects to unauthorized" do | ||
get :index | ||
expect(response).to redirect_to '/unauthorized' | ||
end | ||
end | ||
|
||
context "successful request" do | ||
before do | ||
user = create(:admin_user, email: '[email protected]') | ||
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(user) | ||
end | ||
|
||
it "returns a 200 response" do | ||
get :index | ||
expect(response.code).to eq "200" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe "Accounts", type: :feature do | ||
let(:user) { create(:admin_user, email: '[email protected]') } | ||
|
||
before do | ||
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(user) | ||
end | ||
|
||
it "shows account info" do | ||
without_partial_double_verification do | ||
allow(Spree::Core::Engine.routes.url_helpers).to receive(:admin_logout_path).and_return('/admin/logout') | ||
end | ||
|
||
visit "/admin/account" | ||
|
||
expect(page).to have_content("Logged in as #{user.email}") | ||
expect(page).to have_content("Log out") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,11 @@ | |
require 'spec_helper' | ||
|
||
describe "Products", type: :feature do | ||
before do | ||
user = create(:admin_user, email: '[email protected]') | ||
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(user) | ||
end | ||
|
||
it "lists products", :js do | ||
create(:product, name: "Just a product", price: 19.99) | ||
|
||
|