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.

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.rb
index 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 Spree
def show
@resource ||= current_store.wishlists.find_by!(token: params[:id])
- raise ActiveRecord::RecordNotFound unless @resource.viewable?(spree_current_user)
+ spree_authorize! :read, @resource
render_serialized_payload { serialize_resource(@resource) }
end
diff --git a/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb
index 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 Spree
end
belongs_to :variant
- belongs_to :wishlist, serializer: ::Spree::V2::Storefront::WishlistSerializer
end
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
index 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 d
context 'when a request is sent by random user with no auth' do
it 'returns 404 when wishlist is set to is_private: true' do
get "/api/v2/storefront/wishlists/#{wishlist_private.token}"
- expect(response).to have_http_status(:not_found)
+ expect(response).to have_http_status(:forbidden)
end
it 'returns 200 when wishlist is set to is_private: false' do
@@ -182,8 +182,6 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(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')
end
context 'when a variant is already in the wishlist' do
diff --git a/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb
index 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 do
id: 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.rb
index 6d415ef1e2..5d018b3b87 100644
--- a/core/app/models/spree/ability.rb
+++ b/core/app/models/spree/ability.rb
@@ -87,7 +87,8 @@ module Spree
can :read, ::Spree::Variant
can :read, ::Spree::Zone
can :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 == user
end
end
diff --git a/core/app/models/spree/wishlist.rb b/core/app/models/spree/wishlist.rb
index 9b1524b10a..ed7b410348 100644
--- a/core/app/models/spree/wishlist.rb
+++ b/core/app/models/spree/wishlist.rb
@@ -24,14 +24,6 @@ module Spree
find_by(token: param)
end
- def viewable?(user)
- public? || user == self.user
- end
-
- def public?
- !is_private?
- end
-
private
def ensure_default_exists_and_is_unique
diff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rb
index 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 do
end
end
- 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' do
let!(:wished_item) { create(:wished_item) }