benefits: Add uniqueness validation for email aliases.
authorDavid Thompson <davet@gnu.org>
Fri, 7 Nov 2014 15:48:37 +0000 (10:48 -0500)
committerDavid Thompson <davet@gnu.org>
Fri, 7 Nov 2014 15:48:37 +0000 (10:48 -0500)
* CRM/Memberdashboard/BAO/EmailAlias.php (CRM_Memberdashboard_BAO_EmailAlias)
  [findByLocalpart]: New method.
* CRM/Memberdashboard/Page/Benefits.php (CRM_Memberdashboard_Page_Benefits)
  [add, edit]: Check local part of alias for uniqueness.

CRM/Memberdashboard/BAO/EmailAlias.php
CRM/Memberdashboard/Page/Benefits.php

index c782c56226d22c76c55a64036b07ae12ee46b848..0aa3f57e7b5df5d221c2b55a43a164198165e0c6 100644 (file)
@@ -69,6 +69,30 @@ class CRM_Memberdashboard_BAO_EmailAlias {
     return $aliases;
   }
 
+  static function findByLocalpart($localpart) {
+    $dao = CRM_Core_DAO::executeQuery(
+      'SELECT * FROM member_email_alias WHERE localpart=%1',
+      array( 1 => array($localpart, 'String') )
+    );
+
+    if($dao->fetch()) {
+      $alias = new CRM_Memberdashboard_BAO_EmailAlias(
+        array(
+          'id' => $dao->id,
+          'contact_id' => $dao->contact_id,
+          'localpart' => $dao->localpart,
+          'forward' => $dao->forward
+        )
+      );
+    } else {
+      $alias = NULL;
+    }
+
+    $dao->free();
+
+    return $alias;
+  }
+
   static function countForContact($contactId) {
     $dao = CRM_Core_DAO::executeQuery(
       'SELECT COUNT(*) AS count FROM member_email_alias WHERE contact_id=%1',
index 7d968f8fffb56ea6121b26389e6d24aa0754e459..41a7da1e8aa4485771009c084ef7f328b71d10a1 100644 (file)
@@ -70,22 +70,27 @@ class CRM_Memberdashboard_Page_Benefits extends CRM_Memberdashboard_Page {
 
     if(CRM_Utils_Rule::email($forward)) {
       if(CRM_Utils_Rule::email("$localpart@example.com")) {
-        if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
-          try {
-            $params = array(
-              'contact_id' => $contactId,
-              'localpart' => $localpart,
-              'forward' => $forward
-            );
-            CRM_Memberdashboard_BAO_EmailAlias::create($params);
-            CRM_Core_Session::setStatus('Email alias added!',
-                                        'Success', 'success');
-          } catch (Exception $e) {
-            CRM_Core_Session::setStatus('Failed to create email alias',
+        if(CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart) == NULL) {
+          if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
+            try {
+              $params = array(
+                'contact_id' => $contactId,
+                'localpart' => $localpart,
+                'forward' => $forward
+              );
+              CRM_Memberdashboard_BAO_EmailAlias::create($params);
+              CRM_Core_Session::setStatus('Email alias added!',
+                                          'Success', 'success');
+            } catch (Exception $e) {
+              CRM_Core_Session::setStatus('Failed to create email alias',
+                                          'Error', 'error');
+            }
+          } else {
+            CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
                                         'Error', 'error');
           }
         } else {
-          CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
+          CRM_Core_Session::setStatus("Local part is already in use: $localpart",
                                       'Error', 'error');
         }
       } else {
@@ -121,9 +126,14 @@ class CRM_Memberdashboard_Page_Benefits extends CRM_Memberdashboard_Page {
           $error .= '<li>You cannot alter email aliases that don\'t belong to you!</li>';
         } else if(CRM_Utils_Rule::email($forward)) {
           if(CRM_Utils_Rule::email("$localpart@example.com")) {
-            $alias->localpart = $localpart;
-            $alias->forward = $forward;
-            $alias->save();
+            $existingAlias = CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart);
+            if($existingAlias == NULL ||  $existingAlias->id == $id) {
+              $alias->localpart = $localpart;
+              $alias->forward = $forward;
+              $alias->save();
+            } else {
+              $error .= "<li>Local part already in use: $localpart</li>";
+            }
           } else {
             $error .= "<li>Invalid local part: $localpart</li>";
           }