EntityPermissionsForm.php in Drupal 10
File
core/modules/user/src/Form/EntityPermissionsForm.php
View source
<?php
namespace Drupal\user\Form;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\PermissionHandlerInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class EntityPermissionsForm extends UserPermissionsForm {
protected $configManager;
protected $entityTypeManager;
protected $bundle;
public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($permission_handler, $role_storage, $module_handler);
$this->configManager = $config_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.permissions'), $container
->get('entity_type.manager')
->getStorage('user_role'), $container
->get('module_handler'), $container
->get('config.manager'), $container
->get('entity_type.manager'));
}
protected function permissionsByProvider() : array {
$config_name = $this->bundle
->getConfigDependencyName();
$config_entities = $this->configManager
->getConfigEntitiesToChangeOnDependencyRemoval('config', [
$config_name,
]);
$config_names = array_map(function ($dependent_config) {
return $dependent_config
->getConfigDependencyName();
}, $config_entities['delete'] ?? []);
$config_names[] = $config_name;
$permissions = $this->permissionHandler
->getPermissions();
$permissions_by_provider = [];
foreach ($permissions as $permission_name => $permission) {
$required_configs = $permission['dependencies']['config'] ?? [];
if (array_intersect($required_configs, $config_names)) {
$provider = $permission['provider'];
$permissions_by_provider[$provider][$permission_name] = $permission;
}
}
return $permissions_by_provider;
}
public function buildForm(array $form, FormStateInterface $form_state, string $bundle_entity_type = NULL, $bundle = NULL) : array {
if ($bundle instanceof EntityInterface) {
$this->bundle = $bundle;
return parent::buildForm($form, $form_state);
}
$this->bundle = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($bundle);
return parent::buildForm($form, $form_state);
}
public function access(Route $route, RouteMatchInterface $route_match, $bundle = NULL) : AccessResultInterface {
if ($bundle instanceof EntityInterface) {
$this->bundle = $bundle;
}
else {
$bundle_entity_type = $route
->getDefault('bundle_entity_type');
$bundle_name = is_string($bundle) ? $bundle : $route_match
->getRawParameter($bundle_entity_type);
$this->bundle = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($bundle_name);
}
if (empty($this->bundle)) {
return AccessResult::forbidden();
}
return AccessResult::allowedIf((bool) $this
->permissionsByProvider());
}
}