From d62b1592f203ee0ae895fc6c0d22d4f1b0492957 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 15 Feb 2021 12:23:58 +1300 Subject: [PATCH] dev/mail#89 Fix unreleased regression where civimember is not permitted/enabled 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 | 2 +- CRM/Member/BAO/Membership.php | 47 +++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 0edeb2f09a..05e7a4a2c8 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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); diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 2d6af50517..471a15e394 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -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; + } } /** -- 2.25.1