TransactionAccessControlHandler.php in Transaction 8
File
src/TransactionAccessControlHandler.php
View source
<?php
namespace Drupal\transaction;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
class TransactionAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$result = parent::checkAccess($entity, $operation, $account);
if ($result
->isForbidden()) {
return $result;
}
if ($operation == 'view label') {
$operation = 'view';
}
if (!$entity
->isPending() && ($operation == 'execute' || $operation != 'view' && !$account
->hasPermission('administer transactions'))) {
return AccessResult::forbidden();
}
if ($target_entity = $entity
->getTargetEntity()) {
$target_result = $entity
->getTargetEntity()
->access($operation, $account, TRUE);
if ($target_result
->isForbidden()) {
return $target_result;
}
$result = $result
->andIf($target_result);
}
if (!$result
->isAllowed()) {
if ($operation == 'view label') {
$operation = 'view';
}
$type = $entity
->getTypeId();
$result = AccessResult::allowedIfHasPermission($account, "{$operation} any {$type} transaction");
if ($result
->isNeutral() && $entity
->getOwnerId() == $account
->id()) {
$result = AccessResult::allowedIfHasPermission($account, "{$operation} own {$type} transaction");
}
}
return $target_entity ? $result
->addCacheableDependency($target_entity) : $result;
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, "create {$entity_bundle} transaction");
}
}