The checkout process lacks some access checks. Let's copy commerce 1.x behavior.

What we have in 2.x:

Drupal\commerce_checkout\Controller\CheckoutController::checkAccess()
  1. Check if the order has line items.
  2. Check if the user has 'access checkout' permission.
  3. For authenticated users check if the order owner is the same if the current user.
  4. For anonymous users check the cartSession for the order id.

All of the above is in 1.x (see commerce_checkout_access() and commerce_checkout_router(). Additional checks in 1.x:

  1. If the order is not in a checkout status, deny access for any page but the completion page unless the order is still a shopping cart. See commerce_checkout_page_access().
  2. If the order is still in checkout, only allow access to pages that it is currently on or has previously completed. See commerce_checkout_page_access().
  3. Don't allow completion page access for orders in the cart or canceled states. See commerce_checkout_page_access().

All these missing checks can go to Drupal\commerce_checkout\Controller\CheckoutController::checkAccess().

Comments

czigor created an issue. See original summary.

czigor’s picture

Issue summary: View changes
bojanz’s picture

Title: Checkout access » Checkout access is incomplete
Priority: Normal » Critical

Thanks! This is way more important than "normal".

czigor’s picture

Assigned: Unassigned » czigor

I'm working on this.

czigor’s picture

That's where I am right now: https://github.com/czigor/commerce/commit/5edbfd3cf8860ce41a310274a31d4e...

I got stuck in the test when trying to get the checkout_flow belonging to an order (which I need in line 179). Any help would be appreciated.

bojanz’s picture

$order->checkout_flow is an entity reference, you should be able to use that.

czigor’s picture

Yes, that's what I thought, but

$order->checkout_flow->getPlugin()->getNextStepId()

does not seem to work for me (Fatal error: Call to undefined method Drupal\Core\Field\EntityReferenceFieldItemList::getPlugin()) . Weirdly,

$order->checkout_flow[0]->getEntity()

returns an order entity. I get this behaviour in devel/php for any order, regardless whether the order is in checkout or completed, whether the order was created on the UI or from code (based on CheckoutOrderTest::testOrderAccess()). The code I use in devel/php is:

use Drupal\commerce_checkout\Entity\CheckoutFlow;
use Drupal\commerce_order\Entity\Order;
use Drupal\profile\Entity\Profile;
use Drupal\commerce_order\Entity\OrderItem;

$order = entity_load('commerce_order', 44);
kint($order->checkout_flow->getPlugin()->getNextStepId());

Am I missing something or could this be a commerce bug?

bojanz’s picture

Just general entity_reference unpleasantness :)
$order->checkout_flow->entity->getPlugin()->getNextStepId()
(->entity is not the same as ->getEntity() which just returns the parent entity. DX++)

czigor’s picture

Status: Active » Needs review

Thanks for the hints! In the end the main problem was that I was trying to access checkoutFlow steps outside the checkout, which is not possible.

Here are my PRs:
https://github.com/drupalcommerce/commerce/pull/551 (only tests, should fail)
https://github.com/drupalcommerce/commerce/pull/550

(Is this the recommended way to do tests?)

steveoliver’s picture

Status: Needs review » Needs work

So close! Commented on the PR here -- https://github.com/drupalcommerce/commerce/pull/550#pullrequestreview-20.... Just need to add a store to the order under test.

czigor’s picture

Status: Needs work » Needs review

Rebased the PR, tests are green now.

czigor’s picture

Status: Needs review » Needs work

They are not green.

bojanz’s picture

Assigned: czigor » bojanz

My turn.

  • bojanz committed fc4412b on 8.x-2.x authored by czigor
    Issue #2824320 by czigor, bojanz: Checkout access is incomplete
    
bojanz’s picture

Status: Needs work » Fixed

Problems I had with the PR:

1) CheckoutFlowBase still had its own step ID handling, which duplicated the controller logic.
2) The controller logic couldn't be reused, made more sense on a service such as CheckoutOrderManager
3) The logic didn't account for "allow access to a previously completed step".
4) Tests were failing due to the hacky way CheckoutFlowBase was assigning the step at __construct(), which was too early and magical.

Many rewrites later, the final commit has been pushed.
We now have a single source of the checkout step, and plenty of new tests.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.