PriceListItemAccessControlHandler.php in Commerce Pricelist 8
File
src/PriceListItemAccessControlHandler.php
View source
<?php
namespace Drupal\commerce_pricelist;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler as CoreEntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class PriceListItemAccessControlHandler extends CoreEntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($account
->hasPermission($this->entityType
->getAdminPermission())) {
return AccessResult::allowed()
->cachePerPermissions();
}
$priceList = $entity
->getPriceList();
if (!$priceList) {
return AccessResult::forbidden()
->addCacheableDependency($entity);
}
if ($operation == 'view') {
$result = $priceList
->access('view', $account, TRUE);
}
else {
$bundle = $entity
->bundle();
$result = AccessResult::allowedIfHasPermission($account, "manage {$bundle} price_list_item");
}
return $result;
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$result = AccessResult::allowedIfHasPermissions($account, [
$this->entityType
->getAdminPermission(),
"manage {$entity_bundle} price_list_item",
], 'OR');
return $result;
}
}