CurrencyAccessControlHandler.php in Currency 8.3
File
src/Entity/Currency/CurrencyAccessControlHandler.php
View source
<?php
namespace Drupal\currency\Entity\Currency;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
public function __construct(EntityTypeInterface $entity_type, ModuleHandlerInterface $module_handler) {
parent::__construct($entity_type);
$this->moduleHandler = $module_handler;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('module_handler'));
}
protected function checkAccess(EntityInterface $currency, $operation, AccountInterface $account) {
if ($currency
->getCurrencyCode() == 'XXX' && $operation == 'delete') {
return AccessResult::forbidden();
}
if ($operation == 'enable') {
return $currency
->status() ? AccessResult::forbidden() : $this
->access($currency, 'update', $account, TRUE);
}
if ($operation == 'disable') {
return $currency
->status() ? $this
->access($currency, 'update', $account, TRUE) : AccessResult::forbidden();
}
return AccessResult::allowedIfHasPermission($account, 'currency.currency.' . $operation);
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'currency.currency.create');
}
}