From 0f90ab0f12a174f4cf913f6292029f3266de8120 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 27 Jun 2023 14:36:23 -0700 Subject: [PATCH] (REF) Invert relation between login functions. Prepare to reduce loads. Before: * Civi\Standalone\Security::loginAuthenticatedUserRecord() calls Civix\Authx\Standalone::loginStateless() and Civix\Authx\Standalone::loginStateful() * The logged-in user may be loaded twice - e.g. for workflows that hit `loginAuthenticatedUserRecord()` After * Civix\Authx\Standalone::loginStateless() and Civix\Authx\Standalone::loginStateful() call Civi\Standalone\Security::loginAuthenticatedUserRecord() * The logged-in user is still loaded twice... but it'll small fix to load only once. Comments * The redundancy arose because `loginAuthenticatedUserRecord()` had the full-record, and it called to `loginStateless()` (etc) with just ID or name - which then loaded the full-record again. * One might ask: "Is one orientation conceptually or philosophically better than the other?" I don't have a good answer to that, but... I think this patch makes for a nicer symmetry between `Civi\Authx\Standalone` and `Civi\Authx\{Drupal,WordPress,etc}`. In all cases, they take the userID/username and then perform a 1-4 SLOC incantation that populates the global user-record. --- ext/standaloneusers/Civi/Authx/Standalone.php | 21 ++++--------------- .../Civi/Standalone/Security.php | 20 +++++++++++++----- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/ext/standaloneusers/Civi/Authx/Standalone.php b/ext/standaloneusers/Civi/Authx/Standalone.php index 79ad7ed45b..0eea52cc9f 100644 --- a/ext/standaloneusers/Civi/Authx/Standalone.php +++ b/ext/standaloneusers/Civi/Authx/Standalone.php @@ -28,19 +28,8 @@ class Standalone implements AuthxInterface { * @inheritDoc */ public function loginSession($userId) { - $this->loginStateless($userId); - - $session = \CRM_Core_Session::singleton(); - $session->set('ufID', $userId); - - // Identify the contact - $contactID = civicrm_api3('UFMatch', 'get', [ - 'sequential' => 1, - 'return' => ['contact_id'], - 'uf_id' => $userId, - ])['values'][0]['contact_id'] ?? NULL; - // Confusingly, Civi stores it's *Contact* ID as *userId* on the session. - $session->set('userId', $contactID); + $user = Security::singleton()->loadUserByID($userId); + Security::singleton()->loginAuthenticatedUserRecord($user, TRUE); } /** @@ -54,10 +43,8 @@ class Standalone implements AuthxInterface { * @inheritDoc */ public function loginStateless($userId) { - global $loggedInUserId; - $loggedInUserId = $userId; - global $loggedInUser; - $loggedInUser = \Civi\Standalone\Security::singleton()->loadUserByID($userId); + $user = Security::singleton()->loadUserByID($userId); + Security::singleton()->loginAuthenticatedUserRecord($user, FALSE); } /** diff --git a/ext/standaloneusers/Civi/Standalone/Security.php b/ext/standaloneusers/Civi/Standalone/Security.php index 4a503213df..2e58b8aac3 100644 --- a/ext/standaloneusers/Civi/Standalone/Security.php +++ b/ext/standaloneusers/Civi/Standalone/Security.php @@ -225,12 +225,22 @@ class Security { * Currently only used by CRM_Utils_System_Standalone::loadBootstrap */ public function loginAuthenticatedUserRecord(array $user, bool $withSession) { - $authX = new \Civi\Authx\Standalone(); + global $loggedInUserId, $loggedInUser; + $loggedInUserId = $user['id']; + $loggedInUser = \Civi\Standalone\Security::singleton()->loadUserByID($user['id']); + if ($withSession) { - $authX->loginSession($user['id']); - } - else { - $authX->loginStateless($user['id']); + $session = \CRM_Core_Session::singleton(); + $session->set('ufID', $user['id']); + + // Identify the contact + $contactID = civicrm_api3('UFMatch', 'get', [ + 'sequential' => 1, + 'return' => ['contact_id'], + 'uf_id' => $user['id'], + ])['values'][0]['contact_id'] ?? NULL; + // Confusingly, Civi stores it's *Contact* ID as *userId* on the session. + $session->set('userId', $contactID); } } -- 2.25.1