email_registration.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Allows users to register with an e-mail address as their username.
  5. */
  6. /**
  7. * Implements hook_user_insert().
  8. */
  9. function email_registration_user_insert(&$edit, &$account, $category = NULL) {
  10. // Don't create a new username if one is already set.
  11. if (!empty($account->name) && strpos($account->name, 'email_registration_') !== 0) {
  12. return;
  13. }
  14. // Other modules may implement hook_email_registration_name($edit, $account)
  15. // to generate a username (return a string to be used as the username, NULL
  16. // to have email_registration generate it).
  17. $names = module_invoke_all('email_registration_name', $edit, $account);
  18. // Remove any empty entries.
  19. $names = array_filter($names);
  20. if (empty($names)) {
  21. // Strip off everything after the @ sign.
  22. $new_name = preg_replace('/@.*$/', '', $edit['mail']);
  23. }
  24. else {
  25. // One would expect a single implementation of the hook, but if there
  26. // are multiples out there use the last one.
  27. $new_name = array_pop($names);
  28. }
  29. // Ensure whatever name we have is unique.
  30. $new_name = email_registration_unique_username($new_name, $account->uid);
  31. // Replace with generated username.
  32. db_update('users')
  33. ->fields(array('name' => $new_name))
  34. ->condition('uid', $account->uid)
  35. ->execute();
  36. $edit['name'] = $new_name;
  37. $account->name = $new_name;
  38. return;
  39. }
  40. /**
  41. * Given a starting point for a Drupal username (e.g. the name portion of an
  42. * email address) return a legal, unique Drupal username. This function is
  43. * designed to work on the results of the /user/register or /admin/people/create
  44. * forms which have already called user_validate_name, valid_email_address
  45. * or a similar function. If your custom code is creating users, you should
  46. * ensure that the email/name is already validated using something like that.
  47. *
  48. * @param $name
  49. * A name from which to base the final user name. May contain illegal characters; these will be stripped.
  50. *
  51. * @param $uid
  52. * (optional) Uid to ignore when searching for unique user (e.g. if we update the username after the
  53. * {users} row is inserted)
  54. *
  55. * @return
  56. * A unique user name based on $name.
  57. *
  58. * @see user_validate_name().
  59. *
  60. */
  61. function email_registration_unique_username($name, $uid = 0) {
  62. // Strip illegal characters.
  63. $name = preg_replace('/[^\x{80}-\x{F7} a-zA-Z0-9@_.\'-]/', '', $name);
  64. // Strip leading and trailing spaces.
  65. $name = trim($name);
  66. // Convert any other series of spaces to a single underscore.
  67. $name = preg_replace('/ +/', '_', $name);
  68. // If there's nothing left use a default.
  69. $name = ('' === $name) ? t('user') : $name;
  70. // Truncate to reasonable size.
  71. $name = (drupal_strlen($name) > (USERNAME_MAX_LENGTH - 10)) ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;
  72. // Iterate until we find a unique name.
  73. $i = 0;
  74. do {
  75. $new_name = empty($i) ? $name : $name . '_' . $i;
  76. $found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
  77. $i++;
  78. } while (!empty($found));
  79. return $new_name;
  80. }
  81. /**
  82. * Implements hook_form_FORM_ID_alter().
  83. */
  84. function email_registration_form_user_register_form_alter(&$form, &$form_state) {
  85. $form['account']['name']['#type'] = 'value';
  86. $form['account']['name']['#value'] = 'email_registration_' . user_password();
  87. $form['account']['mail']['#title'] = t('E-mail');
  88. }
  89. /**
  90. * Implements hook_form_FORM_ID_alter().
  91. */
  92. function email_registration_form_user_pass_alter(&$form, &$form_state) {
  93. $form['name']['#title'] = t('E-mail');
  94. $form['name']['#description'] = t('A password reset message will be sent to your e-mail address.');
  95. }
  96. /**
  97. * Implements hook_form_FORM_ID_alter().
  98. */
  99. function email_registration_form_user_login_alter(&$form, &$form_state) {
  100. $form['name']['#title'] = t('E-mail');
  101. $form['name']['#description'] = t('Enter your e-mail address.');
  102. $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
  103. $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
  104. }
  105. /**
  106. * Implements hook_form_FORM_ID_alter().
  107. */
  108. function email_registration_form_user_login_block_alter(&$form, &$form_state) {
  109. $form['name']['#title'] = t('E-mail');
  110. $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
  111. }
  112. /**
  113. * Form element validation handler for the user login form.
  114. * Allows users to authenticate by email, which is our preferred method.
  115. */
  116. function email_registration_user_login_validate($form, &$form_state) {
  117. if (isset($form_state['values']['name'])) {
  118. // Keep the email value in form state for furher validation.
  119. $form_state['values']['email'] = $form_state['values']['name'];
  120. if ($name = db_query('SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)', array(':name' => $form_state['values']['name']))->fetchField()) {
  121. $form_state['values']['name'] = $name;
  122. }
  123. }
  124. }
  125. /**
  126. * Implements hook_form_FORM_ID_alter().
  127. */
  128. function email_registration_form_user_profile_form_alter($form, &$form_state) {
  129. $form['account']['name']['#title'] = t('Display name');
  130. }