From c0d116a97dd5f36d7944d9e2379fabc3e4acb1d0 Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Fri, 8 Apr 2022 16:09:27 -0400 Subject: [PATCH] attribute to distinguish long-term members this is an early version of the feature that still needs some testing. the cutoff date and query must be set in the authsources.php file. --- docs/fsf-drupal-auth.md | 2 ++ lib/Auth/Source/FSFDrupalAuth.php | 32 ++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/fsf-drupal-auth.md b/docs/fsf-drupal-auth.md index 4ebbd02..3c1fd52 100644 --- a/docs/fsf-drupal-auth.md +++ b/docs/fsf-drupal-auth.md @@ -10,6 +10,7 @@ SQL queries in `config/authsources.php` should be something like the following: // custom fsf authentication source wrapped by ratelimit auth source 'fsfdrupalauth:FSFDrupalAuth', + 'member_long_term_date' => '2022-01-01', 'dsn' => 'mysql:host=example.com;port=3306;dbname=drupal', 'username' => '$DB_USERNAME', 'password' => '$DB_PASSWORD', @@ -17,6 +18,7 @@ SQL queries in `config/authsources.php` should be something like the following: // don't filter with 'and c.is_test = 0' because these may be useful for FSF staff '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_long_term_member' => "select c.join_date 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.name = :username and c.status_id is not NULL and (c.status_id = 1 or c.status_id = 2 or c.status_id = 3 or c.status_id = 4) and c.join_date <= :member_long_term_date order by c.join_date limit 1;", ], diff --git a/lib/Auth/Source/FSFDrupalAuth.php b/lib/Auth/Source/FSFDrupalAuth.php index 6695cc0..f09346b 100644 --- a/lib/Auth/Source/FSFDrupalAuth.php +++ b/lib/Auth/Source/FSFDrupalAuth.php @@ -45,6 +45,11 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase private $query_membership; private $query_staff; + /** + * Date for determining whether someone is a long-term member or not + */ + private $member_long_term_date; + /** * Constructor for this authentication source. * @@ -60,7 +65,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'] as $param) { + foreach (['dsn', 'username', 'password', 'query_main', 'query_membership', 'query_staff', 'member_long_term_date'] as $param) { if (!array_key_exists($param, $config)) { throw new Exception('Missing required attribute \''.$param. '\' for authentication source '.$this->authId); @@ -80,6 +85,7 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase $this->query_main = $config['query_main']; $this->query_membership = $config['query_membership']; $this->query_staff = $config['query_staff']; + $this->member_long_term_date = $config['member_long_term_date']; if (isset($config['options'])) { $this->options = $config['options']; } @@ -264,6 +270,30 @@ class FSFDrupalAuth extends \SimpleSAML\Module\core\Auth\UserPassBase } } + // + // query on first membership join date + // + + $membership_date_data = $this->query_db('query_membership_date', $username, $member_long_term_date); + + if (count($membership_date_data) === 0) { + // No rows returned - no old membership start + Logger::debug('fsfdrupalauth:'.$this->authId. + ': No rows in result set. May only be a short term member.'); + } + + $attributes['long_term_member'] = ['false']; + + foreach ($membership_date_data as $row) { + foreach ($row as $key => $value) { + if ($value === null) { + continue; + } elseif ($attributes['is_member'][0] == 'true') { + $attributes['long_term_member'] = ['true']; + } + } + } + // // query on staff // -- 2.25.1