Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Keep drafts out of every seller endpoint, not just the list

Damian Legawiecae56a052017b
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/seller/seller_context.rb b/spree/api/app/controllers/concerns/spree/api/v3/seller/seller_context.rb
index a4e249efde..1cd5a12256 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/seller/seller_context.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/seller/seller_context.rb
@@ -58,6 +58,18 @@ module Spree
current_seller&.store || Spree::Store.default
end
+ # The orders this seller has actually sold.
+ #
+ # A draft is either a checkout still in flight or the operator's own
+ # working document. Neither is this seller's, so neither is reachable
+ # from any endpoint on this branch — including the nested ones, which
+ # is why this lives here rather than in one controller's `scope`.
+ #
+ # @return [ActiveRecord::Relation]
+ def current_seller_orders
+ current_seller.orders.not_drafts
+ end
+
private
def set_current_seller_context
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
index 3cd350d6b1..1345f8128c 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders/fulfillments_controller.rb
@@ -5,7 +5,7 @@ module Spree
module Orders
# Dispatching what a seller owes on one of their orders.
#
- # Rooted at the order fetched through `current_seller.orders`, so a
+ # Rooted at the order fetched through `current_seller_orders`, so a
# fulfillment on somebody else's order reads as missing rather than
# denied — and a seller can never ship against an order that is not
# theirs, whatever id they send.
@@ -62,7 +62,7 @@ module Spree
private
def set_order
- @order = current_seller.orders.find_by_prefix_id!(params[:order_id])
+ @order = current_seller_orders.find_by_prefix_id!(params[:order_id])
authorize! :show, @order
end
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
index f50e478084..7ceca48f71 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
@@ -8,7 +8,9 @@ module Spree
# order each, so a seller's orders are their own rows — not a filtered
# view of somebody else's. The anchor roots `scope` in
# `current_seller.orders`, so an id belonging to another seller reads
- # as missing rather than denied.
+ # as missing rather than denied. Drafts are not this seller's either —
+ # neither an in-flight checkout nor the operator's working document —
+ # and are unreachable from every endpoint on this branch.
#
# Read and cancel only. Fulfilling is the fulfillments endpoint under
# the order, and everything that settles money with the customer —
@@ -56,13 +58,11 @@ module Spree
%w[index show]
end
- # Only orders that were actually placed. A draft is either a
- # checkout still in flight or the operator's own working document —
- # neither is something this seller has sold, and the operator's is
- # not theirs to read or cancel. The admin branch admits the
- # cart-less ones deliberately; this branch must not.
+ # The same exclusion the nested endpoints get from
+ # `current_seller_orders`, applied over the inherited scope so its
+ # includes and preloading survive.
def scope
- super.where.not(status: 'draft')
+ super.not_drafts
end
def collection_includes
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
index 141e96ef81..26d9596a38 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
@@ -37,6 +37,17 @@ RSpec.describe Spree::Api::V3::Seller::Orders::FulfillmentsController, type: :co
expect(response).to have_http_status(:not_found)
end
+
+ # A draft is not an order this seller has sold, so it must be as
+ # unreachable here as it is on the orders endpoint itself — otherwise
+ # the nested route becomes a way around that filter.
+ it '404s on a draft' do
+ draft = create(:order, store: store, seller: seller, status: 'draft', cart: nil)
+
+ get :index, params: { order_id: draft.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
end
describe 'PATCH #fulfill' do
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
index 979f7f9720..6bca076cb5 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
@@ -72,6 +72,17 @@ RSpec.describe Spree::Api::V3::Seller::OrdersController, type: :controller do
end
end
+ # An order is created by a shopper checking out, never by the seller. The
+ # branch routes no write verb at all, so widening `resources :orders` would
+ # have to trip this rather than silently hand sellers a draft to author.
+ describe 'writing an order' do
+ it 'is not routable' do
+ expect(post: '/api/v3/seller/orders').not_to be_routable
+ expect(patch: '/api/v3/seller/orders/ord_x').not_to be_routable
+ expect(delete: '/api/v3/seller/orders/ord_x').not_to be_routable
+ end
+ end
+
describe 'GET #show' do
it 'renders what the seller needs to pack the parcel' do
get :show, params: { id: mine.prefixed_id }, as: :json
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 2ce404ebfe..59d9ef4524 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -125,6 +125,7 @@ module Spree
validates :status, inclusion: { in: STATUSES }
scope :drafts, -> { where(status: 'draft') }
+ scope :not_drafts, -> { where.not(status: 'draft') }
scope :placed_orders, -> { where(status: 'placed') }
scope :canceled_orders, -> { where(status: 'canceled') }