You are here

sharedemail.module in Shared Email 7

Same filename and directory in other branches
  1. 8.2 sharedemail.module
  2. 5 sharedemail.module
  3. 6 sharedemail.module

Allow an e-mail address to be used by more than one user account.

File

sharedemail.module
View source
<?php

/**
 * @file
 * Allow an e-mail address to be used by more than one user account.
 */

/**
 * Implements hook_help().
 */
function sharedemail_help($path, $arg) {
  switch ($path) {
    case 'admin/help#sharedemail':
      $output = '<p>' . t('Allow an e-mail address to be used by more than one user account.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function sharedemail_permission() {
  return array(
    'administer sharedemail' => array(
      'title' => t('Administer Shared E-mail'),
    ),
    'show warning text' => array(
      'title' => t('Show Shared E-mail warning'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function sharedemail_menu() {
  $items['admin/config/people/sharedemail'] = array(
    'title' => 'Shared E-mail',
    'description' => 'Configure the message that Shared E-mail displays.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sharedemail_admin_settings',
    ),
    'access arguments' => array(
      'administer sharedemail',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Configure Shared E-mail settings.
 *
 * @ingroup forms
 * @see system_settings_form()
 */
function sharedemail_admin_settings($form, &$form_state) {
  $msg = t('WARNING: The e-mail address you are using has already been registered on this site by another user account. ' . 'You should be aware that personal information such as password resets will be sent to this address. ' . 'We strongly recommend changing your registered address to a different e-mail address. ' . 'You can do this at any time from your account page when you login.');
  $form['sharedemail_msg'] = array(
    '#type' => 'textarea',
    '#title' => t('Shared E-mail message'),
    '#default_value' => variable_get('sharedemail_msg', $msg),
    '#rows' => 15,
    '#description' => t('Warning message that is only displayed to users with appropriate permission, when they choose to save an e-mail address already used by another user.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_form_FORM_ID_alter() for user_register_form().
 *
 * Replace's the form's default #validate function with a wrapper function
 * to make the User module think the e-mail address is unique.
 */
function sharedemail_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  sharedemail_form_user_account_form_alter($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter() for user_profile_form().
 *
 * Replace's the form's default #validate function with a wrapper function
 * to make the User module think the e-mail address is unique.
 */
function sharedemail_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  sharedemail_form_user_account_form_alter($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter() for user_account_form().
 *
 * Replace's the form's default #validate function with a wrapper function
 * to make the User module think the e-mail address is unique.
 *
 * @see user_account_form()
 * @see user_account_form_validate()
 */
function sharedemail_form_user_account_form_alter(&$form, &$form_state, $form_id) {

  // Find the $form['#validate'] entry for user_profile_form's default validater,
  // and replace it with the name of this module's #validate function.
  if (is_array($form['#validate'])) {
    $key = array_search('user_account_form_validate', $form['#validate'], TRUE);
    if ($key !== FALSE) {
      $form['#validate'][$key] = 'sharedemail_account_form_validate';
    }
  }
}

/**
 * Form validation handler for user_account_form().
 *
 * This function adds a prefix to the e-mail address before calling
 * user_account_form_validate() to perform the default form validation,
 * then restores the e-mail address.
 *
 * @see user_account_form()
 * @see user_account_form_validate()
 */
function sharedemail_account_form_validate($form, &$form_state) {

  // Test whether the e-mail address should be disguised.
  if ($mail = sharedemail_is_shared_email($form, $form_state)) {
    $form_state['values']['mail'] = 'sharedemail_' . $mail;
  }

  // Call the User module's validate function.
  user_account_form_validate($form, $form_state);

  // Restore the actual e-mail address.
  if ($mail) {
    $form_state['values']['mail'] = $mail;
  }
}

/**
 * Check whether the e-mail address should be disguised during validation.
 *
 * If the e-mail address is valid, check whether there are any other users
 * with the same address.  If there are, the address must be modified
 * so that the User module's validation function will think it's unique.
 *
 * @param $form
 *   The user form that was submitted.
 * @param $form_state
 *   The state of the form.
 *
 * @return
 *   Returns the e-mail address if it's a duplicate and is allowed.
 *   Otherwise nothing is returned.
 */
function sharedemail_is_shared_email($form, &$form_state) {
  $mail = trim($form_state['values']['mail']);

  // If e-mail address is invalid, don't have to disguise it.
  if (!user_validate_mail($mail)) {

    // Get the UIDs of all other users who have this e-mail address.
    $account = $form['#user'];
    $existing = db_select('users')
      ->fields('users', array(
      'uid',
    ))
      ->condition('uid', $account->uid, '<>')
      ->condition('mail', db_like($mail), 'LIKE')
      ->execute()
      ->fetchCol();
    if (count($existing) > 0) {
      if (user_access('show warning text')) {
        drupal_set_message(variable_get('sharedemail_msg', ''));
      }
      return $mail;
    }
  }

  // return nothing.
}