Commit f5be97cb authored by alexpott's avatar alexpott
Browse files

Issue #2409129 by webflo, k4v, kgoel, claudiu.cristea, dawehner, tstoeckler:...

Issue #2409129 by webflo, k4v, kgoel, claudiu.cristea, dawehner, tstoeckler: Enforce order of permissions in config export
parent ba068310
......@@ -178,6 +178,12 @@ public function preSave(EntityStorageInterface $storage) {
});
$this->weight = $max + 1;
}
if (!$this->isSyncing()) {
// Permissions are always ordered alphabetically to avoid conflicts in the
// exported configuration.
sort($this->permissions);
}
}
}
......@@ -75,7 +75,6 @@ public function query() {
}
public function preRender(&$values) {
$uids = array();
$this->items = array();
$permission_names = \Drupal::service('user.permissions')->getPermissions();
......@@ -100,10 +99,8 @@ public function preRender(&$values) {
}
}
foreach ($uids as $uid) {
if (isset($this->items[$uid])) {
ksort($this->items[$uid]);
}
foreach ($this->items as &$permission) {
ksort($permission);
}
}
}
......
<?php
namespace Drupal\user\Tests\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
use Drupal\user\Entity\Role;
/**
* Tests user permissions sort upgrade path.
*
* @group Update
*/
class UserUpdateOrderPermissionsTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8-rc1.bare.standard.php.gz',
];
}
/**
* Tests that permissions are ordered by machine name.
*/
public function testPermissionsOrder() {
$authenticated = Role::load('authenticated');
$permissions = $authenticated->getPermissions();
sort($permissions);
$this->assertNotIdentical($permissions, $authenticated->getPermissions());
$this->runUpdates();
$authenticated = Role::load('authenticated');
$this->assertIdentical($permissions, $authenticated->getPermissions());
}
}
......@@ -38,6 +38,7 @@ protected function assertRole($id, array $permissions, $lookupId, MigrateIdMapIn
/** @var \Drupal\user\RoleInterface $role */
$role = Role::load($id);
$this->assertInstanceOf(RoleInterface::class, $role);
sort($permissions);
$this->assertSame($permissions, $role->getPermissions());
$this->assertSame([[$id]], $id_map->lookupDestinationIds(['rid' => $lookupId]));
}
......@@ -67,9 +68,9 @@ protected function assertRoles(MigrateIdMapInterface $id_map) {
// From permission table.
'access comments',
'access content',
'migrate test authenticated permission',
'post comments',
'skip comment approval',
'migrate test authenticated permission',
// From filter_format.
'use text format filtered_html',
];
......
......@@ -45,6 +45,7 @@ protected function assertEntity($id, $label, $original_rid) {
->condition('rid', $original_rid)
->execute()
->fetchCol();
sort($permissions);
$this->assertIdentical($permissions, $entity->getPermissions());
}
}
......
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
/**
* @group user
*/
class UserRoleEntityTest extends KernelTestBase {
public static $modules = ['system', 'user'];
public function testOrderOfPermissions() {
$role = Role::create(['id' => 'test_role']);
$role->grantPermission('b')
->grantPermission('a')
->grantPermission('c')
->save();
$this->assertEquals($role->getPermissions(), ['a', 'b', 'c']);
$role->revokePermission('b')->save();
$this->assertEquals($role->getPermissions(), ['a', 'c']);
$role->grantPermission('b')->save();
$this->assertEquals($role->getPermissions(), ['a', 'b', 'c']);
}
}
......@@ -37,9 +37,9 @@ public function testFieldPermission() {
$expected_permissions[$this->users[2]->id()][] = t('Administer permissions');
// View user profiles comes first, because we sort by the permission
// machine name.
$expected_permissions[$this->users[3]->id()][] = t('View user information');
$expected_permissions[$this->users[3]->id()][] = t('Administer permissions');
$expected_permissions[$this->users[3]->id()][] = t('Administer users');
$expected_permissions[$this->users[3]->id()][] = t('View user information');
foreach ($view->result as $index => $row) {
$uid = $view->field['uid']->getValue($row);
......
<?php
/**
* @file
* Post update functions for User module.
*/
use Drupal\user\Entity\Role;
/**
* @addtogroup updates-8.3.x
* @{
*/
/**
* Enforce order of role permissions.
*/
function user_post_update_enforce_order_of_permissions() {
$entity_save = function (Role $role) {
$permissions = $role->getPermissions();
sort($permissions);
if ($permissions !== $role->getPermissions()) {
$role->save();
}
};
array_map($entity_save, Role::loadMultiple());
}
/**
* @} End of "addtogroup updates-8.3.x".
*/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment