Problem/Motivation

* When a helper module of mine does a $role->revokePermission('foo'),
* and i do a config export,
* i get a diff like this (note the missing '3' index)

diff --git a/config-sync/user.role.toggle_all.yml b/config-sync/user.role.toggle_all.yml
index 0ce46a0..35ac270 100644
--- a/config-sync/user.role.toggle_all.yml
+++ b/config-sync/user.role.toggle_all.yml
@@ -7,11 +7,10 @@ label: 'Toggle all'
 weight: -2
 is_admin: null
 permissions:
-  - 'abstractpermissions:role_toggle:toggle_all'
-  - 'access administration pages'
-  - 'access toolbar'
-  - 'role_toggle:administrator'
-  - 'role_toggle:debugger'
-  - 'role_toggle:design_manager'
-  - 'role_toggle:site_manager'
-  - 'role_toggle:translation_manager'
+  0: 'abstractpermissions:role_toggle:toggle_all'
+  1: 'access administration pages'
+  2: 'access toolbar'
+  4: 'role_toggle:debugger'
+  5: 'role_toggle:design_manager'
+  6: 'role_toggle:site_manager'
+  7: 'role_toggle:translation_manager'

When i then re-add it, i get:

diff --git a/config-sync/user.role.toggle_all.yml b/config-sync/user.role.toggle_all.yml
index 0ce46a0..8cb0074 100644
--- a/config-sync/user.role.toggle_all.yml
+++ b/config-sync/user.role.toggle_all.yml
@@ -10,8 +10,8 @@ permissions:
   - 'abstractpermissions:role_toggle:toggle_all'
   - 'access administration pages'
   - 'access toolbar'
-  - 'role_toggle:administrator'
   - 'role_toggle:debugger'
   - 'role_toggle:design_manager'
   - 'role_toggle:site_manager'
   - 'role_toggle:translation_manager'
+  - 'role_toggle:administrator'

Proposed resolution

In \Drupal\user\Entity\Role::preSave, add the missing array_values to the normilization.

Ex. 2 above shows that neither index normalization nor sorting work reliably.

\Drupal\user\Entity\Role::preSave:

    if (!$this->isSyncing()) {
      // Permissions are always ordered alphabetically to avoid conflicts in the
      // exported configuration.
      sort($this->permissions);
    }

Remaining tasks

Doit

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Comments

axel.rutz created an issue. See original summary.

geek-merlin’s picture

Issue summary: View changes

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

geek-merlin’s picture

Title: Permission normalization can break » Role permissions not sorted in config export

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Component: user system » configuration system
Status: Active » Closed (duplicate)
Issue tags: +Bug Smash Initiative
Related issues: +#2852557: Config export key order is not predictable, use config schema to order keys for maps

This would have been fixed in Drupal 9.3.0, #2852557: Config export key order is not predictable, use config schema to order keys for maps

Therefore, closing as duplicate. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.

Thanks!

alexpott’s picture

Status: Closed (duplicate) » Active

We need to add the sort to the schema - there's also another issue with how permissions are revoked which can cause numeric keys to be added unexpectedly.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new1000 bytes

Here's a fix using schema to sort the permissions key. This actually will also fix the problems caused by revoking a role and storing numeric keys because the sort will remove the keys.

alexpott’s picture

Version: 9.5.x-dev » 10.1.x-dev
StatusFileSize
new889 bytes
new1.84 KB

Here's a post update function to ensure existing roles are fixed. We also did this in Drupal 8 - see #2409129: Enforce order of permissions in config export - but we have new abilities with schema to ensure this is the case.

alexpott’s picture

Issue tags: +Needs tests
alexpott’s picture

We should add a test around the revoke permission case that's detailed in the issue summary.

The last submitted patch, 12: 3039499-12.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 13: 3039499-13.patch, failed testing. View results

alexpott’s picture

Priority: Normal » Critical

A duplicate was created of this - #3341431: user_update_10000 causes exported permissions to be numerically keyed - and this is now critical because it is causing unnecessary config change during an update.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new661 bytes
new2.96 KB
new1.9 KB

Fixing the update function and providing 9.5.x and 10.x patches...

alexpott’s picture

Issue tags: -Needs tests
StatusFileSize
new845 bytes
new3.79 KB
new2.72 KB

Here's a test that proves we've fixed what the issue summary outlines. I'm not sure that an explicit update test is necessary because the logic if only triggering a re-save of the role and we have implicit testing in all the existing update tests (as you can see from the fails in #13).

The test-only patch is the interdiff.

alexpott’s picture

catch credited acbramley.

catch’s picture

Adding credit from the duplicate. Looks RTBC assuming the test only patch fails.

Agreed the update function itself doesn't need explicit test coverage given it's not doing much.

alexpott’s picture

Adding a note to explain why this fixes #3341431: user_update_10000 causes exported permissions to be numerically keyed. Currently in HEAD sorting occurs on the entity level - in Role::preSave(). This patch moves it to the config schema level. Therefore permissions are sorted regardless of whether you interact with the Role configuration entity or with the underlying raw configuration. user_update_10000 is changing the raw configuration so in HEAD was bypassing the sorting but once this is moved to schema it no longer can.

The last submitted patch, 19: 3039499-9.5.x-19.patch, failed testing. View results

The last submitted patch, 20: 3039499-20.test-only.patch, failed testing. View results

catch’s picture

Status: Needs review » Reviewed & tested by the community

Test failure looks good, moving to RTBC.

  • larowlan committed 90d534fc on 10.0.x
    Issue #3039499 by alexpott, acbramley: Role permissions not sorted in...

  • larowlan committed 6b5efdc1 on 10.1.x
    Issue #3039499 by alexpott, acbramley: Role permissions not sorted in...

  • larowlan committed 255b4529 on 9.5.x
    Issue #3039499 by alexpott, acbramley: Role permissions not sorted in...
larowlan’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed to 10.1.x and backported to 10.0.x

Committed the 9.5.x patch to 9.5.x

Was going to ask about update path tests, but saw catch was ok with it as is, so didn't flag it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.