PaymentMethodAccessControlHandler.php in Commerce Core 8.2
File
modules/payment/src/PaymentMethodAccessControlHandler.php
View source
<?php
namespace Drupal\commerce_payment;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsUpdatingStoredPaymentMethodsInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class PaymentMethodAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'update') {
$payment_gateway = $entity
->getPaymentGateway();
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();
}
public function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermissions($account, [
$this->entityType
->getAdminPermission(),
'manage own commerce_payment_method',
], 'OR');
}
}