Standalone - Track logged-in user object
authorTim Otten <totten@civicrm.org>
Tue, 27 Jun 2023 08:49:36 +0000 (01:49 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 29 Jun 2023 20:56:02 +0000 (13:56 -0700)
Overview
--------

This is a partial solution for running `E2E_Extern_CliRunnerTest::testPermissionLookup()` on Standalone.

Technical Details
-----------------

The test calls:

```
cv ev --user='admin' 'echo json_encode(CRM_Core_Config::singleton()->userPermissionClass->check("administer CiviCRM", 0));'
```

Internally, this command relies on `CRM_Core_BAO_UFMatch::synchronize()` by way of:

https://github.com/civicrm/cv/blob/59efd9ffefde8dc500643e048d37db90e0de11b7/src/Util/BootTrait.php#L213-L235

Calls to `UFMatch::synchronize($user)` are expected to pass in a user-record.

Before
------

Standalone logins record the active user-ID.

After
------

Standalone logins record the active user-ID and active user-record (which can be supplied to `UFMatch::synchronize()`).

CRM/Utils/System/Standalone.php
ext/standaloneusers/Civi/Authx/Standalone.php

index 3c842063e93caa41b2dde0a968005981d5c69da9..db53c0bb59cbe1ac58e67937d759a94fb4cc87e1 100644 (file)
@@ -338,6 +338,18 @@ class CRM_Utils_System_Standalone extends CRM_Utils_System_Base {
     return TRUE;
   }
 
+  public function loadUser($username) {
+    $security = \Civi\Standalone\Security::singleton();
+    $user = $security->loadUserByName($username);
+    if ($user) {
+      $security->loginAuthenticatedUserRecord($user, TRUE);
+      return TRUE;
+    }
+    else {
+      return FALSE;
+    }
+  }
+
   /**
    * @inheritdoc
    */
index ee73746b8fdf33726a3313b457bb7db8a2623b31..79ad7ed45be59dc4ee697b9948e982b63188121f 100644 (file)
@@ -56,6 +56,8 @@ class Standalone implements AuthxInterface {
   public function loginStateless($userId) {
     global $loggedInUserId;
     $loggedInUserId = $userId;
+    global $loggedInUser;
+    $loggedInUser = \Civi\Standalone\Security::singleton()->loadUserByID($userId);
   }
 
   /**