You are here

function social_auth_post_update_encrypt_tokens in Social Auth 3.x

Same name and namespace in other branches
  1. 8.2 social_auth.post_update.php \social_auth_post_update_encrypt_tokens()

Encrypts all tokens currently stored by Social Auth.

File

./social_auth.post_update.php, line 11
Method hook_post_update_NAME.

Code

function social_auth_post_update_encrypt_tokens(&$sandbox = NULL) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('social_auth');

  // Initializes some variables during the first pass through.
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = $storage
      ->getQuery()
      ->count()
      ->execute();
    $sandbox['progress'] = 0;
  }
  $ids = $storage
    ->getQuery()
    ->range($sandbox['progress'], 50)
    ->execute();

  /** @var \Drupal\social_auth\Entity\SocialAuth[] $social_auth_users */
  $social_auth_users = $storage
    ->loadMultiple($ids);
  foreach ($social_auth_users as $user) {
    $token = $user
      ->get('token')->value;

    // Sets token take care of the encryption.
    $user
      ->setToken($token)
      ->save();
    $sandbox['progress']++;
  }
  $sandbox['#finished'] = $sandbox['total'] == $sandbox['progress'] ? 1 : $sandbox['progress'] / $sandbox['total'];

  // Once finished.
  if ($sandbox['#finished']) {
    return t('Updated %n out of %t Social Auth users', [
      '%n' => $sandbox['progress'],
      '%t' => $sandbox['total'],
    ]);
  }
}