email_registration.module 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // Clean up the username.
  24. $new_name = email_registration_cleanup_username($new_name, $account->uid);
  25. }
  26. else {
  27. // One would expect a single implementation of the hook, but if there
  28. // are multiples out there use the last one.
  29. $new_name = array_pop($names);
  30. }
  31. // Ensure whatever name we have is unique.
  32. $new_name = email_registration_unique_username($new_name, $account->uid);
  33. // Replace with generated username.
  34. db_update('users')
  35. ->fields(array('name' => $new_name))
  36. ->condition('uid', $account->uid)
  37. ->execute();
  38. $edit['name'] = $new_name;
  39. $account->name = $new_name;
  40. }
  41. /**
  42. * Given a starting point returns a legal, unique Drupal username.
  43. *
  44. * This function is designed to work on the results of the /user/register or
  45. * /admin/people/create forms which have already called user_validate_name,
  46. * valid_email_address or a similar function. If your custom code is creating
  47. * users, you should ensure that the email/name is already validated using
  48. * something like that.
  49. *
  50. * @param string $name
  51. * A name from which to base the final user name. May contain illegal
  52. * characters; these will be stripped.
  53. * @param int $uid
  54. * (optional) Uid to ignore when searching for unique user
  55. * (e.g. if we update the username after the {users} row is inserted)
  56. *
  57. * @return string
  58. * A unique user name based on $name.
  59. *
  60. * @see user_validate_name()
  61. */
  62. function email_registration_unique_username($name, $uid) {
  63. // Iterate until we find a unique name.
  64. $i = 0;
  65. do {
  66. $new_name = empty($i) ? $name : $name . '_' . $i;
  67. $found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
  68. $i++;
  69. } while (!empty($found));
  70. return $new_name;
  71. }
  72. /**
  73. * Function to clean up username.
  74. *
  75. * e.g.
  76. * Replace two or more spaces with a single underscore
  77. * Strip illegal characters.
  78. *
  79. * @param string $name
  80. * The username to be cleaned up.
  81. *
  82. * @return string
  83. * Cleaned up username.
  84. */
  85. function email_registration_cleanup_username($name, $uid = NULL) {
  86. // Strip illegal characters.
  87. $name = preg_replace('/[^\x{80}-\x{F7} a-zA-Z0-9@_.\'-]/', '', $name);
  88. // Strip leading and trailing spaces.
  89. $name = trim($name);
  90. // Convert any other series of spaces to a single underscore.
  91. $name = preg_replace('/ +/', '_', $name);
  92. // If there's nothing left use a default.
  93. $name = ('' === $name) ? t('user') : $name;
  94. if (!empty($uid)) {
  95. // Put uid on the end of the name.
  96. $name = $name . '_' . $uid;
  97. }
  98. // Truncate to a reasonable size.
  99. $name = (drupal_strlen($name) > (USERNAME_MAX_LENGTH - 10)) ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;
  100. return $name;
  101. }
  102. /**
  103. * Implements hook_form_FORM_ID_alter().
  104. */
  105. function email_registration_form_user_register_form_alter(&$form, &$form_state) {
  106. $form['account']['name']['#type'] = 'hidden';
  107. $form['account']['name']['#value'] = 'email_registration_' . user_password();
  108. $form['account']['mail']['#title'] = t('E-mail');
  109. }
  110. /**
  111. * Implements hook_form_FORM_ID_alter().
  112. */
  113. function email_registration_form_user_pass_alter(&$form, &$form_state) {
  114. $form['name']['#title'] = t('E-mail');
  115. }
  116. /**
  117. * Implements hook_form_FORM_ID_alter().
  118. */
  119. function email_registration_form_user_login_alter(&$form, &$form_state) {
  120. $form['name']['#title'] = t('E-mail');
  121. $form['name']['#description'] = t('Enter your e-mail address.');
  122. $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
  123. $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
  124. }
  125. /**
  126. * Implements hook_form_FORM_ID_alter().
  127. */
  128. function email_registration_form_user_login_block_alter(&$form, &$form_state) {
  129. $form['name']['#title'] = t('E-mail');
  130. $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
  131. }
  132. /**
  133. * Form element validation handler for the user login form.
  134. *
  135. * Allows users to authenticate by email, which is our preferred method.
  136. */
  137. function email_registration_user_login_validate($form, &$form_state) {
  138. if (isset($form_state['values']['name'])) {
  139. // Keep the email value in form state for further validation.
  140. $form_state['values']['email'] = $form_state['values']['name'];
  141. if ($name = db_query('SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)', array(':name' => $form_state['values']['name']))->fetchField()) {
  142. $form_state['values']['name'] = $name;
  143. }
  144. }
  145. }
  146. /**
  147. * Implements hook_form_FORM_ID_alter().
  148. */
  149. function email_registration_form_user_profile_form_alter(&$form, &$form_state) {
  150. $form['account']['name']['#title'] = t('Display name');
  151. }