PaymentAccessControlHandler.php in Payment 8.2
File
src/Entity/Payment/PaymentAccessControlHandler.php
View source
<?php
namespace Drupal\payment\Entity\Payment;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\payment\Entity\PaymentInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodCapturePaymentInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodRefundPaymentInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodUpdatePaymentStatusInterface;
class PaymentAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $payment, $operation, AccountInterface $account) {
if ($operation == 'update_status') {
$payment_method = $payment
->getPaymentMethod();
if ($payment_method instanceof PaymentMethodUpdatePaymentStatusInterface && !$payment_method
->updatePaymentStatusAccess($account)) {
return AccessResult::forbidden();
}
}
elseif ($operation == 'capture') {
$payment_method = $payment
->getPaymentMethod();
if ($payment_method instanceof PaymentMethodCapturePaymentInterface) {
return AccessResult::allowedIf($payment_method instanceof PaymentMethodCapturePaymentInterface)
->andIf(AccessResult::allowedIf($payment_method
->capturePaymentAccess($account)))
->andIf($this
->checkAccessPermission($payment, $operation, $account));
}
return AccessResult::forbidden();
}
elseif ($operation == 'refund') {
$payment_method = $payment
->getPaymentMethod();
if ($payment_method instanceof PaymentMethodRefundPaymentInterface) {
return AccessResult::allowedIf($payment_method
->refundPaymentAccess($account))
->andIf($this
->checkAccessPermission($payment, $operation, $account));
}
return AccessResult::forbidden();
}
elseif ($operation == 'complete') {
if ($payment
->getPaymentMethod()) {
return AccessResult::allowedIf($payment
->getOwnerId() == $account
->id())
->orIf(AccessResult::forbiddenIf($payment
->getPaymentMethod()
->getPaymentExecutionResult()
->isCompleted()));
}
else {
return AccessResult::forbidden();
}
}
return $this
->checkAccessPermission($payment, $operation, $account);
}
protected function checkAccessPermission(PaymentInterface $payment, $operation, AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'payment.payment.' . $operation . '.any')
->orIf(AccessResult::allowedIfHasPermission($account, 'payment.payment.' . $operation . '.own')
->andIf(AccessResult::allowedIf($account
->id() == $payment
->getOwnerId())
->addCacheableDependency($payment)));
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowed();
}
protected function getCache($cid, $operation, $langcode, AccountInterface $account) {
}
}