Drupal8::getUfId() - Fix exception on unknown usernames
authorTim Otten <totten@civicrm.org>
Tue, 21 Dec 2021 10:32:11 +0000 (02:32 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 21 Dec 2021 22:55:45 +0000 (14:55 -0800)
As mentioned in https://github.com/civicrm/civicrm-core/pull/22239#issuecomment-997108926, there can be an exception:

```
Error: Call to a member function id() on bool in ...\CRM\Utils\System\Drupal8.php on line 359 #0 ...\ext\authx\Civi\Authx\Authenticator.php(380)
```

The function signature specifies `@return int|null`. Various callers appear to expect this, and that seems to be how it behaves on D7/WP.

CRM/Utils/System/Drupal8.php

index 3037cec10a956e64f67805c8c2298e96bdf439c1..52dac70a3b13aee5c0e1b89052ae62189e1b772c 100644 (file)
@@ -356,7 +356,8 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
    * @return int|null
    */
   public function getUfId($username) {
-    if ($id = user_load_by_name($username)->id()) {
+    $user = user_load_by_name($username);
+    if ($user && $id = $user->id()) {
       return $id;
     }
   }