3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * The basic class that interfaces with the external user framework.
21 class CRM_Core_BAO_UFMatch
extends CRM_Core_DAO_UFMatch
{
24 * Create UF Match, Note that this function is here in it's simplest form @ the moment
28 * @return \CRM_Core_DAO_UFMatch
30 public static function create($params) {
31 $hook = empty($params['id']) ?
'create' : 'edit';
32 CRM_Utils_Hook
::pre($hook, 'UFMatch', CRM_Utils_Array
::value('id', $params), $params);
33 if (empty($params['domain_id'])) {
34 $params['domain_id'] = CRM_Core_Config
::domainID();
36 $dao = new CRM_Core_DAO_UFMatch();
37 $dao->copyValues($params);
38 // Fixme: this function cannot update records
39 if (!$dao->find(TRUE)) {
41 Civi
::$statics[__CLASS__
][$params['domain_id']][(int) $dao->contact_id
] = (int) $dao->uf_id
;
42 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
48 * Given a UF user object, make sure there is a contact
49 * object for this user. If the user has new values, we need
50 * to update the CRM DB with the new values
53 * The drupal user object.
55 * Has the user object been edited.
59 * @param bool $isLogin
61 * @throws CRM_Core_Exception
63 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
64 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
65 $session = CRM_Core_Session
::singleton();
66 if (!is_object($session)) {
67 throw new CRM_Core_Exception('wow, session is not an object?');
71 $userSystemID = $userSystem->getBestUFID($user);
72 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
74 // if the id of the object is zero (true for anon users in drupal)
75 // have we already processed this user, if so early
77 $userID = $session->get('userID');
78 $ufID = $session->get('ufID');
80 if (!$update && $ufID == $userSystemID) {
84 //check do we have logged in user.
85 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
87 // reset the session if we are a different user
88 if ($ufID && $ufID != $userSystemID) {
91 //get logged in user ids, and set to session.
92 if ($isUserLoggedIn) {
93 $userIds = self
::getUFValues();
94 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
95 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
96 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
101 if ($userSystemID == 0) {
105 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
110 //make sure we have session w/ consistent ids.
111 $ufID = $ufmatch->uf_id
;
112 $userID = $ufmatch->contact_id
;
114 if ($isUserLoggedIn) {
115 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
116 //are we processing logged in user.
117 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
118 $userIds = self
::getUFValues($loggedInUserUfID);
119 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
120 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
121 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
125 //set user ids to session.
126 $session->set('ufID', $ufID);
127 $session->set('userID', $userID);
128 $session->set('ufUniqID', $ufUniqID);
130 // add current contact to recently viewed
131 if ($ufmatch->contact_id
) {
132 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl)
133 = CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
136 'imageUrl' => $contactImageUrl,
137 'subtype' => $contactSubtype,
138 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
141 CRM_Utils_Recent
::add($displayName,
142 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
143 $ufmatch->contact_id
,
145 $ufmatch->contact_id
,
153 * Synchronize the object with the UF Match entry. Can be called stand-alone from
154 * the drupalUsers script
156 * @param Object $user
157 * The drupal user object.
158 * @param string $userKey
159 * The id of the user from the uf object.
160 * @param string $uniqId
161 * The OpenID of the user.
163 * The name of the user framework.
165 * Returns the status if user created or already exits (used for CMS sync).
166 * @param string $ctype
168 * @param bool $isLogin
170 * @return CRM_Core_DAO_UFMatch|bool
172 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
173 $config = CRM_Core_Config
::singleton();
175 if (!CRM_Utils_Rule
::email($uniqId)) {
176 $retVal = $status ?
NULL : FALSE;
182 // make sure that a contact id exists for this user id
183 $ufmatch = new CRM_Core_DAO_UFMatch();
184 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
185 $ufmatch->uf_id
= $userKey;
187 if (!$ufmatch->find(TRUE)) {
188 $transaction = new CRM_Core_Transaction();
191 if (!empty($_POST) && !$isLogin) {
193 $params['email'] = $uniqId;
195 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, 'Individual', 'Unsupervised', [], FALSE);
197 if (!empty($ids) && Civi
::settings()->get('uniq_email_per_site')) {
198 // restrict dupeIds to ones that belong to current domain/site.
199 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
200 foreach ($ids as $index => $dupeId) {
201 if (!in_array($dupeId, $siteContacts)) {
205 // re-index the array
206 $ids = array_values($ids);
209 $dao = new CRM_Core_DAO();
210 $dao->contact_id
= $ids[0];
214 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
219 // ensure there does not exists a contact_id / uf_id pair
220 // in the DB. This might be due to multiple emails per contact
224 FROM civicrm_uf_match
225 WHERE contact_id = %1
229 1 => [$dao->contact_id
, 'Integer'],
230 2 => [CRM_Core_Config
::domainID(), 'Integer'],
232 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
236 $ufmatch->contact_id
= $dao->contact_id
;
237 $ufmatch->uf_name
= $uniqId;
242 // Not sure why we're testing for this. Is there ever a case
243 // in which $user is not an object?
244 if (is_object($user)) {
245 if ($config->userSystem
->is_drupal
) {
246 $primary_email = $uniqId;
248 elseif ($uf == 'WordPress') {
249 $primary_email = $user->user_email
;
252 $primary_email = $user->email
;
254 $params = ['email-Primary' => $primary_email];
257 if ($ctype == 'Organization') {
258 $params['organization_name'] = $uniqId;
260 elseif ($ctype == 'Household') {
261 $params['household_name'] = $uniqId;
265 $ctype = "Individual";
267 $params['contact_type'] = $ctype;
269 // extract first / middle / last name
271 if ($uf == 'Joomla' && $user->name
) {
272 CRM_Utils_String
::extractName($user->name
, $params);
275 if ($uf == 'WordPress') {
276 if ($user->first_name
) {
277 $params['first_name'] = $user->first_name
;
280 if ($user->last_name
) {
281 $params['last_name'] = $user->last_name
;
285 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params);
286 $ufmatch->contact_id
= $contactId;
287 $ufmatch->uf_name
= $uniqId;
290 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
291 // user has two e-mails and there is a cms match for each of them
292 // the gets rid of the nasty fata error but still reports the error
295 FROM civicrm_uf_match
296 WHERE ( contact_id = %1
302 1 => [$ufmatch->contact_id
, 'Integer'],
303 2 => [$ufmatch->uf_name
, 'String'],
304 3 => [$ufmatch->uf_id
, 'Integer'],
305 4 => [$ufmatch->domain_id
, 'Integer'],
308 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
311 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
313 $transaction->commit();
316 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
318 1 => $ufmatch->contact_id
,
320 3 => $ufmatch->uf_id
,
337 * Update the uf_name in the user object.
339 * @param int $contactId
340 * Id of the contact to update.
342 public static function updateUFName($contactId) {
343 if (!Civi
::settings()->get('syncCMSEmail') ||
!$contactId) {
347 $config = CRM_Core_Config
::singleton();
348 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
356 // 1.do check for contact Id.
357 $ufmatch = new CRM_Core_DAO_UFMatch();
358 $ufmatch->contact_id
= $contactId;
359 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
360 if (!$ufmatch->find(TRUE)) {
363 if ($ufmatch->uf_name
!= $ufName) {
368 // 2.do check for duplicate ufName.
369 $ufDupeName = new CRM_Core_DAO_UFMatch();
370 $ufDupeName->uf_name
= $ufName;
371 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
372 if ($ufDupeName->find(TRUE) &&
373 $ufDupeName->contact_id
!= $contactId
382 // save the updated ufmatch object
383 $ufmatch->uf_name
= $ufName;
385 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
389 * Update the email value for the contact and user profile.
391 * @param int $contactId
392 * Contact ID of the user.
393 * @param string $emailAddress
394 * Email to be modified for the user.
396 public static function updateContactEmail($contactId, $emailAddress) {
397 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
398 $emailAddress = $strtolower($emailAddress);
400 $ufmatch = new CRM_Core_DAO_UFMatch();
401 $ufmatch->contact_id
= $contactId;
402 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
403 if ($ufmatch->find(TRUE)) {
404 // Save the email in UF Match table
405 $ufmatch->uf_name
= $emailAddress;
406 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
408 // If CMS integration is disabled skip Civi email update if CMS user email is changed
409 if (Civi
::settings()->get('syncCMSEmail') == FALSE) {
413 //check if the primary email for the contact exists
414 //$contactDetails[1] - email
415 //$contactDetails[3] - email id
416 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
418 if (trim($contactDetails[1])) {
419 //update if record is found but different
420 $emailID = $contactDetails[3];
421 if (trim($contactDetails[1]) != $emailAddress) {
422 civicrm_api3('Email', 'create', [
424 'email' => $emailAddress,
429 //else insert a new email record
430 $result = civicrm_api3('Email', 'create', [
431 'contact_id' => $contactId,
432 'email' => $emailAddress,
435 $emailID = $result['id'];
438 CRM_Core_BAO_Log
::register($contactId,
446 * Delete the object records that are associated with this cms user.
449 * Id of the user to delete.
451 public static function deleteUser($ufID) {
452 $ufmatch = new CRM_Core_DAO_UFMatch();
454 $ufmatch->uf_id
= $ufID;
455 $ufmatch->domain_id
= $domainId = CRM_Core_Config
::domainID();
459 Civi
::$statics[__CLASS__
][$domainId] = [];
463 * Get the contact_id given a uf_id.
466 * Id of UF for which related contact_id is required.
469 * contact_id on success, null otherwise
471 public static function getContactId($ufID) {
475 $domainId = CRM_Core_Config
::domainID();
477 if (!isset(Civi
::$statics[__CLASS__
][$domainId])) {
478 Civi
::$statics[__CLASS__
][$domainId] = [];
480 $contactId = array_search($ufID, Civi
::$statics[__CLASS__
][$domainId]);
484 $ufmatch = new CRM_Core_DAO_UFMatch();
485 $ufmatch->uf_id
= $ufID;
486 $ufmatch->domain_id
= $domainId;
487 if ($ufmatch->find(TRUE)) {
488 $contactId = (int) $ufmatch->contact_id
;
489 Civi
::$statics[__CLASS__
][$domainId][$contactId] = (int) $ufID;
496 * Get the uf_id given a contact_id.
498 * @param int $contactID
499 * ID of the contact for which related uf_id is required.
502 * uf_id of the given contact_id on success, null otherwise
504 public static function getUFId($contactID) {
508 $domainId = CRM_Core_Config
::domainID();
509 $contactID = (int) $contactID;
511 if (empty(Civi
::$statics[__CLASS__
][$domainId]) ||
!array_key_exists($contactID, Civi
::$statics[__CLASS__
][$domainId])) {
512 Civi
::$statics[__CLASS__
][$domainId][$contactID] = NULL;
513 $ufmatch = new CRM_Core_DAO_UFMatch();
514 $ufmatch->contact_id
= $contactID;
515 $ufmatch->domain_id
= $domainId;
516 if ($ufmatch->find(TRUE)) {
517 Civi
::$statics[__CLASS__
][$domainId][$contactID] = (int) $ufmatch->uf_id
;
520 return Civi
::$statics[__CLASS__
][$domainId][$contactID];
527 public static function isEmptyTable() {
528 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
529 $sql = "SELECT count(id) FROM civicrm_uf_match";
530 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
534 * Get the list of contact_id.
538 * contact_id on success, null otherwise
540 public static function getContactIDs() {
541 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
543 $dao = new CRM_Core_DAO_UFMatch();
545 while ($dao->fetch()) {
546 $id[] = $dao->contact_id
;
552 * See if this user exists, and if so, if they're allowed to login
558 * true if allowed to login, false otherwise
560 public static function getAllowedToLogin($openId) {
561 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
562 $ufmatch = new CRM_Core_DAO_UFMatch();
563 $ufmatch->uf_name
= $openId;
564 $ufmatch->allowed_to_login
= 1;
565 if ($ufmatch->find(TRUE)) {
572 * Get the next unused uf_id value, since the standalone UF doesn't
573 * have id's (it uses OpenIDs, which go in a different field)
577 * next highest unused value for uf_id
579 public static function getNextUfIdValue() {
580 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
581 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
582 $dao = CRM_Core_DAO
::executeQuery($query);
584 $ufId = $dao->next_uf_id
;
598 public static function isDuplicateUser($email) {
599 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
600 $session = CRM_Core_Session
::singleton();
601 $contactID = $session->get('userID');
602 if (!empty($email) && isset($contactID)) {
603 $dao = new CRM_Core_DAO_UFMatch();
604 $dao->uf_name
= $email;
605 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
613 * Get uf match values for given uf id or logged in user.
621 public static function getUFValues($ufID = NULL) {
623 //get logged in user uf id.
624 $ufID = CRM_Utils_System
::getLoggedInUfID();
631 if ($ufID && !isset($ufValues[$ufID])) {
632 $ufmatch = new CRM_Core_DAO_UFMatch();
633 $ufmatch->uf_id
= $ufID;
634 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
635 if ($ufmatch->find(TRUE)) {
637 'uf_id' => $ufmatch->uf_id
,
638 'uf_name' => $ufmatch->uf_name
,
639 'contact_id' => $ufmatch->contact_id
,
640 'domain_id' => $ufmatch->domain_id
,
644 return $ufValues[$ufID];
650 public function addSelectWhereClause() {
651 // Prevent default behavior of joining ACLs onto the contact_id field
653 CRM_Utils_Hook
::selectWhereClause($this, $clauses);