From 298545328a7fa38850d8bd2d2612d380b67ae8bf Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Wed, 20 Apr 2022 14:31:20 -0400 Subject: [PATCH] also check for gift memberships when determining nomination process eligibility --- docs/fsf-drupal-auth.md | 1 + lib/Auth/Source/FSFDrupalAuth.php | 47 ++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/docs/fsf-drupal-auth.md b/docs/fsf-drupal-auth.md index c0f5ad1..536c8f9 100644 --- a/docs/fsf-drupal-auth.md +++ b/docs/fsf-drupal-auth.md @@ -20,6 +20,7 @@ SQL queries in `config/authsources.php` should be something like the following: 'query_membership' => "select c.status_id from drupal.users a inner join civicrm.civicrm_uf_match b on a.uid=b.uf_id inner join civicrm.civicrm_membership c on b.contact_id=c.contact_id inner join civicrm.civicrm_contact d on c.contact_id=d.id where a.status = 1 and d.is_deleted = 0 and c.status_id is not NULL and a.name = :username and (c.status_id = 1 or c.status_id = 2 or c.status_id = 3 or c.status_id = 4) order by c.status_id limit 10;", 'query_staff' => "select a.name as is_fsf_staff from drupal.users a inner join civicrm.civicrm_uf_match b on a.uid=b.uf_id inner join civicrm.civicrm_contact c on b.contact_id=c.id inner join civicrm.civicrm_relationship d on c.id=d.contact_id_a where a.name=:username and a.status=1 and c.is_deleted=0 and d.relationship_type_id=4 and d.contact_id_b=FOOBAR and d.is_active=1 and (d.end_date>NOW() or d.end_date is NULL) limit 1;", 'query_nomination_process_donations' => "select sum(d.total_amount) from drupal.users a inner join civicrm.civicrm_uf_match b on a.uid=b.uf_id inner join civicrm.civicrm_contact c on b.contact_id=c.id inner join civicrm.civicrm_contribution d on c.id=d.contact_id where a.name = :username and (d.financial_type_id = '2' or :all_contribs) and d.receive_date > :start_date and d.receive_date < :end_date;", + 'query_nomination_process_gift_receipt' => "select count(*) from drupal.users a inner join civicrm.civicrm_uf_match b on a.uid=b.uf_id inner join civicrm.civicrm_contact c on b.contact_id=c.id inner join civicrm.civicrm_contribution d on c.id=d.contact_id where a.name = :username and d.contribution_page_id = :gift_redeem_page and d.receive_date > :start_date and d.receive_date < :end_date;", ], diff --git a/lib/Auth/Source/FSFDrupalAuth.php b/lib/Auth/Source/FSFDrupalAuth.php index ad3c566..365ecef 100644 --- a/lib/Auth/Source/FSFDrupalAuth.php +++ b/lib/Auth/Source/FSFDrupalAuth.php @@ -45,6 +45,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase private $query_membership; private $query_staff; private $query_nomination_process_donations; + private $query_nomination_process_gift_receipt; /** * Date for determining whether someone may participate in board nomination @@ -68,7 +69,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase parent::__construct($info, $config); // Make sure that all required parameters are present. - foreach (['dsn', 'username', 'password', 'query_main', 'query_membership', 'query_staff', 'query_nomination_process_donations', 'nomination_process_contrib_start_date', 'nomination_process_contrib_end_date'] as $param) { + foreach (['dsn', 'username', 'password', 'query_main', 'query_membership', 'query_staff', 'query_nomination_process_donations', 'query_nomination_process_gift_receipt', 'nomination_process_contrib_start_date', 'nomination_process_contrib_end_date'] as $param) { if (!array_key_exists($param, $config)) { throw new Exception('Missing required attribute \''.$param. '\' for authentication source '.$this->authId); @@ -89,6 +90,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase $this->query_membership = $config['query_membership']; $this->query_staff = $config['query_staff']; $this->query_nomination_process_donations = $config['query_nomination_process_donations']; + $this->query_nomination_process_gift_receipt = $config['query_nomination_process_gift_receipt']; $this->nomination_process_contrib_start_date = $config['nomination_process_contrib_start_date']; $this->nomination_process_contrib_end_date = $config['nomination_process_contrib_end_date']; if (isset($config['options'])) { @@ -279,22 +281,36 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase // query for access to board nomination process // - // $all_contribs: whether to include all contrib types in total - // ... false means only membership donations are counted. - // $amount: donation total threshold for qualifying - $meets_a_donation_criterion = function ($all_contribs, $amount) { + $start_date = $this->nomination_process_contrib_start_date; + $end_date = $this->nomination_process_contrib_end_date; + + /** + * @param string $query_name Name of query in authsources + * @param number $amount Amount to compare result to + * @param boolean $all_contribs Pass as 'all_contribs' param. The var may also be null to exclude it + * @param boolean $gift_redeem_page Pass as 'gift_redeem_page' param. The var may also be null to exclude it + */ + $meets_a_donation_criterion = function ($query_name, $amount, $all_contribs, $gift_redeem_page) + use ($username, $start_date, $end_date, $attributes) { + + $parameters = + ['username' => $username, + 'start_date' => $start_date, + 'end_date' => $end_date]; + + if ($all_contribs !== null) { + $parameters['all_contribs'] = $all_contribs; + } + if ($gift_redeem_page !== null) { + $parameters['gift_redeem_page'] = $gift_redeem_page; + } - $ranged_membership_donation_total = - $this->query_db('query_nomination_process_donations', - ['username' => $username, - 'start_date' => $nomination_process_contrib_start_date, - 'end_date' => $nomination_process_contrib_end_date, - 'all_contribs' => $all_contribs]); + $result = $this->query_db($query_name, $parameters); - foreach ($ranged_membership_donation_total[0] as $key => $value) { + foreach ($result[0] as $key => $value) { if ($value < $amount) { continue; - } elseif ($attributes['is_member'][0] == 'true') { + } elseif ($attributes['is_member'] == ['true']) { return true; } } @@ -302,7 +318,10 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase return false; }; - if ($meets_a_donation_criterion(true, 5) || $meets_a_donation_criterion(false, 5)) { + if ($meets_a_donation_criterion('query_nomination_process_donations', 5, true, null) + || $meets_a_donation_criterion('query_nomination_process_donations', 5, false, null) + || $meets_a_donation_criterion('query_nomination_process_gift_receipt', 1, null, 63)) { + $attributes['nomination_process'] = ['true']; } else { $attributes['nomination_process'] = ['false']; -- 2.25.1