update_replace_permissions

update_replace_permissions($replace)

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 671
Drupal database update API.

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') ? : array();
    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();
  }
}
doc_Drupal
2016-10-29 09:51:26
Comments
Leave a Comment

Please login to continue.