? sharedemail.895924-2.patch
Index: sharedemail.api.php
===================================================================
RCS file: sharedemail.api.php
diff -N sharedemail.api.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sharedemail.api.php	29 Aug 2010 02:48:01 -0000
@@ -0,0 +1,27 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ *  Definition of API functions for Sharedemail module.
+ */
+
+/**
+ * Implementation of hook_sharedemail_policy_info().
+ *
+ * @param $account
+ *  The user account which is going to be allowed or denied the ability to use
+ *  an existing email address.
+ * @param $existing
+ *  An array of the UIDs of accounts already using the desired email address.
+ * @param $mail
+ *  A string of the email address to be used.
+ *
+ * @return
+ *  Boolean. FALSE to deny shared email, TRUE to allow it.
+ */
+function hook_sharedemail_policy_info($account, $existing, $mail) {
+  // In this example, we limit shared emails to accounts without user
+  // administration permission.
+  return !user_perm('administer users', $account);
+}
Index: sharedemail.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/sharedemail/sharedemail.module,v
retrieving revision 1.1.2.2.2.6
diff -u -p -r1.1.2.2.2.6 sharedemail.module
--- sharedemail.module	23 Jun 2009 18:54:08 -0000	1.1.2.2.2.6
+++ sharedemail.module	29 Aug 2010 02:48:01 -0000
@@ -74,14 +74,8 @@ function sharedemail_user($type, &$edit,
 
   switch ($type) {
     case 'validate':
-      // If no problems with validation, do nothing
-      if (user_validate_mail($mail)) {
-        return;
-      }
-
       // Show warning message if more than 1 user with the same email
-      $uid = db_result(db_query("SELECT uid FROM {users} WHERE uid <> %d AND LOWER(mail) = LOWER('%s')", $user->uid, $edit['mail']));
-      if (!empty($uid)) {
+      if (sharedemail_use_existing_email($user, $mail)) {
         $edit['mail']= 'sharedemail_'. $mail;
         if (module_exists('logintoboggan')) {
           $edit['conf_mail'] = 'sharedemail_'. $mail;
@@ -116,6 +110,40 @@ function sharedemail_user($type, &$edit,
 }
 
 /**
+ * Identify if the account is allowed to use an existing email address.
+ *
+ * @param $account
+ *  The user account being created or updated.
+ * @param $mail
+ *  The email address being saved for the account.
+ * @return
+ *  Boolean. TRUE to allow sharedemail, FALSE to deny it.
+ */
+function sharedemail_use_existing_email($account, $mail) {
+  // Is it a valid email address? Then there's no need to do this.
+  if (user_validate_mail($mail)) {
+    return FALSE;
+  }
+  // Find existing accounts using this email address.
+  $results = db_query("SELECT uid FROM {users} WHERE uid <> %d AND LOWER(mail) = LOWER('%s')", $account->uid, $mail);
+  while ($u = db_fetch_array($results)) {
+    $existing[] = $u['uid'];
+  }
+  // If no accounts are using this address, there is no need for sharedemail to operate.
+  if (empty($existing)) {
+    return FALSE;
+  }
+  // Allow other modules to reject sharing an email for this account.
+  $policies = module_invoke_all('sharedemail_policy_info', $account, $existing, $mail);
+  foreach ($policies as $policy) {
+    if ($policy === FALSE) {
+      return FALSE;
+    }
+  }
+  return TRUE;
+}
+
+/**
  *  Implementation of hook_simpletest().
  */
 function sharedemail_simpletest() {
