Skip to content
Snippets Groups Projects
Commit bcf3491e authored by mattwire's avatar mattwire
Browse files

Handle the case that the CMS username already exists

parent 165a409e
No related branches found
No related tags found
No related merge requests found
......@@ -37,9 +37,13 @@ function _civicrm_api3_cmsuser_create_spec(&$params) {
*/
function civicrm_api3_cmsuser_create($params) {
// Check for existing contact
$ufID = CRM_Core_BAO_UFMatch::getUFId($params['contact_id']);
if ($ufID) {
return civicrm_api3_create_success(['uf_id' => $ufID, 'created' => FALSE], $params);
$ufMatchByContactID = \Civi\Api4\UFMatch::get(FALSE)
->addWhere('contact_id', '=', $params['contact_id'])
->execute()
->first();
if (!empty($ufMatchByContactID)) {
return civicrm_api3_create_success(['uf_id' => $ufMatchByContactID['id'], 'created' => FALSE], $params);
}
// Get email (either from param or from contact record.
......@@ -61,6 +65,14 @@ function civicrm_api3_cmsuser_create($params) {
$params['cms_name'] = $params['email'];
}
$ufMatchByName = \Civi\Api4\UFMatch::get(FALSE)
->addWhere('uf_name', '=', $params['cms_name'])
->execute()
->first();
if (!empty($ufMatchByName)) {
throw new CiviCRM_API3_Exception("Cannot create CMS user for contact ID: {$params['contact_id']}. CMS Username: {$params['cms_name']} already exists.");
}
// If no password specified generate a random one
if (empty($params['cms_pass'])) {
$params['cms_pass'] = bin2hex(random_bytes(10));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment