Created
July 26, 2024 13:21
-
-
Save isalmanhaider/6a897c9f6d7b232e195ef1344afbc5b3 to your computer and use it in GitHub Desktop.
clean permissions
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