From 8b7a49981f90d84bdf40fa6689cba1008662ee6d Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Fri, 3 Feb 2023 13:09:28 +0000 Subject: [PATCH] standalone: implement createUser --- CRM/Utils/System/Standalone.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/CRM/Utils/System/Standalone.php b/CRM/Utils/System/Standalone.php index f08bb52125..9545aee9eb 100644 --- a/CRM/Utils/System/Standalone.php +++ b/CRM/Utils/System/Standalone.php @@ -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; } /** -- 2.25.1