devel_wizard-2.x-dev/templates/spell/entity_type/content/access_control_handler.php.twig
templates/spell/entity_type/content/access_control_handler.php.twig
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': content.namespace,
}
%}
{%-
include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
with {
'useStatements': {
'Drupal\\Core\\Access\\AccessResult': '',
'Drupal\\Core\\Access\\AccessResultInterface': '',
'Drupal\\Core\\Entity\\EntityAccessControlHandler': '',
'Drupal\\Core\\Entity\\EntityHandlerInterface': '',
'Drupal\\Core\\Entity\\EntityInterface': '',
'Drupal\\Core\\Entity\\EntityTypeInterface': '',
'Drupal\\Core\\Session\\AccountInterface': '',
'Symfony\\Component\\DependencyInjection\\ContainerInterface': '',
},
}
%}
class AccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
/**
* {@inheritdoc}
*
* @return static
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
// @phpstan-ignore-next-line
return new static($entity_type);
}
/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $context
* @phpstan-param ?string $entity_bundle
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL): AccessResultInterface {
$return = parent::checkCreateAccess($account, $context, $entity_bundle);
if (!$return->isNeutral()) {
return $return;
}
return AccessResult::allowedIfHasPermissions(
$account,
$this->getPermissions('create', $entity_bundle),
'OR',
);
}
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$return = parent::checkAccess($entity, $operation, $account);
if (!$return->isNeutral()) {
return $return;
}
return match ($operation) {
'update',
'view',
'delete' => AccessResult::allowedIfHasPermissions(
$account,
$this->getPermissions($operation, $entity->bundle()),
'OR',
),
default => AccessResult::neutral(),
};
}
/**
* The permission IDs based on the operation and the entity type definition.
*
* @return string[]
*/
protected function getPermissions(string $operation, string $bundle = ''): array {
$provider = $this->entityType->getProvider();
$bundle = $bundle ?: '_all';
return array_unique(array_filter([
(string) $this->entityType->getAdminPermission(),
"$provider.{$this->entityTypeId}._all",
"$provider.{$this->entityTypeId}._all.$operation",
"$provider.{$this->entityTypeId}.$bundle",
"$provider.{$this->entityTypeId}.$bundle.$operation",
]));
}
}
