You are here

function multiple_email_user in Multiple E-mail Addresses 5

Same name and namespace in other branches
  1. 6 multiple_email.module \multiple_email_user()

Implementation of hook_user()

Parameters

string $op:

array $edit:

object $account:

string $category:

Return value

mixed

File

./multiple_email.module, line 141
multiple_email module file

Code

function multiple_email_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'login':
      if (!$account->login) {

        // They've never logged in before, so we're assuming their
        //   email address is confirmed since they were able to login
        $email = multiple_email_find_address($account->mail);
        multiple_email_confirm_email($email);
      }
      break;
    case 'insert':

      /**
       * Add user's entered email to the email registry as an
       * unconfirmed address, to be confirmed upon first successful
       * login to the web site.
       */

      // First check for any other user who tried to register the address
      if ($email = multiple_email_find_address($edit['mail'])) {

        // Someone else has it registered, but not confirmed (passed validation), so delete
        multiple_email_delete_email($email->eid);
      }

      // Register email with new user
      multiple_email_register_email($account->uid, $edit['mail']);
      break;
    case 'delete':

      // Cleanup our mess
      db_query('DELETE FROM {multiple_email} WHERE uid=%d', $account->uid);
      break;
    case 'submit':

      // Do not allow primary address to be changed from account edit
      unset($edit['mail']);
      break;
    case 'validate':

      // Keep new users from taking registered email addresses
      if ($edit['form_id'] == 'user_register' && ($email = multiple_email_find_address($edit['mail']))) {
        $owner = user_load(array(
          'uid' => $email->uid,
        ));

        // Don't put up error message if it is owner's primary, Drupal does that
        // Also, they can register with an address if it's unconfirmed
        if ($email->confirmed && $owner->mail != $email->email) {
          $message = t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array(
            '%email' => $edit['mail'],
            '@password' => url('user/password'),
          ));
          form_set_error('mail', $message);
        }
      }
      break;
  }
}