function user_update_10000

Remove non-existent permissions created by migrations.

File

core/modules/user/user.install, line 134

Code

function user_update_10000() {
    $cleaned_roles = [];
    $existing_permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());
    $config_factory = \Drupal::configFactory();
    $role_ids = $config_factory->listAll('user.role.');
    foreach ($role_ids as $role_id) {
        $role_config = $config_factory->getEditable($role_id);
        $removed_permissions = array_diff($role_config->get('permissions'), $existing_permissions);
        if (!empty($removed_permissions)) {
            $cleaned_roles[] = $role_config->get('label');
            \Drupal::logger('update')->notice('The role %role has had the following non-existent permission(s) removed: %permissions.', [
                '%role' => $role_config->get('label'),
                '%permissions' => implode(', ', $removed_permissions),
            ]);
            $permissions = array_intersect($role_config->get('permissions'), $existing_permissions);
            $role_config->set('permissions', $permissions);
            $role_config->save();
        }
    }
    if (!empty($cleaned_roles)) {
        return new PluralTranslatableMarkup(count($cleaned_roles), 'The role %role_list has had non-existent permissions removed. Check the logs for details.', 'The roles %role_list have had non-existent permissions removed. Check the logs for details.', [
            '%role_list' => implode(', ', $cleaned_roles),
        ]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.