You are here

function role_export_user_role_update in Role Export 7

Implements hook_user_role_update().

File

./role_export.module, line 139
Role Export's primary module file.

Code

function role_export_user_role_update($role) {

  // Do not alter the default anonymous and authenticated user roles.
  if ($role->rid <= 2) {
    return;
  }
  if (empty($role->machine_name)) {
    $role->machine_name = role_export_machine_name_gen($role->name);
  }

  // If the machine name has changed we need to update the role id to be
  // consistent.
  $id = role_export_generate_id($role->machine_name);
  if ($role->rid != $id) {

    // Update the numeric id and the machine name in the role table.
    db_update('role')
      ->fields(array(
      'rid' => $id,
      'machine_name' => $role->machine_name,
    ))
      ->condition('rid', $role->rid)
      ->execute();

    // Update references in the users_roles table.
    db_update('users_roles')
      ->fields(array(
      'rid' => $id,
    ))
      ->condition('rid', $role->rid)
      ->execute();

    // Update references in the role_pemission table.
    db_update('role_permission')
      ->fields(array(
      'rid' => $id,
    ))
      ->condition('rid', $role->rid)
      ->execute();

    // Update the user_admin_role variable if necessary.
    if ($role->rid == variable_get('user_admin_role')) {
      variable_set('user_admin_role', $id);
    }

    // Set the new role id in the role object, so that it is available for
    // other modules.
    $role->rid = $id;
    drupal_set_message(t('The role id for @machine_name has been changed. References for permissions and users have been updated. Make sure you update all other configurations that rely on this role (e.g. blocks, views, rules etc.) yourself.', array(
      '@machine_name' => $role->machine_name,
    )), 'warning');
  }
}