Last active
February 13, 2024 19:04
-
-
Save isalmanhaider/fab3e35ab185e17e3117a762a3a85611 to your computer and use it in GitHub Desktop.
clean_permissions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Script to help cleanup the not existing permissions from your roles. | |
* | |
* @code | |
* drush scr clean_permissions.php | |
* drush -y cex | |
* @endcode | |
* | |
* @see https://www.drupal.org/node/3193348 | |
*/ | |
$entity_type_manager = \Drupal::entityTypeManager(); | |
$permissions = array_keys(\Drupal::service('user.permissions')->getPermissions()); | |
/** @var \Drupal\user\RoleInterface[] $roles */ | |
$roles = $entity_type_manager->getStorage('user_role')->loadMultiple(); | |
foreach ($roles as $role) { | |
$role_permissions = $role->getPermissions(); | |
$differences = array_diff($role_permissions, $permissions); | |
if ($differences) { | |
foreach ($differences as $permission) { | |
$role->revokePermission($permission); | |
} | |
$role->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
drush scr clean_permissions.php