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;
194 // dev/core#1858 Ensure that if we have a contactID parameter which is set in the Create user Record contact task form
195 // That this contacID value is passed through as the contact_id to the get duplicate contacts function. This is necessary because for Drupal 8 this function gets invoked
196 // Before the civicrm_uf_match record is added where as in D7 it isn't called until the user tries to actually login.
197 if (!empty($params['contactID'])) {
198 $params['contact_id'] = $params['contactID'];
201 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, 'Individual', 'Unsupervised', [], FALSE);
203 if (!empty($ids) && Civi
::settings()->get('uniq_email_per_site')) {
204 // restrict dupeIds to ones that belong to current domain/site.
205 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
206 foreach ($ids as $index => $dupeId) {
207 if (!in_array($dupeId, $siteContacts)) {
211 // re-index the array
212 $ids = array_values($ids);
215 $dao = new CRM_Core_DAO();
216 $dao->contact_id
= $ids[0];
220 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
225 // ensure there does not exists a contact_id / uf_id pair
226 // in the DB. This might be due to multiple emails per contact
230 FROM civicrm_uf_match
231 WHERE contact_id = %1
235 1 => [$dao->contact_id
, 'Integer'],
236 2 => [CRM_Core_Config
::domainID(), 'Integer'],
238 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
242 $ufmatch->contact_id
= $dao->contact_id
;
243 $ufmatch->uf_name
= $uniqId;
248 // Not sure why we're testing for this. Is there ever a case
249 // in which $user is not an object?
250 if (is_object($user)) {
251 if ($config->userSystem
->is_drupal
) {
252 $primary_email = $uniqId;
254 elseif ($uf == 'WordPress') {
255 $primary_email = $user->user_email
;
258 $primary_email = $user->email
;
260 $params = ['email-Primary' => $primary_email];
263 if ($ctype == 'Organization') {
264 $params['organization_name'] = $uniqId;
266 elseif ($ctype == 'Household') {
267 $params['household_name'] = $uniqId;
271 $ctype = "Individual";
273 $params['contact_type'] = $ctype;
275 // extract first / middle / last name
277 if ($uf == 'Joomla' && $user->name
) {
278 CRM_Utils_String
::extractName($user->name
, $params);
281 if ($uf == 'WordPress') {
282 if ($user->first_name
) {
283 $params['first_name'] = $user->first_name
;
286 if ($user->last_name
) {
287 $params['last_name'] = $user->last_name
;
291 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params);
292 $ufmatch->contact_id
= $contactId;
293 $ufmatch->uf_name
= $uniqId;
296 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
297 // user has two e-mails and there is a cms match for each of them
298 // the gets rid of the nasty fata error but still reports the error
301 FROM civicrm_uf_match
302 WHERE ( contact_id = %1
308 1 => [$ufmatch->contact_id
, 'Integer'],
309 2 => [$ufmatch->uf_name
, 'String'],
310 3 => [$ufmatch->uf_id
, 'Integer'],
311 4 => [$ufmatch->domain_id
, 'Integer'],
314 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
317 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
319 $transaction->commit();
322 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
324 1 => $ufmatch->contact_id
,
326 3 => $ufmatch->uf_id
,
343 * Update the uf_name in the user object.
345 * @param int $contactId
346 * Id of the contact to update.
348 public static function updateUFName($contactId) {
349 if (!Civi
::settings()->get('syncCMSEmail') ||
!$contactId) {
353 $config = CRM_Core_Config
::singleton();
354 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
362 // 1.do check for contact Id.
363 $ufmatch = new CRM_Core_DAO_UFMatch();
364 $ufmatch->contact_id
= $contactId;
365 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
366 if (!$ufmatch->find(TRUE)) {
369 if ($ufmatch->uf_name
!= $ufName) {
374 // 2.do check for duplicate ufName.
375 $ufDupeName = new CRM_Core_DAO_UFMatch();
376 $ufDupeName->uf_name
= $ufName;
377 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
378 if ($ufDupeName->find(TRUE) &&
379 $ufDupeName->contact_id
!= $contactId
388 // save the updated ufmatch object
389 $ufmatch->uf_name
= $ufName;
391 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
395 * Update the email value for the contact and user profile.
397 * @param int $contactId
398 * Contact ID of the user.
399 * @param string $emailAddress
400 * Email to be modified for the user.
402 public static function updateContactEmail($contactId, $emailAddress) {
403 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
404 $emailAddress = $strtolower($emailAddress);
406 $ufmatch = new CRM_Core_DAO_UFMatch();
407 $ufmatch->contact_id
= $contactId;
408 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
409 if ($ufmatch->find(TRUE)) {
410 // Save the email in UF Match table
411 $ufmatch->uf_name
= $emailAddress;
412 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
414 // If CMS integration is disabled skip Civi email update if CMS user email is changed
415 if (Civi
::settings()->get('syncCMSEmail') == FALSE) {
419 //check if the primary email for the contact exists
420 //$contactDetails[1] - email
421 //$contactDetails[3] - email id
422 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
424 if (trim($contactDetails[1])) {
425 //update if record is found but different
426 $emailID = $contactDetails[3];
427 if (trim($contactDetails[1]) != $emailAddress) {
428 civicrm_api3('Email', 'create', [
430 'email' => $emailAddress,
435 //else insert a new email record
436 $result = civicrm_api3('Email', 'create', [
437 'contact_id' => $contactId,
438 'email' => $emailAddress,
441 $emailID = $result['id'];
444 CRM_Core_BAO_Log
::register($contactId,
452 * Delete the object records that are associated with this cms user.
455 * Id of the user to delete.
457 public static function deleteUser($ufID) {
458 $ufmatch = new CRM_Core_DAO_UFMatch();
460 $ufmatch->uf_id
= $ufID;
461 $ufmatch->domain_id
= $domainId = CRM_Core_Config
::domainID();
465 Civi
::$statics[__CLASS__
][$domainId] = [];
469 * Get the contact_id given a uf_id.
472 * Id of UF for which related contact_id is required.
475 * contact_id on success, null otherwise
477 public static function getContactId($ufID) {
481 $domainId = CRM_Core_Config
::domainID();
483 if (!isset(Civi
::$statics[__CLASS__
][$domainId])) {
484 Civi
::$statics[__CLASS__
][$domainId] = [];
486 $contactId = array_search($ufID, Civi
::$statics[__CLASS__
][$domainId]);
490 $ufmatch = new CRM_Core_DAO_UFMatch();
491 $ufmatch->uf_id
= $ufID;
492 $ufmatch->domain_id
= $domainId;
493 if ($ufmatch->find(TRUE)) {
494 $contactId = (int) $ufmatch->contact_id
;
495 Civi
::$statics[__CLASS__
][$domainId][$contactId] = (int) $ufID;
502 * Get the uf_id given a contact_id.
504 * @param int $contactID
505 * ID of the contact for which related uf_id is required.
508 * uf_id of the given contact_id on success, null otherwise
510 public static function getUFId($contactID) {
514 $domainId = CRM_Core_Config
::domainID();
515 $contactID = (int) $contactID;
517 if (empty(Civi
::$statics[__CLASS__
][$domainId]) ||
!array_key_exists($contactID, Civi
::$statics[__CLASS__
][$domainId])) {
518 Civi
::$statics[__CLASS__
][$domainId][$contactID] = NULL;
519 $ufmatch = new CRM_Core_DAO_UFMatch();
520 $ufmatch->contact_id
= $contactID;
521 $ufmatch->domain_id
= $domainId;
522 if ($ufmatch->find(TRUE)) {
523 Civi
::$statics[__CLASS__
][$domainId][$contactID] = (int) $ufmatch->uf_id
;
526 return Civi
::$statics[__CLASS__
][$domainId][$contactID];
533 public static function isEmptyTable() {
534 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
535 $sql = "SELECT count(id) FROM civicrm_uf_match";
536 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
540 * Get the list of contact_id.
544 * contact_id on success, null otherwise
546 public static function getContactIDs() {
547 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
549 $dao = new CRM_Core_DAO_UFMatch();
551 while ($dao->fetch()) {
552 $id[] = $dao->contact_id
;
558 * See if this user exists, and if so, if they're allowed to login
564 * true if allowed to login, false otherwise
566 public static function getAllowedToLogin($openId) {
567 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
568 $ufmatch = new CRM_Core_DAO_UFMatch();
569 $ufmatch->uf_name
= $openId;
570 $ufmatch->allowed_to_login
= 1;
571 if ($ufmatch->find(TRUE)) {
578 * Get the next unused uf_id value, since the standalone UF doesn't
579 * have id's (it uses OpenIDs, which go in a different field)
583 * next highest unused value for uf_id
585 public static function getNextUfIdValue() {
586 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
587 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
588 $dao = CRM_Core_DAO
::executeQuery($query);
590 $ufId = $dao->next_uf_id
;
604 public static function isDuplicateUser($email) {
605 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
606 $session = CRM_Core_Session
::singleton();
607 $contactID = $session->get('userID');
608 if (!empty($email) && isset($contactID)) {
609 $dao = new CRM_Core_DAO_UFMatch();
610 $dao->uf_name
= $email;
611 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
619 * Get uf match values for given uf id or logged in user.
627 public static function getUFValues($ufID = NULL) {
629 //get logged in user uf id.
630 $ufID = CRM_Utils_System
::getLoggedInUfID();
637 if ($ufID && !isset($ufValues[$ufID])) {
638 $ufmatch = new CRM_Core_DAO_UFMatch();
639 $ufmatch->uf_id
= $ufID;
640 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
641 if ($ufmatch->find(TRUE)) {
643 'uf_id' => $ufmatch->uf_id
,
644 'uf_name' => $ufmatch->uf_name
,
645 'contact_id' => $ufmatch->contact_id
,
646 'domain_id' => $ufmatch->domain_id
,
650 return $ufValues[$ufID];
656 public function addSelectWhereClause() {
657 // Prevent default behavior of joining ACLs onto the contact_id field
659 CRM_Utils_Hook
::selectWhereClause($this, $clauses);