spree_api 6.0.6 → 6.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/controllers/concerns/spree/api/v3/admin/product_membership.rb +2 -2
  4. data/app/controllers/concerns/spree/api/v3/admin/validation_details.rb +34 -13
  5. data/app/controllers/concerns/spree/api/v3/authentication_strategies.rb +14 -8
  6. data/app/controllers/concerns/spree/api/v3/error_handler.rb +31 -4
  7. data/app/controllers/concerns/spree/api/v3/invitation_resend.rb +28 -0
  8. data/app/controllers/concerns/spree/api/v3/locale_and_currency.rb +1 -1
  9. data/app/controllers/concerns/spree/api/v3/resource_serializer.rb +5 -0
  10. data/app/controllers/concerns/spree/api/v3/scoped_authorization.rb +20 -3
  11. data/app/controllers/spree/api/v3/admin/admin_users_controller.rb +8 -3
  12. data/app/controllers/spree/api/v3/admin/api_keys_controller.rb +6 -1
  13. data/app/controllers/spree/api/v3/admin/auth_controller.rb +1 -1
  14. data/app/controllers/spree/api/v3/admin/catalogs_controller.rb +2 -1
  15. data/app/controllers/spree/api/v3/admin/channels_controller.rb +2 -2
  16. data/app/controllers/spree/api/v3/admin/collection_rules_controller.rb +1 -1
  17. data/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb +1 -1
  18. data/app/controllers/spree/api/v3/admin/email_templates_controller.rb +2 -2
  19. data/app/controllers/spree/api/v3/admin/exports_controller.rb +49 -0
  20. data/app/controllers/spree/api/v3/admin/gift_cards_controller.rb +9 -0
  21. data/app/controllers/spree/api/v3/admin/integrations_controller.rb +4 -2
  22. data/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rb +10 -3
  23. data/app/controllers/spree/api/v3/admin/invitations_controller.rb +4 -5
  24. data/app/controllers/spree/api/v3/admin/order_routing_rules_controller.rb +1 -1
  25. data/app/controllers/spree/api/v3/admin/orders/payments_controller.rb +11 -3
  26. data/app/controllers/spree/api/v3/admin/password_resets_controller.rb +14 -2
  27. data/app/controllers/spree/api/v3/admin/payment_methods_controller.rb +23 -2
  28. data/app/controllers/spree/api/v3/admin/permissions_controller.rb +9 -3
  29. data/app/controllers/spree/api/v3/admin/products_controller.rb +18 -6
  30. data/app/controllers/spree/api/v3/admin/promotion_actions_controller.rb +1 -1
  31. data/app/controllers/spree/api/v3/admin/promotion_rules_controller.rb +1 -1
  32. data/app/controllers/spree/api/v3/admin/reporting_controller.rb +1 -5
  33. data/app/controllers/spree/api/v3/admin/sellers/invitations_controller.rb +12 -12
  34. data/app/controllers/spree/api/v3/admin/setup_controller.rb +2 -18
  35. data/app/controllers/spree/api/v3/admin/stock_levels_controller.rb +1 -1
  36. data/app/controllers/spree/api/v3/admin/tags_controller.rb +1 -1
  37. data/app/controllers/spree/api/v3/admin/tax_providers_controller.rb +20 -0
  38. data/app/controllers/spree/api/v3/admin/translatable_resources_controller.rb +3 -1
  39. data/app/controllers/spree/api/v3/admin/translations_coverage_controller.rb +11 -9
  40. data/app/controllers/spree/api/v3/admin/webhook_events_controller.rb +9 -3
  41. data/app/controllers/spree/api/v3/base_controller.rb +39 -0
  42. data/app/controllers/spree/api/v3/resource_controller.rb +0 -18
  43. data/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb +2 -2
  44. data/app/controllers/spree/api/v3/seller/invitations_controller.rb +11 -11
  45. data/app/controllers/spree/api/v3/seller/onboarding_controller.rb +1 -1
  46. data/app/controllers/spree/api/v3/seller/password_resets_controller.rb +12 -2
  47. data/app/controllers/spree/api/v3/seller/products_controller.rb +1 -1
  48. data/app/controllers/spree/api/v3/store/account/companies_controller.rb +7 -7
  49. data/app/controllers/spree/api/v3/store/auth_controller.rb +2 -2
  50. data/app/controllers/spree/api/v3/store/company_invitations_controller.rb +7 -4
  51. data/app/controllers/spree/api/v3/store/customer/password_resets_controller.rb +11 -1
  52. data/app/controllers/spree/api/v3/store/newsletter_subscribers_controller.rb +4 -3
  53. data/app/serializers/spree/api/v3/admin/api_key_serializer.rb +2 -1
  54. data/app/serializers/spree/api/v3/admin/bulk_product_result_serializer.rb +33 -0
  55. data/app/serializers/spree/api/v3/admin/bulk_stock_level_result_serializer.rb +17 -0
  56. data/app/serializers/spree/api/v3/admin/calculator_type_serializer.rb +19 -0
  57. data/app/serializers/spree/api/v3/admin/company_invitation_serializer.rb +5 -0
  58. data/app/serializers/spree/api/v3/admin/company_membership_serializer.rb +1 -0
  59. data/app/serializers/spree/api/v3/admin/country_serializer.rb +8 -0
  60. data/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb +2 -1
  61. data/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb +3 -0
  62. data/app/serializers/spree/api/v3/admin/integration_connection_result_serializer.rb +18 -0
  63. data/app/serializers/spree/api/v3/admin/integration_type_definition_serializer.rb +25 -0
  64. data/app/serializers/spree/api/v3/admin/password_reset_acknowledgement_serializer.rb +18 -0
  65. data/app/serializers/spree/api/v3/admin/payment_method_type_serializer.rb +17 -0
  66. data/app/serializers/spree/api/v3/admin/product_membership_result_serializer.rb +19 -0
  67. data/app/serializers/spree/api/v3/admin/reporting_dimension_value_serializer.rb +22 -0
  68. data/app/serializers/spree/api/v3/admin/reporting_metric_value_serializer.rb +49 -0
  69. data/app/serializers/spree/api/v3/admin/reporting_result_meta_serializer.rb +44 -0
  70. data/app/serializers/spree/api/v3/admin/reporting_result_serializer.rb +18 -55
  71. data/app/serializers/spree/api/v3/admin/reporting_row_serializer.rb +35 -0
  72. data/app/serializers/spree/api/v3/admin/resource_type_definition_serializer.rb +29 -0
  73. data/app/serializers/spree/api/v3/admin/setup_status_serializer.rb +17 -0
  74. data/app/serializers/spree/api/v3/admin/tag_serializer.rb +17 -0
  75. data/app/serializers/spree/api/v3/admin/translatable_resource_serializer.rb +28 -0
  76. data/app/serializers/spree/api/v3/admin/translation_coverage_serializer.rb +37 -0
  77. data/app/serializers/spree/api/v3/company_invitation_serializer.rb +10 -1
  78. data/app/serializers/spree/api/v3/company_membership_serializer.rb +2 -0
  79. data/app/serializers/spree/api/v3/fulfillment_serializer.rb +7 -6
  80. data/app/serializers/spree/api/v3/product_category_serializer.rb +2 -10
  81. data/app/serializers/spree/api/v3/product_collection_serializer.rb +2 -10
  82. data/app/serializers/spree/api/v3/seller/bulk_product_result_serializer.rb +22 -0
  83. data/app/serializers/spree/api/v3/seller/fulfillment_serializer.rb +1 -3
  84. data/app/serializers/spree/api/v3/seller/password_reset_acknowledgement_serializer.rb +18 -0
  85. data/app/serializers/spree/api/v3/seller/payout_account_link_serializer.rb +18 -0
  86. data/app/serializers/spree/api/v3/seller/stock_location_serializer.rb +8 -3
  87. data/app/services/spree/api/v3/filters_aggregator.rb +1 -5
  88. data/config/routes.rb +3 -1
  89. data/lib/spree/api/dependencies.rb +7 -2
  90. data/lib/spree/api/openapi/schema_helper.rb +86 -2
  91. data/lib/spree/api/preference_types.rb +8 -0
  92. data/lib/spree/api/testing_support/deterministic_openapi.rb +87 -0
  93. data/lib/spree/api/testing_support/openapi.rb +2 -1
  94. data/lib/spree/api/testing_support/openapi_tasks.rb +3 -0
  95. data/lib/spree/api/testing_support/response_schema.rb +64 -0
  96. data/lib/spree/api.rb +1 -0
  97. data/lib/typelizer/nullable_references.rb +31 -0
  98. data/openapi/admin.yaml +3571 -524
  99. data/openapi/store.yaml +353 -153
  100. metadata +29 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c9c668a0a2a022baab4a46225033a2db830c03710a05610630d994ab1ee769d
4
- data.tar.gz: 2e094bd51c9c1b004e2f268b43255299c75e146198084d232b738023c4825c2c
3
+ metadata.gz: 656d7a33161a0986eee55f9232a07b0c85d38d23aa775411a615590e231599be
4
+ data.tar.gz: df72b082fc862390b6851a68ab3b7a7c56ba1aad94ec9edb6aac185e2e6d5f9e
5
5
  SHA512:
6
- metadata.gz: e08693f225926e85bee796f333f2cdeae0a44a714fafc6761f3666da01efea4c9193cfad5ba5fd3e50d82c6ba8ac24a1196858565f4da26a7cc665cc43a719e2
7
- data.tar.gz: 609aaee1a7a16894f7a6c4a5d328eb3dc7279d730fd6b1932ffdf4e080e27b97af4e07520efffbe8a8ed36d20ecdc2d0baf370b54ab08bd9176c40419892dbac
6
+ metadata.gz: 77807c1b06c9710618844910cf8d1c8f0e891bfa6dc0f8ba45e157c4d6f25acd806409111f5bd7c2710b56a703c385960680f27791901282fa8e571d796bf887
7
+ data.tar.gz: 719263da6eefe5a86b5d01453e37fdcf1b639e9f7b9791656b8db5604f98d8d01eb82e987a8f0ae98e51331580976cba7ea1faac08ce590acda385f47f817deb
data/Rakefile CHANGED
@@ -25,6 +25,9 @@ namespace :rswag do
25
25
 
26
26
  # OPENAPI env var prevents spec_helper from overriding --order defined with :random
27
27
  ENV['OPENAPI'] = 'true'
28
+ # Signed blob ids and file URLs in the examples are keyed on the app's
29
+ # secret, which `rails new` generates per dummy app.
30
+ ENV['SECRET_KEY_BASE'] = 'openapi-example-secret-key-base'
28
31
  t.rspec_opts = ['--format Rswag::Specs::SwaggerFormatter', '--order defined']
29
32
  end
30
33
 
@@ -59,7 +59,7 @@ module Spree
59
59
  added = scope.where(id: new_products.map(&:id)).reorder(nil).distinct.count
60
60
  end
61
61
 
62
- render json: { added_count: added }, status: :created
62
+ render json: Spree.api.admin_product_membership_result_serializer.new({ added_count: added }).to_h, status: :created
63
63
  end
64
64
 
65
65
  # DELETE /{parents}/:parent_id/products
@@ -74,7 +74,7 @@ module Spree
74
74
  removed = products.size - still_members
75
75
  end
76
76
 
77
- render json: { removed_count: removed }
77
+ render json: Spree.api.admin_product_membership_result_serializer.new({ removed_count: removed }).to_h
78
78
  end
79
79
 
80
80
  protected
@@ -11,6 +11,13 @@ module Spree
11
11
  # beside it as the fallback for clients without their own translation
12
12
  # (integrations, scripts, and the dashboard's own last resort).
13
13
  #
14
+ # A validator records the rejected `value` as the model holds it: a raw
15
+ # primary key for a foreign key, a class name for a type column, the
16
+ # whole record when the validation names an association. An entry
17
+ # reports each of those the way a client writes that attribute — a
18
+ # prefixed id, a short type name — and leaves it out when there is no
19
+ # such form.
20
+ #
14
21
  # Store API responses keep the flat `{ attribute => [message] }` shape
15
22
  # — this override is deliberately not in the shared error handler.
16
23
  module ValidationDetails
@@ -19,30 +26,44 @@ module Spree
19
26
  # @param errors [ActiveModel::Errors]
20
27
  # @return [Hash{Symbol => Array<Hash>}] per-attribute error entries
21
28
  def format_validation_details(errors)
22
- errors.messages.each_with_object({}) do |(attribute, messages), result|
23
- # `details` and `messages` are populated in lockstep by
24
- # ActiveModel, so the nth message describes the nth detail. A
25
- # message added as a bare string has no symbol and reports a
26
- # nil code, which the dashboard reads as "render the message".
27
- details = errors.details[attribute] || []
28
-
29
- result[wire_error_attribute(errors, attribute)] = messages.each_with_index.map do |message, index|
30
- detail = details[index] || {}
31
- code = detail[:error].is_a?(Symbol) ? detail[:error] : nil
29
+ errors.group_by_attribute.each_with_object({}) do |(attribute, attribute_errors), result|
30
+ result[wire_error_attribute(errors, attribute)] = attribute_errors.map do |error|
31
+ # A message added as a bare string has no symbol and reports a
32
+ # nil code, which the dashboard reads as "render the message".
33
+ code = error.raw_type.is_a?(Symbol) ? error.raw_type : nil
34
+ message = error.message
35
+ values = error.details.except(:error)
32
36
 
33
37
  # Interpolation values first, so the keys below are the ones
34
38
  # this formatter computed: a validation may itself carry a
35
39
  # `code:` option, and it must not displace the Rails error.
36
- detail.except(:error).merge(
40
+ wire_error_values(error, values).merge(
37
41
  code: code,
38
42
  message: message,
39
- specific: code.present? &&
40
- specific_message?(errors, attribute, code, message, detail.except(:error))
43
+ specific: code.present? && specific_message?(errors, attribute, code, message, values)
41
44
  )
42
45
  end
43
46
  end
44
47
  end
45
48
 
49
+ def wire_error_values(error, values)
50
+ # An error copied up from a nested record keeps the record and the
51
+ # attribute it was raised on inside it.
52
+ error = error.inner_error while error.respond_to?(:inner_error)
53
+
54
+ values.each_with_object({}) do |(key, value), result|
55
+ wire_value = wire_error_value(error.base, error.attribute.to_s, key, value)
56
+ result[key] = wire_value unless wire_value.nil? && !value.nil?
57
+ end
58
+ end
59
+
60
+ def wire_error_value(record, attribute, key, value)
61
+ return value.try(:prefixed_id) if value.is_a?(ActiveRecord::Base)
62
+ return value unless key == :value && value.present? && record.respond_to?(:api_value_for)
63
+
64
+ record.api_value_for(attribute, value)
65
+ end
66
+
46
67
  # Whether `message` says more than its `code` alone would.
47
68
  #
48
69
  # A code like `:invalid` is generic, and a client holding its own
@@ -14,20 +14,26 @@ module Spree
14
14
  # @param provider [String, nil] defaults to the +provider+ param, then :email
15
15
  # @return [Object, nil] a strategy instance, or nil after rendering an error
16
16
  def authentication_strategy(provider = nil)
17
- provider ||= params[:provider].presence || 'email'
17
+ # Only a missing or empty-string provider means email; `provider[]=`
18
+ # is malformed, not missing.
19
+ provider = params[:provider] if provider.nil?
20
+ provider = 'email' if provider.nil? || provider == ''
18
21
 
19
- strategy = authentication_strategies.build(
20
- provider,
21
- params: params,
22
- request_env: request.headers.env,
23
- user_class: authentication_user_class
24
- )
22
+ # `provider[]=email` is not a provider name the registry can look up.
23
+ strategy = if provider.is_a?(String) || provider.is_a?(Symbol)
24
+ authentication_strategies.build(
25
+ provider,
26
+ params: params,
27
+ request_env: request.headers.env,
28
+ user_class: authentication_user_class
29
+ )
30
+ end
25
31
 
26
32
  return strategy if strategy
27
33
 
28
34
  render_error(
29
35
  code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:invalid_provider],
30
- message: "Unsupported authentication provider: #{provider}",
36
+ message: "Unsupported authentication provider: #{provider if provider.is_a?(String)}".strip,
31
37
  status: :bad_request
32
38
  )
33
39
  nil
@@ -175,7 +175,9 @@ module Spree
175
175
  def render_service_error(error, code: ERROR_CODES[:processing_error], status: :unprocessable_content)
176
176
  error = error.value if error.is_a?(Spree::ServiceModule::ResultError) && error.value.is_a?(Symbol)
177
177
 
178
- if error.is_a?(ActiveModel::Errors)
178
+ if error.is_a?(Spree::ServiceModule::ResultError) && error.value.is_a?(Hash)
179
+ render_failure_hash(error.value, code: code, status: status)
180
+ elsif error.is_a?(ActiveModel::Errors)
179
181
  render_validation_error(error, code: code)
180
182
  elsif error.is_a?(String)
181
183
  render_error(code: code, message: error, status: status)
@@ -189,6 +191,26 @@ module Spree
189
191
  end
190
192
  end
191
193
 
194
+ # A workflow failure that names its reason in a hash (`failure(cart, code:,
195
+ # errors:)`) reads as its messages; printed whole it reached clients as a
196
+ # Ruby hash. The endpoint's own code stays the response code, so clients
197
+ # keyed on it are unaffected, and the hash's reason goes in the details.
198
+ #
199
+ # @param failure [Hash]
200
+ def render_failure_hash(failure, code:, status:)
201
+ failure = failure.to_h.symbolize_keys
202
+ messages = Array(failure[:errors]).filter_map do |entry|
203
+ entry.is_a?(Hash) ? (entry[:message] || entry['message']).presence : entry.to_s.presence
204
+ end
205
+
206
+ # A failure naming only its reason reads as that reason, translated
207
+ # the way a bare symbol failure is.
208
+ message = failure[:message].presence || messages.join(', ').presence
209
+ message ||= I18n.t("spree.#{failure[:code]}", default: failure[:code].to_s.humanize) if failure[:code].present?
210
+
211
+ render_error(code: code, message: message, status: status, details: failure.except(:message))
212
+ end
213
+
192
214
  # Renders a failed workflow or service result. A tax engine failure the
193
215
  # workflow caught itself (so it could roll the cart back) answers as
194
216
  # handle_tax_provider_error would have: 503 for an outage, the one
@@ -291,8 +313,13 @@ module Spree
291
313
  )
292
314
  end
293
315
 
316
+ # A missing parameter, a failed validation and an unparseable body are
317
+ # the request's fault, and the response already tells the client what
318
+ # to fix. They are not reported to the error tracker: that is the API
319
+ # working as designed, and reporting it buries real faults under every
320
+ # typo, rejected email or probe. Gateway and tax engine failures are
321
+ # still reported.
294
322
  def handle_parameter_missing(exception)
295
- Rails.error.report(exception, context: error_context, source: 'spree.api.v3')
296
323
  render_error(
297
324
  code: ERROR_CODES[:parameter_missing],
298
325
  message: exception.message,
@@ -300,8 +327,8 @@ module Spree
300
327
  )
301
328
  end
302
329
 
330
+ # Not reported, like a missing parameter.
303
331
  def handle_record_invalid(exception)
304
- Rails.error.report(exception, context: error_context, source: 'spree.api.v3')
305
332
  render_validation_error(exception.record.errors)
306
333
  end
307
334
 
@@ -344,8 +371,8 @@ module Spree
344
371
  )
345
372
  end
346
373
 
374
+ # Not reported: a body that is not valid JSON is the client's mistake.
347
375
  def handle_parse_error(exception)
348
- Rails.error.report(exception, context: error_context, source: 'spree.api.v3')
349
376
  message = exception.respond_to?(:original_message) ? exception.original_message : exception.message
350
377
  render_error(
351
378
  code: ERROR_CODES[:invalid_request],
@@ -0,0 +1,28 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ # Answers a request to send an invitation again, the same way on every
5
+ # API: the invitation when the email went out, otherwise why not — the
6
+ # real refusal (expired, accepted or revoked) or the record's errors.
7
+ module InvitationResend
8
+ private
9
+
10
+ # @yieldparam invitation [Spree::Invitation] the invitation, sent again
11
+ # @yieldreturn [Hash] its serialized form
12
+ def render_invitation_resend(invitation)
13
+ if invitation.resend!
14
+ render json: yield(invitation)
15
+ elsif invitation.resend_refusal
16
+ render_error(
17
+ code: ErrorHandler::ERROR_CODES[:processing_error],
18
+ message: I18n.t("spree.invitation_#{invitation.resend_refusal}"),
19
+ status: :unprocessable_content
20
+ )
21
+ else
22
+ render_validation_error(invitation.errors)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -163,7 +163,7 @@ module Spree
163
163
  #
164
164
  # @return [String, nil]
165
165
  def currency_from_params
166
- params[:currency].presence
166
+ string_param(:currency).presence
167
167
  end
168
168
 
169
169
  # Resolves the market from the +x-spree-country+ header or +params[:country]+.
@@ -20,6 +20,11 @@ module Spree
20
20
  end
21
21
  end
22
22
 
23
+ # Serialize the entries of a `types` list
24
+ def serialize_types(entries, serializer = Spree.api.admin_resource_type_definition_serializer)
25
+ entries.map { |entry| serializer.new(entry).to_h }
26
+ end
27
+
23
28
  # Params passed to serializers
24
29
  def serializer_params
25
30
  {
@@ -145,13 +145,23 @@ module Spree
145
145
  raise MissingScopedResource, self.class unless resource
146
146
 
147
147
  ability = current_ability
148
- return unless ability.respond_to?(:permission_keys)
149
-
150
148
  required = "#{action_kind}_#{resource}"
151
149
  # Scope names outside the catalog (the exports/imports `:all`
152
150
  # fallback, host-registered types) defer to CanCanCan's `authorize!`
153
151
  # — the caller stays gated per subject, just not by key.
154
152
  return unless Spree.permissions.key?(required)
153
+
154
+ # A custom ability without catalog keys leaves the staffer's own
155
+ # authority to CanCanCan, but a delegated credential is still held
156
+ # to its grant: the person consented to part of their authority, and
157
+ # CanCanCan only ever sees the whole of it.
158
+ unless ability.respond_to?(:permission_keys)
159
+ granted = delegated_permission_keys
160
+ return if granted.nil? || granted.include?(required)
161
+
162
+ return render_missing_permission(required)
163
+ end
164
+
155
165
  return if staff_permission_keys.include?(required)
156
166
 
157
167
  Rails.logger.info do
@@ -228,7 +238,14 @@ module Spree
228
238
  return current_api_key.has_scope?(key) if scope_limited_principal?
229
239
 
230
240
  ability = current_ability
231
- return true unless ability.respond_to?(:permission_keys)
241
+ # A custom ability without catalog keys leaves the staffer's own
242
+ # authority to CanCanCan, but a delegated credential still narrows
243
+ # it: the grant is checked on its own, or an OAuth token approved for
244
+ # `read_reports` would pass every member check its owner could.
245
+ unless ability.respond_to?(:permission_keys)
246
+ granted = delegated_permission_keys
247
+ return granted.nil? || granted.include?(key.to_s)
248
+ end
232
249
 
233
250
  staff_permission_keys.include?(key.to_s)
234
251
  end
@@ -25,12 +25,13 @@ module Spree
25
25
 
26
26
  # DELETE /api/v3/admin/admin_users/:id
27
27
  # Removes role assignments for the current store rather than deleting
28
- # the account globally. The user keeps access to any other stores.
28
+ # the account globally, and revokes their pending invitations here.
29
+ # The user keeps access to any other stores.
29
30
  def destroy
30
31
  authorize!(:destroy, @resource)
31
32
  return if reject_unauthorized_role_removal!(@resource, @resource.spree_roles.for_resource(current_store).to_a)
32
33
 
33
- @resource.role_users.where(role: current_store.roles).destroy_all
34
+ current_store.remove_user(@resource)
34
35
  head :no_content
35
36
  end
36
37
 
@@ -116,8 +117,12 @@ module Spree
116
117
  # Adds missing assignments and removes extras — no-op for unchanged.
117
118
  def apply_role_ids(desired_role_ids)
118
119
  store_roles = current_store.roles
119
- current = @resource.role_users.where(role: store_roles).pluck(:role_id).map(&:to_s)
120
120
  target = desired_role_ids.map(&:to_s)
121
+ # A list naming no role of this store — empty, or only unknown ids —
122
+ # leaves the person with no store role: a removal from staff.
123
+ return current_store.remove_user(@resource) unless store_roles.exists?(id: target)
124
+
125
+ current = @resource.role_users.where(role: store_roles).pluck(:role_id).map(&:to_s)
121
126
 
122
127
  store_roles.where(id: target - current).each do |role|
123
128
  @resource.role_users.find_or_create_by!(role: role)
@@ -83,7 +83,11 @@ module Spree
83
83
  )
84
84
  end
85
85
 
86
- render json: serialize_resource(current_api_key)
86
+ key = serialize_resource(current_api_key)
87
+ # Any key may describe itself, but where its creator was is api_keys data.
88
+ # Blanked rather than dropped, since the ApiKey schema requires the field.
89
+ key = key.merge('created_by_ip_address' => nil) unless current_api_key.has_scope?('read_api_keys')
90
+ render json: key
87
91
  end
88
92
 
89
93
  protected
@@ -105,6 +109,7 @@ module Spree
105
109
  def build_resource
106
110
  scope.new(permitted_params).tap do |key|
107
111
  key.created_by = current_actor
112
+ key.created_by_ip_address = request.remote_ip
108
113
  end
109
114
  end
110
115
 
@@ -192,7 +192,7 @@ module Spree
192
192
  end
193
193
 
194
194
  def valid_oauth_state?
195
- state = params[:state]
195
+ state = string_param(:state)
196
196
  return false if state.blank?
197
197
 
198
198
  # The verifier round-trips through JSON, so payload keys come back as strings.
@@ -85,7 +85,8 @@ module Spree
85
85
  )
86
86
  end
87
87
 
88
- render json: { added_count: @resource.import_products_from_price_list }
88
+ added = @resource.import_products_from_price_list
89
+ render json: Spree.api.admin_product_membership_result_serializer.new({ added_count: added }).to_h
89
90
  end
90
91
 
91
92
  # POST /api/v3/admin/catalogs/:id/assign
@@ -53,7 +53,7 @@ module Spree
53
53
  published_at: params[:published_at].presence,
54
54
  unpublished_at: params[:unpublished_at].presence
55
55
  )
56
- render json: { product_count: count }
56
+ render json: Spree.api.admin_bulk_product_result_serializer.new({ product_count: count }).to_h
57
57
  end
58
58
 
59
59
  # POST /api/v3/admin/channels/:id/remove_products
@@ -62,7 +62,7 @@ module Spree
62
62
  authorize! :update, channel
63
63
 
64
64
  removed = channel.remove_products(scoped_product_ids)
65
- render json: { product_count: removed }
65
+ render json: Spree.api.admin_bulk_product_result_serializer.new({ product_count: removed }).to_h
66
66
  end
67
67
 
68
68
  def create
@@ -16,7 +16,7 @@ module Spree
16
16
  def types
17
17
  authorize! :read, model_class
18
18
 
19
- render json: { data: model_class.subclasses_with_preference_schema }
19
+ render json: { data: serialize_types(model_class.subclasses_with_preference_schema) }
20
20
  end
21
21
 
22
22
  protected
@@ -101,7 +101,7 @@ module Spree
101
101
  }
102
102
  end
103
103
 
104
- render json: { data: data }
104
+ render json: { data: serialize_types(data, Spree.api.admin_calculator_type_serializer) }
105
105
  end
106
106
 
107
107
  # GET /api/v3/admin/delivery_methods/fulfillment_providers
@@ -15,8 +15,8 @@ module Spree
15
15
  def index
16
16
  authorize! :read, Spree::EmailTemplate
17
17
 
18
- entries = Spree::EmailTemplates::Entry.all(current_store, locale: language)
19
- render json: { data: serialize_collection(entries), meta: {} }
18
+ entries = paginate_whole(Spree::EmailTemplates::Entry.all(current_store, locale: language))
19
+ render json: { data: serialize_collection(entries), meta: collection_meta(entries) }
20
20
  end
21
21
 
22
22
  # GET /api/v3/admin/email_templates/:id
@@ -40,9 +40,19 @@ module Spree
40
40
  # signed-URL endpoint because the SPA's Vite proxy only forwards
41
41
  # `/api/*`. A cross-origin redirect to `/rails/active_storage/...`
42
42
  # strips the Authorization header and the download fails silently.
43
+ # POST /api/v3/admin/exports
44
+ def create
45
+ @resource = build_resource
46
+ authorize_resource!(@resource, :create)
47
+ return if render_missing_member_scopes(@resource)
48
+
49
+ save_and_render(@resource, status: :created)
50
+ end
51
+
43
52
  def download
44
53
  @resource = find_resource
45
54
  authorize_resource!(@resource, :show)
55
+ return if render_missing_member_scopes(@resource)
46
56
 
47
57
  unless @resource.done?
48
58
  return render_error(
@@ -146,6 +156,45 @@ module Spree
146
156
 
147
157
  private
148
158
 
159
+ # A report export reads whatever its query groups or filters by, so
160
+ # it is held to the per-member scopes the reporting endpoint checks
161
+ # (customers, products, promotions…), not just `read_reports`. The
162
+ # export model checks the requesting user's ability; an OAuth grant
163
+ # authenticates as the staff member who approved it, so without
164
+ # this a grant for `read_reports` alone could export, or download,
165
+ # a report broken down by customer — names, emails and spend.
166
+ #
167
+ # @return [Boolean] whether the request was refused
168
+ def render_missing_member_scopes(export)
169
+ required = export.required_key_scopes
170
+
171
+ if required.nil?
172
+ # A new export's query that does not compile is the model's to
173
+ # refuse, with a validation error naming the problem. A stored
174
+ # one whose members can no longer be resolved fails closed:
175
+ # what it reads cannot be checked against this credential.
176
+ return false if export.new_record?
177
+
178
+ render_error(
179
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
180
+ message: 'This report export can no longer be checked against your permissions',
181
+ status: :forbidden
182
+ )
183
+ return true
184
+ end
185
+
186
+ missing = required.reject { |scope| holds_permission?(scope) }
187
+ return false if missing.empty?
188
+
189
+ render_error(
190
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
191
+ message: "Missing permission: #{missing.join(', ')}",
192
+ status: :forbidden,
193
+ details: { required_scopes: missing }
194
+ )
195
+ true
196
+ end
197
+
149
198
  def export_class
150
199
  action_name == 'create' ? resolve_export_type(params[:type]) : find_resource.class
151
200
  end
@@ -39,6 +39,15 @@ module Spree
39
39
  @resource.with_lock { super }
40
40
  end
41
41
 
42
+ # Spree::GiftCards::Destroy takes the card off any open cart or draft
43
+ # order holding it, then deletes it; a refusal answers 422.
44
+ def destroy
45
+ result = Spree.gift_card_destroy_workflow.call(gift_card: @resource)
46
+ return head(:no_content) if result.success?
47
+
48
+ render_service_error(result.error, code: ERROR_CODES[:validation_error])
49
+ end
50
+
42
51
  protected
43
52
 
44
53
  def model_class
@@ -35,7 +35,7 @@ module Spree
35
35
  def types
36
36
  authorize! :create, model_class
37
37
 
38
- render json: { data: Spree::Integration.discovery_entries }
38
+ render json: { data: serialize_types(Spree::Integration.discovery_entries, Spree.api.admin_integration_type_definition_serializer) }
39
39
  end
40
40
 
41
41
  # POST /api/v3/admin/integrations/:id/test
@@ -45,7 +45,9 @@ module Spree
45
45
  authorize_resource!(@resource, :update)
46
46
 
47
47
  connected = @resource.can_connect?
48
- render json: { connected: connected, error_message: connected ? nil : @resource.connection_error_message }
48
+ render json: Spree.api.admin_integration_connection_result_serializer.new(
49
+ { connected: connected, error_message: connected ? nil : @resource.connection_error_message }
50
+ ).to_h
49
51
  end
50
52
 
51
53
  protected
@@ -56,7 +56,7 @@ module Spree
56
56
  # leaking whether an ID exists.
57
57
  def load_invitation
58
58
  decoded_id = Spree::Invitation.decode_prefixed_id(params[:id])
59
- @invitation = Spree::Invitation.pending.not_expired.find_by(id: decoded_id, token: params[:token])
59
+ @invitation = Spree::Invitation.pending.not_expired.find_by(id: decoded_id, token: string_param(:token).to_s)
60
60
 
61
61
  unless @invitation
62
62
  render_error(
@@ -99,7 +99,7 @@ module Spree
99
99
  end
100
100
 
101
101
  def create_new_invitee(invitation)
102
- if params[:password].blank?
102
+ if string_param(:password).blank?
103
103
  render_error(
104
104
  code: ERROR_CODES[:parameter_missing],
105
105
  message: 'Password is required to create your account',
@@ -108,7 +108,14 @@ module Spree
108
108
  return nil
109
109
  end
110
110
 
111
- Spree.admin_user_class.create!(signup_params(invitation))
111
+ # A rejected sign-up (a weak password, a mismatched confirmation, an
112
+ # email the store's own validations refuse) is answered with the
113
+ # field errors, and the invitation stays pending.
114
+ user = Spree.admin_user_class.new(signup_params(invitation))
115
+ return user if user.save
116
+
117
+ render_validation_error(user.errors)
118
+ nil
112
119
  end
113
120
 
114
121
  def signup_params(invitation)
@@ -8,6 +8,7 @@ module Spree
8
8
  # becomes a member of the staff list for this store.
9
9
  class InvitationsController < ResourceController
10
10
  include Spree::Api::V3::Admin::RoleGrantGuard
11
+ include Spree::Api::V3::InvitationResend
11
12
 
12
13
  # Staff management is the escalation path — its own scope pair,
13
14
  # grantable separately from store configuration (6.0-admin-rbac.md).
@@ -30,15 +31,13 @@ module Spree
30
31
  end
31
32
 
32
33
  # PATCH /api/v3/admin/invitations/:id/resend
33
- # Issues a fresh token + email for an existing pending invitation.
34
- # The model's `resend!` is responsible for resetting `expires_at`
35
- # and dispatching the mailer.
34
+ # Issues a fresh token + email for an existing pending invitation, and
35
+ # starts its expiry window again.
36
36
  def resend
37
37
  @resource = find_resource
38
38
  authorize!(:update, @resource)
39
39
 
40
- @resource.resend!
41
- render json: serialize_resource(@resource)
40
+ render_invitation_resend(@resource) { |invitation| serialize_resource(invitation) }
42
41
  end
43
42
 
44
43
  protected
@@ -28,7 +28,7 @@ module Spree
28
28
  def types
29
29
  authorize! :read, model_class
30
30
 
31
- render json: { data: model_class.subclasses_with_preference_schema }
31
+ render json: { data: serialize_types(model_class.subclasses_with_preference_schema) }
32
32
  end
33
33
 
34
34
  protected
@@ -31,7 +31,14 @@ module Spree
31
31
  # The source must belong to the customer assigned to the order.
32
32
  def create
33
33
  with_order_lock do
34
- payment_method = current_store.payment_methods.accessible_by(current_ability, :show).find_by_prefix_id!(params[:payment_method_id])
34
+ # Managing payments is enough to pick any of the store's payment
35
+ # methods. A caller who reads settings stays bound by the
36
+ # ability's rules, as the payment methods list binds them.
37
+ payment_methods = current_store.payment_methods
38
+ if holds_permission?('read_settings')
39
+ payment_methods = payment_methods.accessible_by(current_ability, :show)
40
+ end
41
+ payment_method = payment_methods.find_by_prefix_id!(params[:payment_method_id])
35
42
  # The default is what the order still owes beside the payments
36
43
  # already recorded, which is nothing once they cover it — a zero
37
44
  # payment is refused the same way a zero amount is.
@@ -100,11 +107,12 @@ module Spree
100
107
 
101
108
  # Saved-source charges require an order customer — sources are scoped
102
109
  # to that customer to prevent attaching customer A's card to
103
- # customer B's order. Refuse if no customer is assigned.
110
+ # customer B's order. Refuse if no customer is assigned. Cards are
111
+ # also scoped to this payment method.
104
112
  def find_source!(payment_method, source_id)
105
113
  raise ActiveRecord::RecordNotFound unless @parent.customer
106
114
 
107
- @parent.customer.credit_cards.find_by_prefix_id!(source_id)
115
+ @parent.customer.credit_cards.where(payment_method: payment_method).find_by_prefix_id!(source_id)
108
116
  end
109
117
  end
110
118
  end