You are here

public function ShipmentCollectionAccessCheck::access in Commerce Shipping 8.2

Checks access to the Shipment collection.

Parameters

\Symfony\Component\Routing\Route $route: The route to check against.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Access/ShipmentCollectionAccessCheck.php, line 47

Class

ShipmentCollectionAccessCheck
Defines an access checker for the Shipment collection route.

Namespace

Drupal\commerce_shipping\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $route_match
    ->getParameter('commerce_order');
  $order_type_storage = $this->entityTypeManager
    ->getStorage('commerce_order_type');

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  $order_type = $order_type_storage
    ->load($order
    ->bundle());
  $shipment_type_id = $order_type
    ->getThirdPartySetting('commerce_shipping', 'shipment_type');

  // Check if this is a cart order.
  $order_is_cart = $order
    ->hasField('cart') && $order
    ->get('cart')->value;
  $access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler('commerce_shipment');

  // Only allow access if order type has a corresponding shipment type.
  return AccessResult::allowedIf($shipment_type_id !== NULL)
    ->andIf(AccessResult::allowedIf($access_control_handler
    ->createAccess($shipment_type_id, $account)))
    ->andIf(AccessResult::allowedIf(!$order_is_cart))
    ->addCacheableDependency($order_type)
    ->addCacheableDependency($order);
}