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
Use cancan for authorization of is_private.
Matthew Kennedy0b025fc3df9a
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rbindex 58cd2b84d2..d017dace97 100644--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb@@ -9,7 +9,7 @@ module Spreedef show@resource ||= current_store.wishlists.find_by!(token: params[:id])- raise ActiveRecord::RecordNotFound unless @resource.viewable?(spree_current_user)+ spree_authorize! :read, @resourcerender_serialized_payload { serialize_resource(@resource) }enddiff --git a/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_item_serializer.rbindex bda6fbf2b4..52809dc6b9 100644--- a/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb@@ -23,7 +23,6 @@ module Spreeendbelongs_to :variant- belongs_to :wishlist, serializer: ::Spree::V2::Storefront::WishlistSerializerendendenddiff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rbindex 4055a23a0f..5b5386c7cd 100644--- a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb@@ -103,7 +103,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dcontext 'when a request is sent by random user with no auth' doit 'returns 404 when wishlist is set to is_private: true' doget "/api/v2/storefront/wishlists/#{wishlist_private.token}"- expect(response).to have_http_status(:not_found)+ expect(response).to have_http_status(:forbidden)endit 'returns 200 when wishlist is set to is_private: false' do@@ -182,8 +182,6 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dexpect(json_response['data']['attributes']['display_total']).to eql ('$59.97')expect(json_response['data']['relationships']['variant']['data']['id']).to eql (variant.id.to_s)expect(json_response['data']['relationships']['variant']['data']['type']).to eql ('variant')- expect(json_response['data']['relationships']['wishlist']['data']['id']).to eql (user.wishlists.first.id.to_s)- expect(json_response['data']['relationships']['wishlist']['data']['type']).to eql ('wishlist')endcontext 'when a variant is already in the wishlist' dodiff --git a/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rbindex 379517feff..02082511c6 100644--- a/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb+++ b/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb@@ -27,12 +27,6 @@ describe Spree::V2::Storefront::WishedItemSerializer doid: wished_item.variant.id.to_s,type: :variant}- },- wishlist: {- data: {- id: wishlist.id.to_s,- type: :wishlist- }}}}diff --git a/core/app/models/spree/ability.rb b/core/app/models/spree/ability.rbindex 6d415ef1e2..5d018b3b87 100644--- a/core/app/models/spree/ability.rb+++ b/core/app/models/spree/ability.rb@@ -87,7 +87,8 @@ module Spreecan :read, ::Spree::Variantcan :read, ::Spree::Zonecan :manage, ::Spree::Wishlist, user_id: user.id- can [:create, :update, :destroy], Spree::WishedItem do |wished_item|+ can :read, ::Spree::Wishlist, is_private: false+ can [:create, :update, :destroy], ::Spree::WishedItem do |wished_item|wished_item.wishlist.user == userendenddiff --git a/core/app/models/spree/wishlist.rb b/core/app/models/spree/wishlist.rbindex 9b1524b10a..ed7b410348 100644--- a/core/app/models/spree/wishlist.rb+++ b/core/app/models/spree/wishlist.rb@@ -24,14 +24,6 @@ module Spreefind_by(token: param)end- def viewable?(user)- public? || user == self.user- end-- def public?- !is_private?- end-privatedef ensure_default_exists_and_is_uniquediff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rbindex 529e3f1034..fbd12b72f6 100644--- a/core/spec/models/spree/wishlist_spec.rb+++ b/core/spec/models/spree/wishlist_spec.rb@@ -87,41 +87,6 @@ describe Spree::Wishlist, type: :model doendend- describe '.viewable?' do- context 'when the wishlist is private' do- it 'is true when the user owns the wishlist' do- wishlist.is_private = true- expect(wishlist.viewable?(user)).to be true- end-- it 'is false when the user does not own the wishlist' do- wishlist.is_private = true- other_user = create(:user)- expect(wishlist.viewable?(other_user)).to be false- end- end-- context 'when the wishlist is public' do- it 'is true for any user' do- wishlist.is_private = false- other_user = create(:user)- expect(wishlist.viewable?(other_user)).to be true- end- end- end-- describe '.public?' do- it 'is true when the wishlist is public' do- wishlist.is_private = false- expect(wishlist.public?).to be true- end-- it 'is false when the wishlist is private' do- wishlist.is_private = true- expect(wishlist.public?).not_to be true- end- end-describe '#destroy' dolet!(:wished_item) { create(:wished_item) }