user_registrationpassword.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @file
  4. * Enables password creation on registration form.
  5. */
  6. /**
  7. * No verification email is sent.
  8. */
  9. define('USER_REGISTRATIONPASS_NO_VERIFICATION', 0);
  10. /**
  11. * Verification email is sent before password is set.
  12. */
  13. define('USER_REGISTRATIONPASS_VERIFICATION_DEFAULT', 1);
  14. /**
  15. * Verification email is sent after password is set.
  16. */
  17. define('USER_REGISTRATIONPASS_VERIFICATION_PASS', 2);
  18. /**
  19. * Implements hook_menu().
  20. */
  21. function user_registrationpassword_menu() {
  22. $items['user/registrationpassword/%/%/%'] = array(
  23. 'title' => 'Confirm account',
  24. 'page callback' => 'drupal_get_form',
  25. 'page arguments' => array('user_registrationpassword_confirm_account', 2, 3, 4),
  26. 'access callback' => TRUE,
  27. 'type' => MENU_CALLBACK,
  28. 'file' => 'user_registrationpassword.pages.inc',
  29. );
  30. return $items;
  31. }
  32. /**
  33. * Implements hook_form_FORM_ID_alter() for the user administration form.
  34. */
  35. function user_registrationpassword_form_user_admin_settings_alter(&$form, &$form_state) {
  36. $form['registration_cancellation']['user_registrationpassword_registration'] = array(
  37. '#type' => 'radios',
  38. '#title' => t('Require e-mail verification when a visitor creates an account'),
  39. '#description' => t('Choose whether new users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password, or if they can set their password directly on the registration form. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.'),
  40. '#weight' => 0,
  41. '#options' => array(
  42. USER_REGISTRATIONPASS_NO_VERIFICATION => t('Do not require a verification e-mail, and let users set their password on the registration form.'),
  43. USER_REGISTRATIONPASS_VERIFICATION_DEFAULT => t('Require a verification e-mail, but wait for the approval e-mail to let users set their password.'),
  44. USER_REGISTRATIONPASS_VERIFICATION_PASS => t('Require a verification e-mail, but let users set their password directly on the registration form.'),
  45. ),
  46. '#default_value' => variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASS_VERIFICATION_PASS),
  47. );
  48. $form['registration_cancellation']['user_email_verification']['#access'] = FALSE;
  49. $email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
  50. $form['email_user_registrationpassword'] = array(
  51. '#type' => 'fieldset',
  52. '#title' => t('Welcome (no approval required, password is set)'),
  53. '#collapsible' => TRUE,
  54. '#collapsed' => TRUE,
  55. '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required and password has already been set.') . ' ' . $email_token_help,
  56. '#group' => 'email',
  57. );
  58. $form['email_user_registrationpassword']['user_registrationpassword_register_subject'] = array(
  59. '#type' => 'textfield',
  60. '#title' => t('Subject'),
  61. '#default_value' => _user_registrationpassword_mail_text('register_subject', NULL, array(), FALSE),
  62. '#maxlength' => 180,
  63. );
  64. $form['email_user_registrationpassword']['user_registrationpassword_register_body'] = array(
  65. '#type' => 'textarea',
  66. '#title' => t('Body'),
  67. '#default_value' => _user_registrationpassword_mail_text('register_body', NULL, array(), FALSE),
  68. '#rows' => 15,
  69. );
  70. // Overwrite the default activation e-mail template with our own.
  71. $form['email_activated']['settings']['user_mail_status_activated_subject'] = array(
  72. '#type' => 'textfield',
  73. '#title' => t('Subject'),
  74. '#default_value' => variable_get('user_mail_status_activated_subject', _user_registrationpassword_mail_text('status_activated_subject', NULL, array(), FALSE)),
  75. '#maxlength' => 180,
  76. );
  77. $form['email_activated']['settings']['user_mail_status_activated_body'] = array(
  78. '#type' => 'textarea',
  79. '#title' => t('Body'),
  80. '#default_value' => variable_get('user_mail_status_activated_body', _user_registrationpassword_mail_text('status_activated_body', NULL, array(), FALSE)),
  81. '#rows' => 15,
  82. );
  83. $form['#submit'][] = 'user_registrationpassword_admin_settings_submit';
  84. }
  85. /**
  86. * Submit handler for the user admin form.
  87. */
  88. function user_registrationpassword_admin_settings_submit(&$form, &$form_state) {
  89. $value = $form_state['values']['user_registrationpassword_registration'];
  90. if ($value == USER_REGISTRATIONPASS_VERIFICATION_PASS) {
  91. variable_set('user_email_verification', 0);
  92. // Prevent standard notification email to administrators and to user.
  93. variable_set('user_mail_register_pending_approval_notify', 0);
  94. }
  95. else {
  96. variable_set('user_email_verification', (int) $value);
  97. // Let default value.
  98. // @see _user_mail_notify()
  99. variable_del('user_mail_register_pending_approval_notify');
  100. }
  101. }
  102. /**
  103. * Implements hook_form_FORM_ID_alter() for the user registration form.
  104. */
  105. function user_registrationpassword_form_user_register_form_alter(&$form, &$form_state) {
  106. // Prevent this from being run if approval with password on registration
  107. // form is set and the user is an anonymous user registering to the site.
  108. // When admin users create a user, this does not need to be executed.
  109. // And when this also does not need to be executed 'user_register' is not set as
  110. // 'Visitors can create accounts and no administrator approval is required.'
  111. // (user registers, recieves user_registrationpass email, would not make sense.)
  112. // (cause that will unblock the user Without the admin 'approving'.)
  113. global $user;
  114. if (variable_get('user_register', 0) == 1 && variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASS_VERIFICATION_PASS) == USER_REGISTRATIONPASS_VERIFICATION_PASS && $user->uid < 1) {
  115. $form['account']['status']['#default_value'] = 0;
  116. $form['account']['notify']['#default_value'] = 0;
  117. $form['#submit'][] = 'user_registrationpassword_form_user_register_submit';
  118. }
  119. }
  120. /**
  121. * Submit handler for the user registration form.
  122. */
  123. function user_registrationpassword_form_user_register_submit(&$form, &$form_state) {
  124. // Remove the message sent by default.
  125. $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.');
  126. $position = array_search($message, $_SESSION['messages']['status']);
  127. if ($position !== FALSE) {
  128. unset($_SESSION['messages']['status'][$position]);
  129. }
  130. $_SESSION['messages']['status'] = array_values($_SESSION['messages']['status']);
  131. // Notify the user.
  132. $account = $form_state['user'];
  133. $params['account'] = $account;
  134. $mail = drupal_mail('user_registrationpassword', 'register', $account->mail, user_preferred_language($account), $params);
  135. drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
  136. }
  137. /**
  138. * Implements hook_mail().
  139. */
  140. function user_registrationpassword_mail($key, &$message, $params) {
  141. $language = $message['language'];
  142. $variables = array('user' => $params['account']);
  143. $message['subject'] .= _user_registrationpassword_mail_text($key . '_subject', $language, $variables);
  144. $message['body'][] = _user_registrationpassword_mail_text($key . '_body', $language, $variables);
  145. }
  146. /**
  147. * Returns a mail string for a variable name.
  148. *
  149. * Used by user_registrationpassword_mail() and the settings forms to retrieve strings.
  150. */
  151. function _user_registrationpassword_mail_text($key, $language = NULL, $variables = array(), $replace = TRUE) {
  152. $langcode = isset($language) ? $language->language : NULL;
  153. if ($admin_setting = variable_get('user_registrationpassword_' . $key, FALSE)) {
  154. // An admin setting overrides the default string.
  155. $text = $admin_setting;
  156. }
  157. else {
  158. // No override, return default string.
  159. switch ($key) {
  160. case 'register_subject':
  161. $text = t('Account details for [user:name] at [site:name]', array(), array('langcode' => $langcode));
  162. break;
  163. case 'register_body':
  164. $text = t("[user:name],
  165. Thank you for registering at [site:name]. You may now log in and verify your account by clicking this link or copying and pasting it to your browser:
  166. [user:registrationpassword-url]
  167. This link can only be used once. You will be able to log in at [site:login-url] in the future using:
  168. username: [user:name]
  169. password: Your password
  170. -- [site:name] team", array(), array('langcode' => $langcode));
  171. break;
  172. case 'status_activated_subject':
  173. $text = t('Account details for !username at !site', array(), array('langcode' => $langcode));
  174. break;
  175. case 'status_activated_body':
  176. $text = t("!username,
  177. Your account at !site has been activated.
  178. You will be able to log in to !login_uri in the future using:
  179. username: !username
  180. password: your password.
  181. -- !site team", array(), array('langcode' => $langcode));
  182. break;
  183. }
  184. }
  185. if ($replace) {
  186. // We do not sanitize the token replacement, since the output of this
  187. // replacement is intended for an e-mail message, not a web browser.
  188. return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_registrationpassword_mail_tokens', 'sanitize' => FALSE));
  189. }
  190. return $text;
  191. }
  192. /**
  193. * Token callback to add unsafe tokens for user mails.
  194. *
  195. * @see user_mail_tokens()
  196. * @see user_registrationpassword_mail()
  197. */
  198. function user_registrationpassword_mail_tokens(&$replacements, $data, $options) {
  199. user_mail_tokens($replacements, $data, $options);
  200. if (isset($data['user'])) {
  201. $replacements['[user:registrationpassword-url]'] = user_registrationpassword_confirmation_url($data['user']);
  202. }
  203. }
  204. /**
  205. * Generates a unique URL for a user to login with their password already set.
  206. *
  207. * @param object $account
  208. * An object containing the user account.
  209. *
  210. * @return
  211. * A unique URL that provides a one-time log in for the user, from which
  212. * they can change their password.
  213. */
  214. function user_registrationpassword_confirmation_url($account) {
  215. $timestamp = REQUEST_TIME;
  216. return url("user/registrationpassword/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), array('absolute' => TRUE));
  217. }
  218. /**
  219. * Implements hook_mailkeys().
  220. *
  221. * @return array
  222. */
  223. function user_registrationpassword_mailkeys() {
  224. return array(
  225. 'register' => t('Welcome message when user self-registers and sets password during registration'),
  226. );
  227. }
  228. /**
  229. * Implements hook_mail_edit_text().
  230. *
  231. * @param string $mailkey
  232. * @param object $language
  233. *
  234. * @return array
  235. */
  236. function user_registrationpassword_mail_edit_text($mailkey, $language) {
  237. $return = array();
  238. $return['subject'] = _user_registrationpassword_mail_text($mailkey . '_subject', $language, array(), FALSE);
  239. $return['body'] = _user_registrationpassword_mail_text($mailkey . '_body', $language, array(), FALSE);
  240. return $return;
  241. }
  242. /**
  243. * Implements hook_mail_edit_token_types().
  244. *
  245. * @param string $mailkey
  246. *
  247. * @return array
  248. */
  249. function user_registrationpassword_mail_edit_token_types($mailkey) {
  250. return array('user');
  251. }
  252. /**
  253. * Implements hook_variable_info().
  254. *
  255. * @see variable.module.
  256. */
  257. function user_registrationpassword_variable_info($options) {
  258. $variables['user_registrationpassword_register_[mail_part]'] = array(
  259. 'type' => 'user_mail',
  260. 'title' => t('Welcome, no approval, password is already set', array(), $options),
  261. 'description' => t('Customize welcome e-mail messages sent to new members when password is already set .', array(), $options),
  262. 'group' => 'user_mails',
  263. );
  264. return $variables;
  265. }