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.

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.rb
index 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 Spree
module V2
module Platform
class ClassificationsController < ResourceController
- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition
-
def reposition
spree_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.rb
index 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 Spree
module V2
module Platform
class CmsSectionsController < ResourceController
- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition
-
def reposition
spree_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.rb
index 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 Spree
module V2
module Platform
class MenuItemsController < ResourceController
- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition
-
def reposition
spree_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.rb
index 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 Spree
module V2
module Platform
class OrdersController < ResourceController
- WRITE_ACTIONS.push(:use_store_credit, :remove_store_credit, :apply_coupon_code, :remove_coupon_code, :next, :advance,
- :complete, :empty, :approve, :cancel)
-
include CouponCodesHelper
def create
@@ -116,6 +113,8 @@ module Spree
if action_name == 'create'
spree_authorize! :create, model_class
+ elsif %w[show index].include?(action_name)
+ spree_authorize! :read, model_class
elsif %w[next cancel complete empty advance add_store_credit remove_store_credit].include?(action_name)
spree_authorize! :update, resource
else
diff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rb
index 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 Spree
module V2
module Platform
class 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 Spree
if action_name == 'create'
spree_authorize! :create, model_class
+ elsif %w[show index].include?(action_name)
+ spree_authorize! :read, model_class
else
spree_authorize! action_name, resource
end
@@ -108,6 +107,10 @@ module Spree
def allowed_sort_attributes
(super << spree_permitted_attributes).uniq.compact
end
+
+ def write_request?
+ %w[put patch post delete].include?(request.request_method.downcase)
+ end
end
end
end