dev/mail#89 Fix unreleased regression where civimember is not permitted/enabled
authoreileen <emcnaughton@wikimedia.org>
Sun, 14 Feb 2021 23:23:58 +0000 (12:23 +1300)
committereileen <emcnaughton@wikimedia.org>
Sun, 14 Feb 2021 23:23:58 +0000 (12:23 +1300)
Fixes a bug where a person with no access to CiviMember will get an exception thrown (and
not caught) rather than receive a result of 0 when checking the memberships
that a contact has access to

CRM/Contact/BAO/Contact.php
CRM/Member/BAO/Membership.php

index 0edeb2f09a4101f923a0fc136f278a95597c85c6..05e7a4a2c85c61c0bb9e99b4acfab77c96f55566 100644 (file)
@@ -2657,7 +2657,7 @@ LEFT JOIN civicrm_email    ON ( civicrm_contact.id = civicrm_email.contact_id )
         return CRM_Contribute_BAO_Contribution::contributionCount($contactId);
 
       case 'membership':
-        return CRM_Member_BAO_Membership::getContactMembershipCount($contactId, TRUE);
+        return CRM_Member_BAO_Membership::getContactMembershipCount((int) $contactId, TRUE);
 
       case 'participant':
         return CRM_Event_BAO_Participant::getContactParticipantCount($contactId);
index 2d6af50517c2c1062b1543a16a7ce537fe2ef172..471a15e39416b86d7a100c94c145ffa92f07502e 100644 (file)
@@ -9,6 +9,9 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\API\Exception\UnauthorizedException;
+use Civi\Api4\MembershipType;
+
 /**
  *
  * @package CRM
@@ -1572,29 +1575,35 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
    * @param int $contactID
    * @param bool $activeOnly
    *
-   * @return null|string
+   * @return int
+   * @throws \API_Exception
    */
-  public static function getContactMembershipCount($contactID, $activeOnly = FALSE) {
-    $membershipTypes = \Civi\Api4\MembershipType::get(TRUE)
-      ->execute()
-      ->indexBy('id')
-      ->column('name');
-    $addWhere = " AND membership_type_id IN (0)";
-    if (!empty($membershipTypes)) {
-      $addWhere = " AND membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
-    }
+  public static function getContactMembershipCount(int $contactID, $activeOnly = FALSE): int {
+    try {
+      $membershipTypes = MembershipType::get(TRUE)
+        ->execute()
+        ->indexBy('id')
+        ->column('name');
+      $addWhere = " AND membership_type_id IN (0)";
+      if (!empty($membershipTypes)) {
+        $addWhere = " AND membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
+      }
 
-    $select = "SELECT count(*) FROM civicrm_membership ";
-    $where = "WHERE civicrm_membership.contact_id = {$contactID} AND civicrm_membership.is_test = 0 ";
+      $select = "SELECT COUNT(*) FROM civicrm_membership ";
+      $where = "WHERE civicrm_membership.contact_id = {$contactID} AND civicrm_membership.is_test = 0 ";
 
-    // CRM-6627, all status below 3 (active, pending, grace) are considered active
-    if ($activeOnly) {
-      $select .= " INNER JOIN civicrm_membership_status ON civicrm_membership.status_id = civicrm_membership_status.id ";
-      $where .= " and civicrm_membership_status.is_current_member = 1";
-    }
+      // CRM-6627, all status below 3 (active, pending, grace) are considered active
+      if ($activeOnly) {
+        $select .= " INNER JOIN civicrm_membership_status ON civicrm_membership.status_id = civicrm_membership_status.id ";
+        $where .= " and civicrm_membership_status.is_current_member = 1";
+      }
 
-    $query = $select . $where . $addWhere;
-    return CRM_Core_DAO::singleValueQuery($query);
+      $query = $select . $where . $addWhere;
+      return (int) CRM_Core_DAO::singleValueQuery($query);
+    }
+    catch (UnauthorizedException $e) {
+      return 0;
+    }
   }
 
   /**