BillingScheduleAccessControlHandler.php in Commerce Recurring Framework 8
File
src/BillingScheduleAccessControlHandler.php
View source
<?php
namespace Drupal\commerce_recurring;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\EntityAccessControlHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BillingScheduleAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $viewLabelOperation = TRUE;
protected $entityTypeManager;
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type);
$this->entityTypeManager = $entity_type_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'));
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view label') {
$permissions = [
'administer commerce_billing_schedule',
'administer commerce_subscription',
'update any commerce_subscription',
'view any commerce_subscription',
'view own commerce_subscription',
];
return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
}
elseif ($operation === 'delete') {
$is_referenced = (bool) $this->entityTypeManager
->getStorage('commerce_subscription')
->getQuery()
->accessCheck(FALSE)
->condition('billing_schedule', $entity
->id())
->count()
->execute();
if ($is_referenced) {
return AccessResult::forbidden();
}
}
return parent::checkAccess($entity, $operation, $account);
}
}