class EntityPermissionsForm

Same name and namespace in other branches
  1. 10 core/modules/user/src/Form/EntityPermissionsForm.php \Drupal\user\Form\EntityPermissionsForm
  2. 11.x core/modules/user/src/Form/EntityPermissionsForm.php \Drupal\user\Form\EntityPermissionsForm

Provides the permissions administration form for a bundle.

This class handles bundles that are defined by configuration objects.

@internal

Hierarchy

Expanded class hierarchy of EntityPermissionsForm

1 file declares its use of EntityPermissionsForm
EntityPermissionsFormTest.php in core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php

File

core/modules/user/src/Form/EntityPermissionsForm.php, line 25

Namespace

Drupal\user\Form
View source
class EntityPermissionsForm extends UserPermissionsForm {
    
    /**
     * The configuration entity manager.
     *
     * @var \Drupal\Core\Config\ConfigManagerInterface
     */
    protected $configManager;
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The bundle object.
     *
     * @var \Drupal\Core\Entity\EntityInterface
     */
    protected $bundle;
    
    /**
     * Constructs a new EntityPermissionsForm.
     *
     * @param \Drupal\user\PermissionHandlerInterface $permission_handler
     *   The permission handler.
     * @param \Drupal\user\RoleStorageInterface $role_storage
     *   The role storage.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler.
     * @param Drupal\Core\Config\ConfigManagerInterface $config_manager
     *   The configuration entity manager.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    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'));
    }
    
    /**
     * {@inheritdoc}
     */
    protected function permissionsByProvider() : array {
        // Get the names of all config entities that depend on $this->bundle.
        $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;
        // Find all the permissions that depend on $this->bundle.
        $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;
    }
    
    /**
     * Builds the user permissions administration form for a bundle.
     *
     * @param array $form
     *   An associative array containing the structure of the form.
     * @param \Drupal\Core\Form\FormStateInterface $form_state
     *   The current state of the form.
     * @param string $bundle_entity_type
     *   (optional) The entity type ID.
     * @param string|Drupal\Core\Entity\EntityInterface $bundle
     *   (optional) Either the bundle name or the bundle object.
     */
    public function buildForm(array $form, FormStateInterface $form_state, string $bundle_entity_type = NULL, $bundle = NULL) : array {
        // Set $this->bundle for use by ::permissionsByProvider().
        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);
    }
    
    /**
     * Checks that there are permissions to be managed.
     *
     * @param \Symfony\Component\Routing\Route $route
     *   The route to check against.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The parametrized route.
     * @param string|EntityInterface $bundle
     *   (optional) The bundle. Different entity types can have different names
     *   for their bundle key, so if not specified on the route via a {bundle}
     *   parameter, the access checker determines the appropriate key name, and
     *   gets the value from the corresponding request attribute. For example,
     *   for nodes, the bundle key is "node_type", so the value would be
     *   available via the {node_type} parameter rather than a {bundle}
     *   parameter.
     *
     * @return \Drupal\Core\Access\AccessResultInterface
     *   The access result.
     */
    public function access(Route $route, RouteMatchInterface $route_match, $bundle = NULL) : AccessResultInterface {
        // Set $this->bundle for use by ::permissionsByProvider().
        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)) {
            // A typo in the request path can lead to this case.
            return AccessResult::forbidden();
        }
        return AccessResult::allowedIf((bool) $this->permissionsByProvider());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityPermissionsForm::$bundle protected property The bundle object.
EntityPermissionsForm::$configManager protected property The configuration entity manager.
EntityPermissionsForm::$entityTypeManager protected property The entity type manager service.
EntityPermissionsForm::access public function Checks that there are permissions to be managed.
EntityPermissionsForm::buildForm public function Builds the user permissions administration form for a bundle. Overrides UserPermissionsForm::buildForm
EntityPermissionsForm::create public static function Instantiates a new instance of this class. Overrides UserPermissionsForm::create
EntityPermissionsForm::permissionsByProvider protected function Group permissions by the modules that provide them. Overrides UserPermissionsForm::permissionsByProvider
EntityPermissionsForm::__construct public function Constructs a new EntityPermissionsForm. Overrides UserPermissionsForm::__construct
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 73
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserPermissionsForm::$moduleHandler protected property The module handler.
UserPermissionsForm::$permissionHandler protected property The permission handler.
UserPermissionsForm::$roleStorage protected property The role storage.
UserPermissionsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
UserPermissionsForm::getRoles protected function Gets the roles to display in this form. 1
UserPermissionsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.