Skip to content

Instantly share code, notes, and snippets.

@isalmanhaider
Last active February 13, 2024 19:04
Show Gist options
  • Save isalmanhaider/fab3e35ab185e17e3117a762a3a85611 to your computer and use it in GitHub Desktop.
Save isalmanhaider/fab3e35ab185e17e3117a762a3a85611 to your computer and use it in GitHub Desktop.
clean_permissions.php
<?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();
}
}
@isalmanhaider
Copy link
Author

isalmanhaider commented Feb 13, 2024

drush scr clean_permissions.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment