Merge pull request #14510 from totten/master-relax-get-to-it
[civicrm-core.git] / CRM / Core / BAO / UFMatch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * The basic class that interfaces with the external user framework.
36 */
37 class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
38
39 /**
40 * Create UF Match, Note that this function is here in it's simplest form @ the moment
41 *
42 * @param $params
43 *
44 * @return \CRM_Core_DAO_UFMatch
45 */
46 public static function create($params) {
47 $hook = empty($params['id']) ? 'create' : 'edit';
48 CRM_Utils_Hook::pre($hook, 'UFMatch', CRM_Utils_Array::value('id', $params), $params);
49 if (empty($params['domain_id'])) {
50 $params['domain_id'] = CRM_Core_Config::domainID();
51 }
52 $dao = new CRM_Core_DAO_UFMatch();
53 $dao->copyValues($params);
54 // Fixme: this function cannot update records
55 if (!$dao->find(TRUE)) {
56 $dao->save();
57 Civi::$statics[__CLASS__][$params['domain_id']][(int) $dao->contact_id] = (int) $dao->uf_id;
58 CRM_Utils_Hook::post($hook, 'UFMatch', $dao->id, $dao);
59 }
60 return $dao;
61 }
62
63 /**
64 * Given a UF user object, make sure there is a contact
65 * object for this user. If the user has new values, we need
66 * to update the CRM DB with the new values
67 *
68 * @param Object $user
69 * The drupal user object.
70 * @param bool $update
71 * Has the user object been edited.
72 * @param $uf
73 *
74 * @param $ctype
75 * @param bool $isLogin
76 */
77 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
78 $userSystem = CRM_Core_Config::singleton()->userSystem;
79 $session = CRM_Core_Session::singleton();
80 if (!is_object($session)) {
81 CRM_Core_Error::fatal('wow, session is not an object?');
82 return;
83 }
84
85 $userSystemID = $userSystem->getBestUFID($user);
86 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
87
88 // if the id of the object is zero (true for anon users in drupal)
89 // have we already processed this user, if so early
90 // return.
91 $userID = $session->get('userID');
92 $ufID = $session->get('ufID');
93
94 if (!$update && $ufID == $userSystemID) {
95 return;
96 }
97
98 //check do we have logged in user.
99 $isUserLoggedIn = CRM_Utils_System::isUserLoggedIn();
100
101 // reset the session if we are a different user
102 if ($ufID && $ufID != $userSystemID) {
103 $session->reset();
104
105 //get logged in user ids, and set to session.
106 if ($isUserLoggedIn) {
107 $userIds = self::getUFValues();
108 $session->set('ufID', CRM_Utils_Array::value('uf_id', $userIds, ''));
109 $session->set('userID', CRM_Utils_Array::value('contact_id', $userIds, ''));
110 $session->set('ufUniqID', CRM_Utils_Array::value('uf_name', $userIds, ''));
111 }
112 }
113
114 // return early
115 if ($userSystemID == 0) {
116 return;
117 }
118
119 $ufmatch = self::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
120 if (!$ufmatch) {
121 return;
122 }
123
124 //make sure we have session w/ consistent ids.
125 $ufID = $ufmatch->uf_id;
126 $userID = $ufmatch->contact_id;
127 $ufUniqID = '';
128 if ($isUserLoggedIn) {
129 $loggedInUserUfID = CRM_Utils_System::getLoggedInUfID();
130 //are we processing logged in user.
131 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
132 $userIds = self::getUFValues($loggedInUserUfID);
133 $ufID = CRM_Utils_Array::value('uf_id', $userIds, '');
134 $userID = CRM_Utils_Array::value('contact_id', $userIds, '');
135 $ufUniqID = CRM_Utils_Array::value('uf_name', $userIds, '');
136 }
137 }
138
139 //set user ids to session.
140 $session->set('ufID', $ufID);
141 $session->set('userID', $userID);
142 $session->set('ufUniqID', $ufUniqID);
143
144 // add current contact to recently viewed
145 if ($ufmatch->contact_id) {
146 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl)
147 = CRM_Contact_BAO_Contact::getDisplayAndImage($ufmatch->contact_id, TRUE, TRUE);
148
149 $otherRecent = [
150 'imageUrl' => $contactImageUrl,
151 'subtype' => $contactSubtype,
152 'editUrl' => CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
153 ];
154
155 CRM_Utils_Recent::add($displayName,
156 CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
157 $ufmatch->contact_id,
158 $contactType,
159 $ufmatch->contact_id,
160 $displayName,
161 $otherRecent
162 );
163 }
164 }
165
166 /**
167 * Synchronize the object with the UF Match entry. Can be called stand-alone from
168 * the drupalUsers script
169 *
170 * @param Object $user
171 * The drupal user object.
172 * @param string $userKey
173 * The id of the user from the uf object.
174 * @param string $uniqId
175 * The OpenID of the user.
176 * @param string $uf
177 * The name of the user framework.
178 * @param int $status
179 * Returns the status if user created or already exits (used for CMS sync).
180 * @param string $ctype
181 * contact type
182 * @param bool $isLogin
183 *
184 * @return CRM_Core_DAO_UFMatch|bool
185 */
186 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
187 $config = CRM_Core_Config::singleton();
188
189 if (!CRM_Utils_Rule::email($uniqId)) {
190 $retVal = $status ? NULL : FALSE;
191 return $retVal;
192 }
193
194 $newContact = FALSE;
195
196 // make sure that a contact id exists for this user id
197 $ufmatch = new CRM_Core_DAO_UFMatch();
198 $ufmatch->domain_id = CRM_Core_Config::domainID();
199 $ufmatch->uf_id = $userKey;
200
201 if (!$ufmatch->find(TRUE)) {
202 $transaction = new CRM_Core_Transaction();
203
204 $dao = NULL;
205 if (!empty($_POST) && !$isLogin) {
206 $params = $_POST;
207 $params['email'] = $uniqId;
208
209 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, 'Individual', 'Unsupervised', [], FALSE);
210
211 if (!empty($ids) && Civi::settings()->get('uniq_email_per_site')) {
212 // restrict dupeIds to ones that belong to current domain/site.
213 $siteContacts = CRM_Core_BAO_Domain::getContactList();
214 foreach ($ids as $index => $dupeId) {
215 if (!in_array($dupeId, $siteContacts)) {
216 unset($ids[$index]);
217 }
218 }
219 // re-index the array
220 $ids = array_values($ids);
221 }
222 if (!empty($ids)) {
223 $dao = new CRM_Core_DAO();
224 $dao->contact_id = $ids[0];
225 }
226 }
227 else {
228 $dao = CRM_Contact_BAO_Contact::matchContactOnEmail($uniqId, $ctype);
229 }
230
231 $found = FALSE;
232 if ($dao) {
233 // ensure there does not exists a contact_id / uf_id pair
234 // in the DB. This might be due to multiple emails per contact
235 // CRM-9091
236 $sql = "
237 SELECT id
238 FROM civicrm_uf_match
239 WHERE contact_id = %1
240 AND domain_id = %2
241 ";
242 $params = [
243 1 => [$dao->contact_id, 'Integer'],
244 2 => [CRM_Core_Config::domainID(), 'Integer'],
245 ];
246 $conflict = CRM_Core_DAO::singleValueQuery($sql, $params);
247
248 if (!$conflict) {
249 $found = TRUE;
250 $ufmatch->contact_id = $dao->contact_id;
251 $ufmatch->uf_name = $uniqId;
252 }
253 }
254
255 if (!$found) {
256 // Not sure why we're testing for this. Is there ever a case
257 // in which $user is not an object?
258 if (is_object($user)) {
259 if ($config->userSystem->is_drupal) {
260 $primary_email = $uniqId;
261 }
262 elseif ($uf == 'WordPress') {
263 $primary_email = $user->user_email;
264 }
265 else {
266 $primary_email = $user->email;
267 }
268 $params = ['email-Primary' => $primary_email];
269 }
270
271 if ($ctype == 'Organization') {
272 $params['organization_name'] = $uniqId;
273 }
274 elseif ($ctype == 'Household') {
275 $params['household_name'] = $uniqId;
276 }
277
278 if (!$ctype) {
279 $ctype = "Individual";
280 }
281 $params['contact_type'] = $ctype;
282
283 // extract first / middle / last name
284 // for joomla
285 if ($uf == 'Joomla' && $user->name) {
286 CRM_Utils_String::extractName($user->name, $params);
287 }
288
289 if ($uf == 'WordPress') {
290 if ($user->first_name) {
291 $params['first_name'] = $user->first_name;
292 }
293
294 if ($user->last_name) {
295 $params['last_name'] = $user->last_name;
296 }
297 }
298
299 $contactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray);
300 $ufmatch->contact_id = $contactId;
301 $ufmatch->uf_name = $uniqId;
302 }
303
304 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
305 // user has two e-mails and there is a cms match for each of them
306 // the gets rid of the nasty fata error but still reports the error
307 $sql = "
308 SELECT uf_id
309 FROM civicrm_uf_match
310 WHERE ( contact_id = %1
311 OR uf_name = %2
312 OR uf_id = %3 )
313 AND domain_id = %4
314 ";
315 $params = [
316 1 => [$ufmatch->contact_id, 'Integer'],
317 2 => [$ufmatch->uf_name, 'String'],
318 3 => [$ufmatch->uf_id, 'Integer'],
319 4 => [$ufmatch->domain_id, 'Integer'],
320 ];
321
322 $conflict = CRM_Core_DAO::singleValueQuery($sql, $params);
323
324 if (!$conflict) {
325 $ufmatch = CRM_Core_BAO_UFMatch::create((array) $ufmatch);
326 $newContact = TRUE;
327 $transaction->commit();
328 }
329 else {
330 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
331 [
332 1 => $ufmatch->contact_id,
333 2 => $uf,
334 3 => $ufmatch->uf_id,
335 4 => $conflict,
336 ]
337 );
338 unset($conflict);
339 }
340 }
341
342 if ($status) {
343 return $newContact;
344 }
345 else {
346 return $ufmatch;
347 }
348 }
349
350 /**
351 * Update the uf_name in the user object.
352 *
353 * @param int $contactId
354 * Id of the contact to update.
355 */
356 public static function updateUFName($contactId) {
357 if (!Civi::settings()->get('syncCMSEmail') || !$contactId) {
358 return;
359 }
360
361 $config = CRM_Core_Config::singleton();
362 $ufName = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId);
363
364 if (!$ufName) {
365 return;
366 }
367
368 $update = FALSE;
369
370 // 1.do check for contact Id.
371 $ufmatch = new CRM_Core_DAO_UFMatch();
372 $ufmatch->contact_id = $contactId;
373 $ufmatch->domain_id = CRM_Core_Config::domainID();
374 if (!$ufmatch->find(TRUE)) {
375 return;
376 }
377 if ($ufmatch->uf_name != $ufName) {
378 $update = TRUE;
379 }
380
381 // CRM-6928
382 // 2.do check for duplicate ufName.
383 $ufDupeName = new CRM_Core_DAO_UFMatch();
384 $ufDupeName->uf_name = $ufName;
385 $ufDupeName->domain_id = CRM_Core_Config::domainID();
386 if ($ufDupeName->find(TRUE) &&
387 $ufDupeName->contact_id != $contactId
388 ) {
389 $update = FALSE;
390 }
391
392 if (!$update) {
393 return;
394 }
395
396 // save the updated ufmatch object
397 $ufmatch->uf_name = $ufName;
398 $ufmatch->save();
399 $config->userSystem->updateCMSName($ufmatch->uf_id, $ufName);
400 }
401
402 /**
403 * Update the email value for the contact and user profile.
404 *
405 * @param int $contactId
406 * Contact ID of the user.
407 * @param string $emailAddress
408 * Email to be modified for the user.
409 */
410 public static function updateContactEmail($contactId, $emailAddress) {
411 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
412 $emailAddress = $strtolower($emailAddress);
413
414 $ufmatch = new CRM_Core_DAO_UFMatch();
415 $ufmatch->contact_id = $contactId;
416 $ufmatch->domain_id = CRM_Core_Config::domainID();
417 if ($ufmatch->find(TRUE)) {
418 // Save the email in UF Match table
419 $ufmatch->uf_name = $emailAddress;
420 CRM_Core_BAO_UFMatch::create((array) $ufmatch);
421
422 // If CMS integration is disabled skip Civi email update if CMS user email is changed
423 if (Civi::settings()->get('syncCMSEmail') == FALSE) {
424 return;
425 }
426
427 //check if the primary email for the contact exists
428 //$contactDetails[1] - email
429 //$contactDetails[3] - email id
430 $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId);
431
432 if (trim($contactDetails[1])) {
433 //update if record is found but different
434 $emailID = $contactDetails[3];
435 if (trim($contactDetails[1]) != $emailAddress) {
436 civicrm_api3('Email', 'create', [
437 'id' => $emailID,
438 'email' => $emailAddress,
439 ]);
440 }
441 }
442 else {
443 //else insert a new email record
444 $result = civicrm_api3('Email', 'create', [
445 'contact_id' => $contactId,
446 'email' => $emailAddress,
447 'is_primary' => 1,
448 ]);
449 $emailID = $result->id;
450 }
451
452 CRM_Core_BAO_Log::register($contactId,
453 'civicrm_email',
454 $emailID
455 );
456 }
457 }
458
459 /**
460 * Delete the object records that are associated with this cms user.
461 *
462 * @param int $ufID
463 * Id of the user to delete.
464 */
465 public static function deleteUser($ufID) {
466 $ufmatch = new CRM_Core_DAO_UFMatch();
467
468 $ufmatch->uf_id = $ufID;
469 $ufmatch->domain_id = $domainId = CRM_Core_Config::domainID();
470 $ufmatch->delete();
471
472 // Flush cache
473 Civi::$statics[__CLASS__][$domainId] = [];
474 }
475
476 /**
477 * Get the contact_id given a uf_id.
478 *
479 * @param int $ufID
480 * Id of UF for which related contact_id is required.
481 *
482 * @return int|null
483 * contact_id on success, null otherwise
484 */
485 public static function getContactId($ufID) {
486 if (!$ufID) {
487 return NULL;
488 }
489 $domainId = CRM_Core_Config::domainID();
490
491 if (!isset(Civi::$statics[__CLASS__][$domainId])) {
492 Civi::$statics[__CLASS__][$domainId] = [];
493 }
494 $contactId = array_search($ufID, Civi::$statics[__CLASS__][$domainId]);
495 if ($contactId) {
496 return $contactId;
497 }
498 $ufmatch = new CRM_Core_DAO_UFMatch();
499 $ufmatch->uf_id = $ufID;
500 $ufmatch->domain_id = $domainId;
501 if ($ufmatch->find(TRUE)) {
502 $contactId = (int) $ufmatch->contact_id;
503 Civi::$statics[__CLASS__][$domainId][$contactId] = (int) $ufID;
504 return $contactId;
505 }
506 return NULL;
507 }
508
509 /**
510 * Get the uf_id given a contact_id.
511 *
512 * @param int $contactID
513 * ID of the contact for which related uf_id is required.
514 *
515 * @return int|null
516 * uf_id of the given contact_id on success, null otherwise
517 */
518 public static function getUFId($contactID) {
519 if (!$contactID) {
520 return NULL;
521 }
522 $domainId = CRM_Core_Config::domainID();
523 $contactID = (int) $contactID;
524
525 if (empty(Civi::$statics[__CLASS__][$domainId]) || !array_key_exists($contactID, Civi::$statics[__CLASS__][$domainId])) {
526 Civi::$statics[__CLASS__][$domainId][$contactID] = NULL;
527 $ufmatch = new CRM_Core_DAO_UFMatch();
528 $ufmatch->contact_id = $contactID;
529 $ufmatch->domain_id = $domainId;
530 if ($ufmatch->find(TRUE)) {
531 Civi::$statics[__CLASS__][$domainId][$contactID] = (int) $ufmatch->uf_id;
532 }
533 }
534 return Civi::$statics[__CLASS__][$domainId][$contactID];
535 }
536
537 /**
538 * @return bool
539 */
540 public static function isEmptyTable() {
541 $sql = "SELECT count(id) FROM civicrm_uf_match";
542 return CRM_Core_DAO::singleValueQuery($sql) > 0 ? FALSE : TRUE;
543 }
544
545 /**
546 * Get the list of contact_id.
547 *
548 *
549 * @return int
550 * contact_id on success, null otherwise
551 */
552 public static function getContactIDs() {
553 $id = [];
554 $dao = new CRM_Core_DAO_UFMatch();
555 $dao->find();
556 while ($dao->fetch()) {
557 $id[] = $dao->contact_id;
558 }
559 return $id;
560 }
561
562 /**
563 * See if this user exists, and if so, if they're allowed to login
564 *
565 *
566 * @param int $openId
567 *
568 * @return bool
569 * true if allowed to login, false otherwise
570 */
571 public static function getAllowedToLogin($openId) {
572 $ufmatch = new CRM_Core_DAO_UFMatch();
573 $ufmatch->uf_name = $openId;
574 $ufmatch->allowed_to_login = 1;
575 if ($ufmatch->find(TRUE)) {
576 return TRUE;
577 }
578 return FALSE;
579 }
580
581 /**
582 * Get the next unused uf_id value, since the standalone UF doesn't
583 * have id's (it uses OpenIDs, which go in a different field)
584 *
585 *
586 * @return int
587 * next highest unused value for uf_id
588 */
589 public static function getNextUfIdValue() {
590 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
591 $dao = CRM_Core_DAO::executeQuery($query);
592 if ($dao->fetch()) {
593 $ufId = $dao->next_uf_id;
594 }
595
596 if (!isset($ufId)) {
597 $ufId = 1;
598 }
599 return $ufId;
600 }
601
602 /**
603 * @param $email
604 *
605 * @return bool
606 */
607 public static function isDuplicateUser($email) {
608 $session = CRM_Core_Session::singleton();
609 $contactID = $session->get('userID');
610 if (!empty($email) && isset($contactID)) {
611 $dao = new CRM_Core_DAO_UFMatch();
612 $dao->uf_name = $email;
613 if ($dao->find(TRUE) && $contactID != $dao->contact_id) {
614 return TRUE;
615 }
616 }
617 return FALSE;
618 }
619
620 /**
621 * Get uf match values for given uf id or logged in user.
622 *
623 * @param int $ufID
624 * Uf id.
625 *
626 * @return array
627 * uf values.
628 */
629 public static function getUFValues($ufID = NULL) {
630 if (!$ufID) {
631 //get logged in user uf id.
632 $ufID = CRM_Utils_System::getLoggedInUfID();
633 }
634 if (!$ufID) {
635 return [];
636 }
637
638 static $ufValues;
639 if ($ufID && !isset($ufValues[$ufID])) {
640 $ufmatch = new CRM_Core_DAO_UFMatch();
641 $ufmatch->uf_id = $ufID;
642 $ufmatch->domain_id = CRM_Core_Config::domainID();
643 if ($ufmatch->find(TRUE)) {
644 $ufValues[$ufID] = [
645 'uf_id' => $ufmatch->uf_id,
646 'uf_name' => $ufmatch->uf_name,
647 'contact_id' => $ufmatch->contact_id,
648 'domain_id' => $ufmatch->domain_id,
649 ];
650 }
651 }
652 return $ufValues[$ufID];
653 }
654
655 /**
656 * @inheritDoc
657 */
658 public function addSelectWhereClause() {
659 // Prevent default behavior of joining ACLs onto the contact_id field
660 $clauses = [];
661 CRM_Utils_Hook::selectWhereClause($this, $clauses);
662 return $clauses;
663 }
664
665 }