You are here

protected function PaymentMethodAccessControlHandler::checkAccess in Commerce Core 8.2

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

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

Overrides EntityAccessControlHandler::checkAccess

File

modules/payment/src/PaymentMethodAccessControlHandler.php, line 19

Class

PaymentMethodAccessControlHandler
Defines the access control handler for payment methods.

Namespace

Drupal\commerce_payment

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $entity */
  if ($operation == 'update') {
    $payment_gateway = $entity
      ->getPaymentGateway();

    // Deny access if the gateway is missing or doesn't support updates.
    if (!$payment_gateway) {
      return AccessResult::forbidden()
        ->addCacheableDependency($entity);
    }
    if (!$payment_gateway
      ->getPlugin() instanceof SupportsUpdatingStoredPaymentMethodsInterface) {
      return AccessResult::forbidden()
        ->addCacheableDependency($entity);
    }
  }
  $any_result = AccessResult::allowedIfHasPermissions($account, [
    "{$operation} any commerce_payment_method",
    $this->entityType
      ->getAdminPermission(),
  ], 'OR');
  if ($any_result
    ->isAllowed()) {
    return $any_result;
  }
  if ($account
    ->id() == $entity
    ->getOwnerId()) {
    $own_result = AccessResult::allowedIfHasPermission($account, 'manage own commerce_payment_method')
      ->addCacheableDependency($entity);
  }
  else {
    $own_result = AccessResult::neutral()
      ->cachePerPermissions();
  }
  return $own_result
    ->cachePerUser();
}