function update_replace_permissions

Replace permissions during update.

This function can replace one permission to several or even delete an old one.

Parameters

array $replace: An associative array. The keys are the old permissions the values are lists of new permissions. If the list is an empty array, the old permission is removed.

File

core/includes/update.inc, line 768

Code

function update_replace_permissions($replace) {
    $prefix = 'user.role.';
    $cut = strlen($prefix);
    $role_names = \Drupal::service('config.storage')->listAll($prefix);
    foreach ($role_names as $role_name) {
        $rid = substr($role_name, $cut);
        $config = \Drupal::config("user.role.{$rid}");
        $permissions = $config->get('permissions') ?: [];
        foreach ($replace as $old_permission => $new_permissions) {
            if (($index = array_search($old_permission, $permissions)) !== FALSE) {
                unset($permissions[$index]);
                $permissions = array_unique(array_merge($permissions, $new_permissions));
            }
        }
        $config->set('permissions', $permissions)
            ->save();
    }
}

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