You are here

function hybridauth_update_7004 in HybridAuth Social Login 7.2

Remove users passwords stored in plaintext from users DB table.

File

./hybridauth.install, line 246
Install, update and uninstall functions for the HybridAuth module.

Code

function hybridauth_update_7004(&$sandbox) {

  // Calculate the total number of users.
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = db_select('users', 'u')
      ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    if (!$sandbox['total']) {
      return t('There are no user accounts to process.');
    }
    $sandbox['current'] = 0;
  }

  // Process this number of users per pass.
  $users_per_pass = 10;

  // Get users' ids for one pass.
  $result = db_select('users', 'u')
    ->fields('u', array(
    'uid',
  ))
    ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
    ->orderBy('u.uid')
    ->range($sandbox['current'], $users_per_pass)
    ->execute();

  // Remove user password from $user['data']['hybridauth'].
  foreach ($result as $row) {

    // Skip the anonymous user uid == 0.
    if (!empty($row->uid)) {
      $account = user_load($row->uid);
      if (isset($account->data['hybridauth']['pass'])) {
        unset($account->data['hybridauth']['pass']);
        user_save($account);
      }
    }
    $sandbox['current']++;
  }
  $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  if ($sandbox['#finished'] === 1) {
    return t('@total user accounts were processed.', array(
      '@total' => $sandbox['total'],
    ));
  }
}