standalone: implement createUser
authorRich Lott / Artful Robot <code.commits@artfulrobot.uk>
Fri, 3 Feb 2023 13:09:28 +0000 (13:09 +0000)
committerRich Lott / Artful Robot <code.commits@artfulrobot.uk>
Fri, 23 Jun 2023 10:47:57 +0000 (11:47 +0100)
CRM/Utils/System/Standalone.php

index f08bb5212567a2cbee4f5bbdf827919307a33922..9545aee9ebfec57b08c7d2a0ac10ac0dce08b58d 100644 (file)
@@ -33,9 +33,37 @@ class CRM_Utils_System_Standalone extends CRM_Utils_System_Base {
 
   /**
    * @inheritDoc
+   *
+   * Create a user in the CMS.
+   *
+   * @param array $params keys:
+   *    - 'cms_name'
+   *    - 'cms_pass' plaintext password
+   *    - 'notify' boolean
+   * @param string $mail
+   *   Email id for cms user.
+   *
+   * @return int|bool
+   *   uid if user was created, false otherwise
    */
   public function createUser(&$params, $mail) {
-    return FALSE;
+
+    try {
+      $userID = \Civi\Api4\User::create(TRUE)
+      ->addValue('username', $params['cms_name'])
+      ->addValue('mail', $mail)
+      // @todo the Api should ensure a password is encrypted? Or call a method to do that here?
+      ->addValue('password', $params['cms_pass'])
+      ->execute()->single()['id'];
+      }
+    catch (\Exception $e) {
+      \Civi::log()->warning("Failed to create user '$mail': " . $e->getMessage());
+      return FALSE;
+    }
+
+    // @todo This is what Drupal does, but it's unclear why.
+    // CRM_Core_Config::singleton()->inCiviCRM = FALSE;
+    return (int) $userID;
   }
 
   /**