(REF) Invert relation between login functions. Prepare to reduce loads.
authorTim Otten <totten@civicrm.org>
Tue, 27 Jun 2023 21:36:23 +0000 (14:36 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 30 Jun 2023 18:57:51 +0000 (11:57 -0700)
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
ext/standaloneusers/Civi/Standalone/Security.php

index 79ad7ed45be59dc4ee697b9948e982b63188121f..0eea52cc9f84f1273530b7465be92ad60c26f537 100644 (file)
@@ -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);
   }
 
   /**
index 4a503213dfb0c2e9c55ab60c7b672db5ba4557bf..2e58b8aac38b7c173e8961d8ee93b84d3d950c3e 100644 (file)
@@ -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);
     }
   }