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
Simplify permission checks for Platform API (#11394)
Damian Legawiec5bb91451d947
diff --git a/api/app/controllers/spree/api/v2/platform/classifications_controller.rb b/api/app/controllers/spree/api/v2/platform/classifications_controller.rbindex 2f71298918..61bf775b0d 100644--- a/api/app/controllers/spree/api/v2/platform/classifications_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/classifications_controller.rb@@ -3,8 +3,6 @@ module Spreemodule V2module Platformclass ClassificationsController < ResourceController- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition-def repositionspree_authorize! :update, resource if spree_current_user.present?diff --git a/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb b/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rbindex 5549637fdc..40ed0ad161 100644--- a/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb@@ -3,8 +3,6 @@ module Spreemodule V2module Platformclass CmsSectionsController < ResourceController- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition-def repositionspree_authorize! :update, @moved_section if spree_current_user.present?diff --git a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rbindex 86344ab0f8..be69200fc0 100644--- a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb@@ -3,8 +3,6 @@ module Spreemodule V2module Platformclass MenuItemsController < ResourceController- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition-def repositionspree_authorize! :update, @moved_item if spree_current_user.present?diff --git a/api/app/controllers/spree/api/v2/platform/orders_controller.rb b/api/app/controllers/spree/api/v2/platform/orders_controller.rbindex 1d814540a3..1bf216e3c3 100644--- a/api/app/controllers/spree/api/v2/platform/orders_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/orders_controller.rb@@ -3,9 +3,6 @@ module Spreemodule V2module Platformclass OrdersController < ResourceController- WRITE_ACTIONS.push(:use_store_credit, :remove_store_credit, :apply_coupon_code, :remove_coupon_code, :next, :advance,- :complete, :empty, :approve, :cancel)-include CouponCodesHelperdef create@@ -116,6 +113,8 @@ module Spreeif action_name == 'create'spree_authorize! :create, model_class+ elsif %w[show index].include?(action_name)+ spree_authorize! :read, model_classelsif %w[next cancel complete empty advance add_store_credit remove_store_credit].include?(action_name)spree_authorize! :update, resourceelsediff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rbindex 20d7ffc09a..e4a3c071be 100644--- a/api/app/controllers/spree/api/v2/platform/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/resource_controller.rb@@ -3,15 +3,12 @@ module Spreemodule V2module Platformclass ResourceController < ::Spree::Api::V2::ResourceController- READ_ACTIONS = %i[show index]- WRITE_ACTIONS = %i[create update destroy]-# doorkeeper scopes usage: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes- before_action -> { doorkeeper_authorize! :read, :admin }, if: -> { READ_ACTIONS.include?(action_name.to_sym) }- before_action -> { doorkeeper_authorize! :write, :admin }, if: -> { WRITE_ACTIONS.include?(action_name.to_sym) }+ before_action -> { doorkeeper_authorize! :read, :admin }+ before_action -> { doorkeeper_authorize! :write, :admin }, if: :write_request?# optional authorization if using a user token instead of app token- before_action :authorize_spree_user, if: -> { WRITE_ACTIONS.include?(action_name.to_sym) }+ before_action :authorize_spree_user# index and show acrtions are defined in Spree::Api::V2::ResourceController@@ -88,6 +85,8 @@ module Spreeif action_name == 'create'spree_authorize! :create, model_class+ elsif %w[show index].include?(action_name)+ spree_authorize! :read, model_classelsespree_authorize! action_name, resourceend@@ -108,6 +107,10 @@ module Spreedef allowed_sort_attributes(super << spree_permitted_attributes).uniq.compactend++ def write_request?+ %w[put patch post delete].include?(request.request_method.downcase)+ endendendend