Create User Record systematically create an account with just the e-mail address
Overview
Create User Record (civicrm/contact/view/useradd?reset=1&action=add&cid=xxxx) don't find existing contact, and create a new one with only email adress of the existing contact
Reproduction steps
- On view contact, click Actions and Create User Record
- Complete form with username and password, then validate
- Search contact with the same email address than the existing contact
- You will find the existing contact, and a new one
- UFid is set on the new account
Expected behaviour
UFid should be set to existing account, and no new one should be created
Environment information
- CiviCRM: 5.61.4 and the problem must have existed for quite a few previous versions
- PHP: 8.1.15
- CMS: Drupal 9.5.9
- Database: MariaDB 10.5.16
- Web Server: Apache/2.4.53
Comments
It seems to me that the problem lies in UFMatch.php, in the synchronizeUFMatch() function, which doesn't retrieve the contact_id if it exists.
As a workaround, I propose replacing lines 252 and 253
$contactID = civicrm_api3('Contact', 'create', $contactParameters)['id'];
$ufmatch->contact_id = $contactID;
with
// If contactID exist, user exist and use it
if (isset($dedupeParameters['contact_id'])) {
$ufmatch->contact_id = $dedupeParameters['contact_id'];
}
else {
// Conatct does not exist, so create a new one
$contactID = civicrm_api3('Contact', 'create', $contactParameters)['id'];
$ufmatch->contact_id = $contactID;
}