Guest User

Untitled

a guest
Mar 22nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. use Drupal\user\Entity\User;
  4.  
  5. /**
  6. * Rehash migrated Drupal 5 MD5 hashes for Drupal 8.
  7. */
  8. function example_update_8101(&$sandbox) {
  9.  
  10. // This update hook should only be ran if the user migration didn't process
  11. // the MD5 passwords with the 'md5_passwords' configuration option. Ideally
  12. // the user migration should handle this re-hashing.
  13.  
  14. /** @var \Drupal\Core\Password\PasswordInterface $password */
  15. $password = \Drupal::service('password');
  16. // We exclude user records already having the correct hash.
  17. $hashType = '$S$';
  18.  
  19. if (!isset($sandbox['progress'])) {
  20.  
  21. $user_count = \Drupal::entityQuery('user')
  22. ->condition('pass', $hashType . '%', 'NOT LIKE')
  23. ->count()
  24. ->execute();
  25.  
  26. $sandbox['progress'] = 0;
  27. $sandbox['limit'] = 500;
  28. $sandbox['total'] = $user_count;
  29. }
  30.  
  31. $uids = \Drupal::entityQuery('user')
  32. ->condition('pass', $hashType . '%', 'NOT LIKE')
  33. ->range($sandbox['progress'], $sandbox['progress'] + $sandbox['limit'])
  34. ->execute();
  35.  
  36. foreach ($uids as $uid) {
  37. $user = User::load($uid);
  38. // @see \Drupal\user\Plugin\migrate\destination\EntityUser::save()
  39. // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
  40. $user->pass->pre_hashed = TRUE;
  41. $user->pass->value = 'U' . $password->hash($user->pass->value);
  42. $user->save();
  43. $sandbox['progress']++;
  44. }
  45.  
  46. $sandbox['#finished'] = ($sandbox['progress'] / $sandbox['total']);
  47. }
Add Comment
Please, Sign In to add comment