Unverified Commit cffb02aa authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2571235 by alexpott, claudiu.cristea, dawehner, Wim Leers, gabesullice,...

Issue #2571235 by alexpott, claudiu.cristea, dawehner, Wim Leers, gabesullice, IJsbrandy, benjifisher, alisonjo315, larowlan, Berdir, effulgentsia, longwave, herom, dmouse, heddn, catch: [regression] Roles should depend on objects that are building the granted permissions
parent 4067ac09
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Entity;

/**
 * Provides a method to simplify generating bundle level permissions.
 */
trait BundlePermissionHandlerTrait {

  /**
   * Builds a permissions array for the supplied bundles.
   *
   * @param \Drupal\Core\Entity\EntityInterface[] $bundles
   *   An array of bundles to generate permissions for.
   * @param callable $permission_builder
   *   A callable to generate the permissions for a particular bundle. Returns
   *   an array of permissions. See PermissionHandlerInterface::getPermissions()
   *   for the array structure.
   *
   * @return array
   *   Permissions array. See PermissionHandlerInterface::getPermissions() for
   *   the array structure.
   *
   * @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  protected function generatePermissions(array $bundles, callable $permission_builder) {
    $permissions = [];
    foreach ($bundles as $bundle) {
      $permissions += array_map(
        function (array $perm) use ($bundle) {
          // This permission is generated on behalf of a bundle, therefore
          // add the bundle as a config dependency.
          $perm['dependencies'][$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
          return $perm;
        },
        $permission_builder($bundle)
      );
    }
    return $permissions;
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ public function testCacheabilityOf401Response() {
    // If the permissions of the 'anonymous' role change, it may no longer be
    // necessary to be authenticated to access this route. Therefore the cached
    // 401 responses should be invalidated.
    $this->grantPermissions(Role::load(Role::ANONYMOUS_ID), [$this->randomMachineName()]);
    $this->grantPermissions(Role::load(Role::ANONYMOUS_ID), ['access content']);
    $assert_response_cacheability('MISS', 'MISS');
    $assert_response_cacheability('HIT', 'MISS');
    // Idem for when the 'system.site' config changes.
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ class MigrateBlockContentTranslationTest extends MigrateDrupal6TestBase {
    'block_content',
    'config_translation',
    'language',
    'locale',
    'path_alias',
    'statistics',
    'taxonomy',
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ class MigrateBlockContentTranslationTest extends MigrateDrupal7TestBase {
    'block_content',
    'config_translation',
    'language',
    'locale',
    'path_alias',
    'statistics',
    'taxonomy',
+1 −0
Original line number Diff line number Diff line
@@ -141,6 +141,7 @@ public function testInstallProfileConfigOverwrite() {
    // Ensure the authenticated role has the access tour permission.
    $role = Role::load(Role::AUTHENTICATED_ID);
    $this->assertTrue($role->hasPermission('access tour'), 'The Authenticated role has the "access tour" permission.');
    $this->assertEquals(['module' => ['tour']], $role->getDependencies());
  }

}
Loading