You are here

function ga_login_update_8003 in Google Authenticator login 8

Update plugin names from tfa prefix to ga_login in users_data.

File

./ga_login.install, line 99
Installation related functions for GA Login module.

Code

function ga_login_update_8003(&$sandbox) {

  /** @var \Drupal\user\UserData $user_data */
  $user_data = \Drupal::service('user.data');
  $tfa_settings = $user_data
    ->get('tfa', NULL, 'tfa_user_settings');

  // Setup batch.
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = count($tfa_settings);
    $sandbox['current'] = 0;
  }
  $batch_size = 50;
  foreach (array_slice($tfa_settings, $sandbox['current'], $batch_size, TRUE) as $uid => $user_settings) {
    $changed = FALSE;
    $plugins = $user_settings['data']['plugins'];
    $new_plugins = [];
    foreach ($plugins as $key => $plugin) {
      switch ($key) {
        case 'tfa_hotp':
        case 'tfa_totp':
          $new_key = str_replace('tfa_', 'ga_login_', $key);
          $new_plugins[$new_key] = $new_key;
          $changed = TRUE;
          break;
        default:
          $new_plugins[$key] = $key;
          break;
      }
    }
    if ($changed) {
      $user_settings['data']['plugins'] = $new_plugins;
      $user_data
        ->set('tfa', $uid, 'tfa_user_settings', $user_settings);
    }
    $sandbox['current']++;
  }
  if ($sandbox['total'] == 0) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  }
}