You are here

class ShipmentCollectionAccessCheck in Commerce Shipping 8.2

Defines an access checker for the Shipment collection route.

Hierarchy

Expanded class hierarchy of ShipmentCollectionAccessCheck

1 string reference to 'ShipmentCollectionAccessCheck'
commerce_shipping.services.yml in ./commerce_shipping.services.yml
commerce_shipping.services.yml
1 service uses ShipmentCollectionAccessCheck
access_check.shipment_collection in ./commerce_shipping.services.yml
Drupal\commerce_shipping\Access\ShipmentCollectionAccessCheck

File

src/Access/ShipmentCollectionAccessCheck.php, line 15

Namespace

Drupal\commerce_shipping\Access
View source
class ShipmentCollectionAccessCheck implements AccessInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new ShipmentCollectionAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Checks access to the Shipment collection.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShipmentCollectionAccessCheck::$entityTypeManager protected property The entity type manager.
ShipmentCollectionAccessCheck::access public function Checks access to the Shipment collection.
ShipmentCollectionAccessCheck::__construct public function Constructs a new ShipmentCollectionAccessCheck object.