diff --git a/app/controllers/api/checkout_controller.rb b/app/controllers/api/checkout_controller.rb index 45da6a63e..05e5faa84 100644 --- a/app/controllers/api/checkout_controller.rb +++ b/app/controllers/api/checkout_controller.rb @@ -8,7 +8,8 @@ class API::CheckoutController < API::ApiController before_action :ensure_order def payment - if order.statistic_profile_id.nil? && current_user.privileged? + authorize @current_order, policy_class: CheckoutPolicy + if @current_order.statistic_profile_id.nil? && current_user.privileged? user = User.find(params[:customer_id]) @current_order.statistic_profile = user.statistic_profile end @@ -20,6 +21,7 @@ class API::CheckoutController < API::ApiController end def confirm_payment + authorize @current_order, policy_class: CheckoutPolicy res = Checkout::PaymentService.new.confirm_payment(@current_order, current_user, params[:coupon_code], params[:payment_id]) render json: res rescue StandardError => e diff --git a/app/policies/checkout_policy.rb b/app/policies/checkout_policy.rb new file mode 100644 index 000000000..045361caf --- /dev/null +++ b/app/policies/checkout_policy.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# Check the access policies for API::CheckoutController +class CheckoutPolicy < ApplicationPolicy + %w[payment confirm_payment].each do |action| + define_method "#{action}?" do + return user.privileged? || (record.statistic_profile_id == user.statistic_profile.id) + end + end +end