Problem/Motivation

Customer access to carts and checkout is decided outside entity access, so code without a route (REST/JSON:API, an MCP server, custom integrations) cannot ask "can this user check out this order?" or "can this user change this cart?".

  • Checkout: CheckoutController::checkAccess() checks the uid or cart session, a published store and, for draft orders, the cart flag. It is used by commerce_checkout.form and the payment return and cancel routes.
  • Cart: commerce_cart's CommerceCartHooks::orderAccess() grants update on uid or cart session, but only on front-end routes: on admin routes it returns neutral (adminContext->isAdminRoute()) and adds the route.name cache context. update therefore means "admin edit" or "change my cart" depending on the route.

Proposed resolution

Operation Means Granted by
update Admin edit of the order or order item Permissions only
update_cart Change a draft cart and its items commerce_cart: draft state, cart flag, uid or cart session; for order items, the order and the item are not locked
checkout Go through checkout commerce_checkout: uid or cart session, published store, non-empty order, cart flag for draft orders, access checkout permission; forbidden for canceled orders
  • checkout: the conditions from CheckoutController::checkAccess() move to a hook_commerce_order_access() implementation in commerce_checkout (commerce_order can't depend on the cart session or the checkout permission). commerce_checkout.form and the payment return/cancel routes use _entity_access: 'commerce_order.checkout'.
  • update_cart: commerce_cart grants it on orders and order items instead of update, so the admin route check (AdminContext) and the route.name cache context go away. Order item update is the admin edit too, which keeps customers off the admin order item routes.
  • The access handlers aren't special-cased: users with administer commerce_order pass both operations, as for any other operation.
  • OrderRefresh::shouldRefresh() checks update_cart for anonymous customers.

This also prepares organization-owned orders (see #3638816): ownership then lives in entity access only, where a B2B module can grant it.

Remaining tasks

  • Move the conditions and switch the routes.
  • Update the cart access tests. The commerce_checkout tests need CI (4.x requires core ^11.5).
  • Change record.
  • Follow-up in commerce_api: cart routes move from commerce_order.update / commerce_order_item.update to update_cart, and the checkout resource to checkout.

User interface changes

None.

API changes

Breaking, no transition release:

  • Code checking $order->access('update') or $order_item->access('update') for customers must check update_cart. This includes payment gateway routes requiring commerce_order.update (e.g. an approve route on the payment step), which should use checkout.
  • Customers can no longer modify carts through core JSON:API or REST; use commerce_api.
  • Users with administer commerce_order now pass checkout on any order (previously only the order's customer could access its checkout).
  • CheckoutController::checkAccess() is removed, and CheckoutController no longer takes the cart session.

Data model changes

None.

Issue fork commerce-3639234

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Issue summary: View changes

jsacksick’s picture

Issue summary: View changes
Status: Active » Needs review

Pushed to 3639234-add-checkout-and (!797) and updated the issue summary. Changes from the original proposal:

  • checkout is granted by commerce_checkout, not OrderAccessControlHandler: the anonymous check needs the cart session (commerce_cart) and the access checkout permission.
  • Order items get update_cart too. Mapping order item update to the order's update_cart reopened the admin order item edit form to customers.
  • No admin special case in the access handlers, so administer commerce_order passes both operations.

Behavior changes are listed under API changes. The commerce_checkout tests need CI (4.x requires core ^11.5).