From 3fa64def9730655677d2e50c0aa560fcf8720135 Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Wed, 20 Apr 2022 17:56:52 -0400 Subject: [PATCH] test group memberhip for nomination_process attr as a fallback criteria, being in an adhoc group and currently a member allows for access to the candidate discussion forum --- docs/fsf-drupal-auth.md | 3 +- lib/Auth/Source/FSFDrupalAuth.php | 49 +++++++++++++++++-------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/docs/fsf-drupal-auth.md b/docs/fsf-drupal-auth.md index 59f1db3..aa9a56b 100644 --- a/docs/fsf-drupal-auth.md +++ b/docs/fsf-drupal-auth.md @@ -21,6 +21,7 @@ SQL queries in `config/authsources.php` should be something like the following: '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 = :fsf_org_id 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_id and d.receive_date > :start_date and d.receive_date < :end_date;", + 'query_nomination_process_adhoc' => "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_group_contact d on c.id=d.contact_id where a.name=:username and a.status=1 and c.is_deleted=0 and d.group_id = :adhoc_access_group_id and d.status = 'Added' limit 1;", 'fsf_org_id' => '739106', @@ -29,7 +30,7 @@ SQL queries in `config/authsources.php` should be something like the following: 'gift_redeem_page_id' => '63', 'membership_donation_threshold' => '5', 'donation_threshold' => '5', - + 'adhoc_access_group_id' => '1786', ], ## License diff --git a/lib/Auth/Source/FSFDrupalAuth.php b/lib/Auth/Source/FSFDrupalAuth.php index 515a876..8311a06 100644 --- a/lib/Auth/Source/FSFDrupalAuth.php +++ b/lib/Auth/Source/FSFDrupalAuth.php @@ -46,6 +46,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase private $query_staff; private $query_nomination_process_donations; private $query_nomination_process_gift_receipt; + private $query_nomination_process_adhoc; /** * SQL query parameters, or variables that help determine which attributes @@ -57,6 +58,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase private $gift_redeem_page_id; private $donation_threshold; private $membership_donation_threshold; + private $adhoc_access_group_id; /** * Constructor for this authentication source. @@ -76,11 +78,13 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase foreach (['dsn', 'username', 'password', 'query_main', 'query_membership', 'query_staff', 'query_nomination_process_donations', - 'query_nomination_process_gift_receipt', 'gift_redeem_page_id', + 'query_nomination_process_gift_receipt', + 'query_nomination_process_adhoc', 'gift_redeem_page_id', 'fsf_org_id', 'donation_threshold', 'membership_donation_threshold', 'nomination_process_contrib_start_date', - 'nomination_process_contrib_end_date'] as $param) { + 'nomination_process_contrib_end_date', 'adhoc_access_group_id'] + as $param) { if (!array_key_exists($param, $config)) { throw new Exception('Missing required attribute \''.$param. @@ -184,18 +188,18 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase $return_value = proc_close($process); //Logger::debug('fsfdrupalauth:'.$this->authId.': authenticator stdout: '.$result); - + $errors_found_yet = false; if ($errors != "") { Logger::error('fsfdrupalauth:'.$this->authId.': authenticator stderr: '.$errors); $errors_found_yet = true; } - + if ($return_value != 0) { Logger::error('fsfdrupalauth:'.$this->authId.': authenticator non-zero return code: '.$return_value); $errors_found_yet = true; } - + return (!$errors_found_yet && is_string($result) && rtrim($result) == "true"); } else { @@ -294,37 +298,38 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase * @param boolean $all_contribs Pass as 'all_contribs' param. The var may also be null to exclude it * @param boolean $gift_redeem_page_id Pass as 'gift_redeem_page_id' param. The var may also be null to exclude it */ - $meets_a_donation_criterion = function ($query_name, $amount, $all_contribs, $gift_redeem_page_id) + $meets_a_donation_criterion = function ($query_name, $amount, $extra_params) use ($username, $start_date, $end_date, $attributes) { - $parameters = - ['username' => $username, - 'start_date' => $start_date, - 'end_date' => $end_date]; + $parameters = ['username' => $username]; - if ($all_contribs !== null) { - $parameters['all_contribs'] = $all_contribs; - } - if ($gift_redeem_page_id !== null) { - $parameters['gift_redeem_page_id'] = $gift_redeem_page_id; + foreach ($extra_params as $key => $value) { + $parameters[$key] = $value; } $result = $this->query_db($query_name, $parameters); foreach ($result[0] as $key => $value) { - if (intval($value) < $amount) { - continue; - } elseif ($attributes['is_member'] == ['true']) { + if (intval($value) >= $amount) { return true; } } - return false; }; - if ($meets_a_donation_criterion('query_nomination_process_donations', intval($this->donation_threshold), true, null) - || $meets_a_donation_criterion('query_nomination_process_donations', intval($this->membership_donation_threshold), false, null) - || $meets_a_donation_criterion('query_nomination_process_gift_receipt', 1, null, intval($this->gift_redeem_page_id))) { + if (($meets_a_donation_criterion('query_nomination_process_donations', + intval($this->donation_threshold), ['all_contribs' => true, 'start_date' => $start_date, 'end_date' => $end_date]) + + || $meets_a_donation_criterion('query_nomination_process_donations', + intval($this->membership_donation_threshold), ['all_contribs' => false, 'start_date' => $start_date, 'end_date' => $end_date]) + + || $meets_a_donation_criterion('query_nomination_process_gift_receipt', + 1, ['gift_redeem_page_id' => intval($this->gift_redeem_page_id), 'start_date' => $start_date, 'end_date' => $end_date]) + + || $meets_a_donation_criterion('query_nomination_process_adhoc', + 1, ['adhoc_access_group_id' => intval($this->adhoc_access_group_id)]) + ) + && ($attributes['is_member'] == ['true'])) { $attributes['nomination_process'] = ['true']; } else { -- 2.25.1