Commit 31d6a9c8 authored by Kristiaan Van den Eynde's avatar Kristiaan Van den Eynde
Browse files

Issue #3268136 by kristiaanvandeneynde: Harden group relation manager

parent 0ebdc6a6
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -252,8 +252,6 @@ function group_query_entity_query_alter(SelectInterface $query) {
    // groups will have no query access added for nested groups whatsoever. This
    // is by design as those plugins should opt to use the permission calculator
    // system, as it's way faster and automatically included in GroupQueryAlter.
    //
    // @todo Enforce that plugins may not define entity_access for "group".
    switch ($entity_type_id) {
      case 'group':
        $class_name = GroupQueryAlter::class;
+15 −2
Original line number Diff line number Diff line
@@ -19,8 +19,6 @@ use Symfony\Component\DependencyInjection\ContainerAwareTrait;
 * Each entity type definition array is set in the entity type's annotation and
 * altered by hook_group_relation_type_alter().
 *
 * @todo Prevent target entity type of group_content?
 *
 * @see \Drupal\group\Annotation\GroupRelationType
 * @see \Drupal\group\Plugin\Group\Relation\GroupRelationInterface
 * @see \Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface
@@ -122,6 +120,21 @@ class GroupRelationTypeManager extends DefaultPluginManager implements GroupRela
    $this->groupTypePluginMapCacheKey = $this->cacheKey . '_GT_map';
  }

  /**
   * {@inheritdoc}
   */
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);

    assert($definition instanceof GroupRelationTypeInterface);
    if ($definition->getEntityTypeId() === 'group_content') {
      throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" plugin tries to group group_content entities, which is simply not possible.', $plugin_id));
    }
    elseif ($definition->definesEntityAccess() && $definition->getEntityTypeId() === 'group') {
      throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" plugin defines entity access over group entities. This should be dealt with by altering the group permissions of the current user.', $plugin_id));
    }
  }

  /**
   * {@inheritdoc}
   */
+46 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ class GroupRelationTypeManagerTest extends UnitTestCase {
      });
    $this->discovery->getDefinitions()->willReturn($definitions);

    foreach ($definitions as $plugin_id => $definition) {
    foreach ($definitions as $definition) {
      foreach ($handlers as $handler_name => $class_name) {
        $service_name = "group.relation_handler.$handler_name.{$definition->id()}";
        if ($class_name === FALSE) {
@@ -137,6 +137,51 @@ class GroupRelationTypeManagerTest extends UnitTestCase {
    }
  }

  /**
   * Tests that you may not define an access plugin for group entities.
   *
   * @covers ::processDefinition
   */
  public function testPluginForGroupException() {
    $this->setUpPluginDefinitions(
      ['some_plugin' => (new GroupRelationType([
        'id' => 'some_plugin',
        'entity_type_id' => 'group',
      ]))->setClass(GroupRelationTypeInterface::class)]
    );
    $this->groupRelationTypeManager->getDefinitions();
    $this->groupRelationTypeManager->clearCachedDefinitions();

    $this->setUpPluginDefinitions(
      ['some_plugin' => (new GroupRelationType([
        'id' => 'some_plugin',
        'entity_access' => TRUE,
        'entity_type_id' => 'group',
      ]))->setClass(GroupRelationTypeInterface::class)]
    );
    $this->expectException(InvalidPluginDefinitionException::class);
    $this->expectExceptionMessage('The "some_plugin" plugin defines entity access over group entities. This should be dealt with by altering the group permissions of the current user.');
    $this->groupRelationTypeManager->getDefinitions();
  }

  /**
   * Tests that you may not define a plugin for group_content entities.
   *
   * @covers ::processDefinition
   */
  public function testPluginForGroupContentException() {
    $this->setUpPluginDefinitions(
      ['some_plugin' => (new GroupRelationType([
        'id' => 'some_plugin',
        'entity_type_id' => 'group_content',
      ]))->setClass(GroupRelationTypeInterface::class)]
    );

    $this->expectException(InvalidPluginDefinitionException::class);
    $this->expectExceptionMessage('The "some_plugin" plugin tries to group group_content entities, which is simply not possible.');
    $this->groupRelationTypeManager->getDefinitions();
  }

  /**
   * Tests the createHandlerInstance() method.
   *