You are here

function uuid_user_features_rebuild in UUID Features Integration 7

Implements hook_features_rebuild().

Rebuilds users based on UUID from code defaults.

1 call to uuid_user_features_rebuild()
uuid_user_features_revert in includes/uuid_user.features.inc
Implements hook_features_revert().

File

includes/uuid_user.features.inc, line 119
Features hooks for the user features component.

Code

function uuid_user_features_rebuild($module) {
  $users = features_get_default('uuid_user', $module);
  uuid_node_features_rebuild($module);
  cache_clear_all();
  if (!empty($users)) {
    module_load_include('inc', 'user', 'user.pages');
    $entity_type = 'user';
    foreach ($users as $data) {
      $uid = NULL;
      $account = (object) $data;
      $existing = new stdClass();

      // Find the matching uid.
      $efq = new EntityFieldQuery();
      $efq
        ->entityCondition('entity_type', 'user');
      $efq
        ->propertyCondition('name', $account->name);
      $result = $efq
        ->execute();
      if (isset($result['user'])) {
        $uids = array_keys($result['user']);
        $uid = reset($uids);
      }
      if (!empty($uid)) {
        if ($uid == 1) {
          global $user;
          if (!empty($user->uid) && $user->uid == 1) {
            $existing = $user;
          }
          else {
            $existing = user_load(1);
          }
        }
        else {

          // Find the matching user by name with a fresh cache.
          $existing = user_load($uid, TRUE);
          $existing->uid = $uid;
        }
        $account->uid = $uid;
      }
      else {
        $existing->uid = NULL;
      }
      drupal_alter('uuid_entity_features_rebuild', $entity_type, $account, $data, $module);
      drupal_alter('uuid_user_features_rebuild', $account, $module);
      if ($existing->uid === NULL) {

        // Ensure a new user gets a new password.
        $account->pass = user_password(16);
        $account = user_save(NULL, (array) $account);
      }
      else {

        // Ensure we don't reset the password.
        unset($account->pass);
        $account = user_save($existing, (array) $account);
      }

      // Clear out previously loaded user account if one was found.
      unset($existing);
    }
    module_invoke_all('uuid_entity_features_rebuild_complete', $entity_type, $users, $module);
  }
  uuid_node_features_rebuild($module);
}