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

Removed the unsafe group_membership.roles.permissions cache context instead of deprecating it.

parent 4e7d8aa6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -436,7 +436,7 @@ function group_update_8015() {
        $cache_metadata_key = "display.$display.cache_metadata";
        $cache_metadata = $view->get($cache_metadata_key);

        // Remove the deprecated cache context and add the updated metadata.
        // Remove the deleted cache context and add the updated metadata.
        if ($key = array_search('group_membership.roles.permissions', $cache_metadata['contexts'])) {
          unset($cache_metadata['tags'][$key]);
        }
+0 −5
Original line number Diff line number Diff line
@@ -59,11 +59,6 @@ services:
    arguments: ['@current_route_match', '@current_user', '@entity_type.manager']
    tags:
      - { name: 'cache.context'}
  cache_context.group_membership.roles.permissions:
    class: 'Drupal\group\Cache\Context\GroupMembershipPermissionsCacheContext'
    arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@group.permissions_hash_generator']
    tags:
      - { name: 'cache.context'}
  cache_context.route.group:
    class: 'Drupal\group\Cache\Context\RouteGroupCacheContext'
    arguments: ['@current_route_match', '@entity_type.manager']
+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ function gnode_update_8006() {
        $cache_metadata_key = "display.$display.cache_metadata";
        $cache_metadata = $view->get($cache_metadata_key);

        // Remove the deprecated cache context and add the updated metadata.
        // Remove the deleted cache context and add the updated metadata.
        if ($key = array_search('group_membership.roles.permissions', $cache_metadata['contexts'])) {
          unset($cache_metadata['tags'][$key]);
        }
+0 −98
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Cache\Context;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Access\GroupPermissionsHashGeneratorInterface;

/**
 * Defines a cache context for "per group membership permissions" caching.
 *
 * Please note: This cache context uses the group from the current route as the
 * value object to work with. This context is therefore only to be used with
 * data that was based on the group from the route. You can retrieve it using
 * the 'entity:group' context provided by the 'group.group_route_context'
 * service. See an example at: \Drupal\group\Plugin\Block\GroupOperationsBlock.
 *
 * Cache context ID: 'group_membership.roles.permissions'.
 *
 * @deprecated in Group 1.0-rc3, will be removed before Group 1.0. Use
 *   \Drupal\group\Cache\Context\GroupPermissionsCacheContext instead.
 */
class GroupMembershipPermissionsCacheContext extends GroupMembershipCacheContextBase implements CacheContextInterface {

  /**
   * The permissions hash generator.
   *
   * @var \Drupal\group\Access\GroupPermissionsHashGeneratorInterface
   */
  protected $permissionsHashGenerator;

  /**
   * Constructs a new GroupMembershipPermissionsCacheContext class.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
   *   The current route match object.
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\group\Access\GroupPermissionsHashGeneratorInterface $hash_generator
   *   The permissions hash generator.
   */
  public function __construct(RouteMatchInterface $current_route_match, AccountInterface $user, EntityTypeManagerInterface $entity_type_manager, GroupPermissionsHashGeneratorInterface $hash_generator) {
    parent::__construct($current_route_match, $user, $entity_type_manager);
    $this->permissionsHashGenerator = $hash_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t("Group membership permissions");
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    // If there was no existing group on the route, there can be no membership.
    if (!$this->hasExistingGroup()) {
      return 'none';
    }

    return $this->permissionsHashGenerator->generate($this->group, $this->user);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    $cacheable_metadata = new CacheableMetadata();

    // If any of the membership's roles are updated, it could mean the list of
    // permissions changed as well. We therefore need to set the membership's
    // roles' cacheable metadata.
    //
    // Note that we do not set the membership's cacheable metadata because that
    // one is taken care of in the parent 'group_membership.roles' context.
    if ($this->hasExistingGroup()) {
      // Retrieve all of the group roles the user may get for the group.
      $group_roles = $this->groupRoleStorage()->loadByUserAndGroup($this->user, $this->group);

      // Merge the cacheable metadata of all the roles.
      foreach ($group_roles as $group_role) {
        $group_role_cacheable_metadata = new CacheableMetadata();
        $group_role_cacheable_metadata->createFromObject($group_role);
        $cacheable_metadata = $cacheable_metadata->merge($group_role_cacheable_metadata);
      }
    }

    return $cacheable_metadata;
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ use Drupal\Core\Cache\Context\CalculatedCacheContextInterface;
 *
 * Only use this cache context when checking explicitly for certain roles. For
 * instance when you want to show a block listing all of the member's roles. Use
 * group_membership.roles.permissions for anything that checks permissions.
 * user.group_permissions for anything that checks permissions.
 *
 * Please note: This cache context uses the group from the current route as the
 * value object to work with. This context is therefore only to be used with