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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Ensure that we only fetch current Product resources
Damian Legawiec430f7f32d613
diff --git a/backend/app/controllers/concerns/spree/admin/product_concern.rb b/backend/app/controllers/concerns/spree/admin/product_concern.rbnew file mode 100644index 0000000000..b47938dba2--- /dev/null+++ b/backend/app/controllers/concerns/spree/admin/product_concern.rb@@ -0,0 +1,11 @@+module Spree+ module Admin+ module ProductConcern+ extend ActiveSupport::Concern++ def product_scope+ current_store.products.accessible_by(current_ability, :index)+ end+ end+ end+enddiff --git a/backend/app/controllers/spree/admin/base_controller.rb b/backend/app/controllers/spree/admin/base_controller.rbindex 05346d506c..af693f692b 100644--- a/backend/app/controllers/spree/admin/base_controller.rb+++ b/backend/app/controllers/spree/admin/base_controller.rb@@ -38,7 +38,7 @@ module Spreedef flash_message_for(object, event_sym)resource_desc = object.class.model_name.human- resource_desc += " \"#{object.name}\"" if object.respond_to?(:name) && object.name.present? && !object.is_a?(Spree::Order)+ resource_desc += " \"#{object.name}\"" if object.persisted? && object.respond_to?(:name) && object.name.present? && !object.is_a?(Spree::Order)Spree.t(event_sym, resource: resource_desc)enddiff --git a/backend/app/controllers/spree/admin/images_controller.rb b/backend/app/controllers/spree/admin/images_controller.rbindex 7d8ffb2c6b..cc43b7d382 100644--- a/backend/app/controllers/spree/admin/images_controller.rb+++ b/backend/app/controllers/spree/admin/images_controller.rb@@ -1,8 +1,11 @@module Spreemodule Adminclass ImagesController < ResourceController+ include Spree::Admin::ProductConcern++ belongs_to 'spree/product', find_by: :slug+before_action :load_edit_data, except: :index- before_action :load_index_data, only: :indexcreate.before :set_viewableupdate.before :set_viewable@@ -10,41 +13,24 @@ module Spreeprivatedef location_after_destroy- admin_product_images_url(@product)+ spree.admin_product_images_url(@arent)enddef location_after_save- admin_product_images_url(@product)- end-- def load_index_data- @product = Product.friendly.preload(*variant_index_includes).find(params[:product_id])+ spree.admin_product_images_url(parent)enddef load_edit_data- @product = Product.friendly.preload(*variant_edit_includes).find(params[:product_id])- @variants = @product.variants.map do |variant|+ @variants = parent.variants.map do |variant|[variant.sku_and_options_text, variant.id]end- @variants.insert(0, [Spree.t(:all), @product.master.id])+ @variants.insert(0, [Spree.t(:all), parent.master.id])enddef set_viewable@image.viewable_type = 'Spree::Variant'@image.viewable_id = params[:image][:viewable_id]end-- def variant_index_includes- [- variant_images: [viewable: { option_values: :option_type }]- ]- end-- def variant_edit_includes- [- variants_including_master: { option_values: :option_type, images: :viewable }- ]- endendendenddiff --git a/backend/app/controllers/spree/admin/prices_controller.rb b/backend/app/controllers/spree/admin/prices_controller.rbindex 3b9584e2e1..944df5e4d8 100644--- a/backend/app/controllers/spree/admin/prices_controller.rb+++ b/backend/app/controllers/spree/admin/prices_controller.rb@@ -8,7 +8,7 @@ module Spreeparams[:vp].each do |variant_id, prices|next unless variant_id- variant = Spree::Variant.find(variant_id)+ variant = parent.variants_including_master.find(variant_id)next unless variantsupported_currencies_for_all_stores.each do |currency|diff --git a/backend/app/controllers/spree/admin/products_controller.rb b/backend/app/controllers/spree/admin/products_controller.rbindex 1e92f5bd1d..3449493229 100644--- a/backend/app/controllers/spree/admin/products_controller.rb+++ b/backend/app/controllers/spree/admin/products_controller.rb@@ -1,6 +1,8 @@module Spreemodule Adminclass ProductsController < ResourceController+ include Spree::Admin::ProductConcern+helper 'spree/products'before_action :load_data, except: :index@@ -45,7 +47,7 @@ module Spreeenddef destroy- @product = Product.friendly.find(params[:id])+ @product = product_scope.friendly.find(params[:id])begin# TODO: why is @product.destroy raising ActiveRecord::RecordNotDestroyed instead of failing with false result@@ -92,12 +94,8 @@ module Spreeprotected- def scope- current_store.products.accessible_by(current_ability, :index)- end-def find_resource- scope.with_deleted.friendly.find(params[:id])+ product_scope.with_deleted.friendly.find(params[:id])enddef location_after_save@@ -120,7 +118,7 @@ module Spreeparams[:q][:s] ||= 'name asc'- @collection = scope+ @collection = product_scope# Don't delete params[:q][:deleted_at_null] here because it is used in view to check the# checkbox for 'q[deleted_at_null]'. This also messed with pagination when deleted_at_null is checked.diff --git a/backend/app/controllers/spree/admin/resource_controller.rb b/backend/app/controllers/spree/admin/resource_controller.rbindex fed8a8d942..8c7d5b1c5d 100644--- a/backend/app/controllers/spree/admin/resource_controller.rb+++ b/backend/app/controllers/spree/admin/resource_controller.rb@@ -162,6 +162,10 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController# Don't use `find_by_attribute_name` to workaround globalize/globalize#423 bugsend(:find_by, parent_data[:find_by].to_s => params["#{resource.model_name}_id"])instance_variable_set("@#{resource.model_name}", @parent)++ raise ActiveRecord::RecordNotFound if @parent.nil?++ @parentendenddiff --git a/backend/app/controllers/spree/admin/variants_controller.rb b/backend/app/controllers/spree/admin/variants_controller.rbindex cb843fd144..aa76413a3f 100644--- a/backend/app/controllers/spree/admin/variants_controller.rb+++ b/backend/app/controllers/spree/admin/variants_controller.rb@@ -1,6 +1,8 @@module Spreemodule Adminclass VariantsController < ResourceController+ include Spree::Admin::ProductConcern+belongs_to 'spree/product', find_by: :slugnew_action.before :new_beforebefore_action :redirect_on_empty_option_values, only: [:new]@@ -9,7 +11,7 @@ module Spree# override the destroy method to set deleted_at value# instead of actually deleting the product.def destroy- @variant = Variant.find(params[:id])+ @variant = parent.variants.find(params[:id])if @variant.destroyflash[:success] = Spree.t('notice_messages.variant_deleted')else@@ -34,10 +36,6 @@ module Spree@object.default_price = master.default_price.clone if master.default_price.present?end- def parent- @product = Product.with_deleted.friendly.find(params[:product_id])- end-def collectionreturn @collection if @collection.present?diff --git a/backend/spec/controllers/spree/admin/images_controller_spec.rb b/backend/spec/controllers/spree/admin/images_controller_spec.rbnew file mode 100644index 0000000000..9ac760f3ae--- /dev/null+++ b/backend/spec/controllers/spree/admin/images_controller_spec.rb@@ -0,0 +1,65 @@+require 'spec_helper'++module Spree+ module Admin+ describe ImagesController, type: :controller do+ stub_authorization!++ let(:store) { Spree::Store.default }+ let(:product) { create(:product, stores: [store]) }++ describe '#index' do+ let!(:image_1) { create(:image, viewable: product.master) }+ let!(:image_2) { create(:image, viewable: product.master) }+ let!(:image_3) { create(:image, viewable: create(:variant)) }++ it 'assigns the images for a requested product' do+ get :index, params: { product_id: product.slug }+ expect(assigns(:collection)).to include image_1+ expect(assigns(:collection)).to include image_2+ expect(assigns(:collection)).not_to include image_3+ end+ end++ describe '#destroy' do+ subject(:send_request) do+ delete :destroy, params: { product_id: product, id: image, format: :js }+ end++ let(:image) { create(:image, viewable: product.master) }++ shared_examples 'correct response' do+ it { expect(assigns(:image)).to eq(image) }+ it { expect(response).to have_http_status(:ok) }+ end++ context 'will successfully destroy image' do+ describe 'returns response' do+ before { send_request }++ it_behaves_like 'correct response'+ it { expect(flash[:success]).to eq('Image has been successfully removed!') }+ end+ end++ context 'cannot destroy image of other product' do+ let(:other_product) { create(:product, stores: [store]) }+ let(:image) { create(:image, viewable: other_product) }++ it { expect(send_request).to redirect_to(spree.admin_product_images_path(product)) }++ it do+ send_request+ expect(flash[:error]).to eq('Image is not found')+ end+ end++ context 'cannot destroy image of product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }+ end+ end+ end+ end+enddiff --git a/backend/spec/controllers/spree/admin/products_controller_spec.rb b/backend/spec/controllers/spree/admin/products_controller_spec.rbindex 38833d2b57..e4cfb08c36 100644--- a/backend/spec/controllers/spree/admin/products_controller_spec.rb+++ b/backend/spec/controllers/spree/admin/products_controller_spec.rb@@ -3,21 +3,32 @@ require 'spec_helper'describe Spree::Admin::ProductsController, type: :controller dostub_authorization!+ let(:store) { Spree::Store.default }+context '#index' dolet(:ability_user) { stub_model(Spree::LegacyUser, has_spree_role?: true) }# Regression test for #1259it 'can find a product by SKU' do- product = create(:product, sku: 'ABC123', stores: Spree::Store.all)+ product = create(:product, sku: 'ABC123', stores: [store])get :index, params: { q: { sku_start: 'ABC123' } }expect(assigns[:collection]).not_to be_emptyexpect(assigns[:collection]).to include(product)end++ it 'cannot find products from different stores' do+ product = create(:product, sku: 'ABC123', stores: [create(:store)])+ get :index, params: { q: { sku_start: 'ABC123' } }+ expect(assigns[:collection]).to be_empty+ expect(assigns[:collection]).not_to include(product)+ endend++# regression test for #1370context 'adding properties to a product' do- let!(:product) { create(:product, stores: Spree::Store.all) }+ let!(:product) { create(:product, stores: [store]) }specify doput :update, params: {@@ -101,6 +112,17 @@ describe Spree::Admin::ProductsController, type: :controller doendendend++ context 'cannot destroy product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect(send_request).to redirect_to(spree.admin_products_path) }++ it do+ send_request+ expect(flash[:error]).to eq('Product is not found')+ end+ endenddescribe '#clone' do@@ -147,14 +169,57 @@ describe Spree::Admin::ProductsController, type: :controller doendendend++ context 'cannot clone product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect(send_request).to redirect_to(spree.admin_products_path) }++ it do+ send_request+ expect(flash[:error]).to eq('Product is not found')+ end+ endend- context 'stock' do- let(:product) { create(:product, stores: Spree::Store.all) }+ describe '#show' do+ let(:product) { create(:product, stores: [store]) }+ let(:send_request) { get :stock, params: { id: product } }++ it { expect(send_request).to have_http_status(:ok) }++ context 'cannot see product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect(send_request).to redirect_to(spree.admin_products_path) }++ it do+ send_request+ expect(flash[:error]).to eq('Product is not found')+ end+ end+ end++ describe '#stock' do+ let(:product) { create(:product, stores: [store]) }+ let(:send_request) { get :stock, params: { id: product } }it 'restricts stock location based on accessible attributes' doexpect(Spree::StockLocation).to receive(:accessible_by).and_return([])- get :stock, params: { id: product }+ send_request+ end++ it { expect(send_request).to have_http_status(:ok) }++ context 'cannot see stock of product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect(send_request).to redirect_to(spree.admin_products_path) }++ it do+ send_request+ expect(flash[:error]).to eq('Product is not found')+ endendendenddiff --git a/backend/spec/controllers/spree/admin/variants_controller_spec.rb b/backend/spec/controllers/spree/admin/variants_controller_spec.rbindex a76cd98b6e..b356c047d4 100644--- a/backend/spec/controllers/spree/admin/variants_controller_spec.rb+++ b/backend/spec/controllers/spree/admin/variants_controller_spec.rb@@ -11,12 +11,14 @@ module Spreedescribe '#index' dolet!(:variant_1) { create(:variant, product: product) }let!(:variant_2) { create(:variant, product: product) }+ let!(:variant_3) { create(:variant, product: create(:product)) }context 'deleted is not requested' doit 'assigns the variants for a requested product' doget :index, params: { product_id: product.slug }expect(assigns(:collection)).to include variant_1expect(assigns(:collection)).to include variant_2+ expect(assigns(:collection)).not_to include variant_3endend@@ -37,8 +39,6 @@ module Spreeendlet(:variant) { create(:variant, product: product) }- let(:variants) { double(ActiveRecord::Relation) }- let(:products) { double(ActiveRecord::Relation) }shared_examples 'correct response' doit { expect(assigns(:variant)).to eq(variant) }@@ -53,6 +53,24 @@ module Spreeit { expect(flash[:success]).to eq(Spree.t('notice_messages.variant_deleted')) }endend++ context 'cannot destroy variant of other product' do+ let(:other_product) { create(:product, stores: [store]) }+ let(:variant) { create(:variant, product: other_product) }++ it { expect(send_request).to redirect_to(spree.admin_product_variants_path(product)) }++ it do+ send_request+ expect(flash[:error]).to eq('Variant is not found')+ end+ end++ context 'cannot destroy variant of product from different store' do+ let(:product) { create(:product, stores: [create(:store)]) }++ it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }+ endendendenddiff --git a/backend/spec/features/admin/products/edit/images_spec.rb b/backend/spec/features/admin/products/edit/images_spec.rbindex 0f6c3078e4..5fe8f52a0b 100644--- a/backend/spec/features/admin/products/edit/images_spec.rb+++ b/backend/spec/features/admin/products/edit/images_spec.rb@@ -5,6 +5,9 @@ describe 'Product Images', type: :feature, js: true dolet(:file_path) { Rails.root + '../../spec/support/ror_ringer.jpeg' }+ let(:store) { Spree::Store.default }+ let(:product) { create(:product, stores: [store]) }+before do# Ensure attachment style keys are symbolized before running all tests# Otherwise this would result in this error:@@ -15,8 +18,7 @@ describe 'Product Images', type: :feature, js: true docontext 'uploading, editing, and deleting an image' doit 'allows an admin to upload and edit an image for a product' do- create(:product, stores: Spree::Store.all)-+ productvisit spree.admin_products_pathclick_icon(:edit)click_link 'Images'@@ -42,9 +44,9 @@ describe 'Product Images', type: :feature, js: true do# Regression test for #2228it 'sees variant images', js: false do- variant = create(:variant)+ variant = create(:variant, product: product)create_image(variant, File.open(file_path))- visit spree.admin_product_images_path(variant.product)+ visit spree.admin_product_images_path(product)expect(page).not_to have_content('No Images Found.')within('table.table') do@@ -66,7 +68,6 @@ describe 'Product Images', type: :feature, js: true doendit 'does not see variant column when product has no variants', js: false do- product = create(:product, stores: Spree::Store.all)create_image(product, File.open(file_path))visit spree.admin_product_images_path(product)diff --git a/backend/spec/features/admin/products/variant_prices_spec.rb b/backend/spec/features/admin/products/variant_prices_spec.rbindex 33a104c304..446d2ac53a 100644--- a/backend/spec/features/admin/products/variant_prices_spec.rb+++ b/backend/spec/features/admin/products/variant_prices_spec.rb@@ -2,8 +2,13 @@ require 'spec_helper'describe 'Variant Prices', type: :feature, js: true dostub_authorization!- let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR') }+ let(:store) { Spree::Store.default }let!(:store2) { create(:store, default: false, default_currency: 'GBP') }++ before do+ store.update(supported_currencies: 'USD,EUR')+ end+let!(:product) { create(:product, stores: Spree::Store.all) }context 'with USD and EUR as currencies' do