3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * The basic class that interfaces with the external user framework.
37 class CRM_Core_BAO_UFMatch
extends CRM_Core_DAO_UFMatch
{
40 * Create UF Match, Note that this function is here in it's simplest form @ the moment
44 * @return \CRM_Core_DAO_UFMatch
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();
52 $dao = new CRM_Core_DAO_UFMatch();
53 $dao->copyValues($params);
54 // Fixme: this function cannot update records
55 if (!$dao->find(TRUE)) {
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);
65 * Given a UF user object, make sure there is a contact
66 * object for this user. If the user has new values, we need
67 * to update the CRM DB with the new values
70 * The drupal user object.
72 * Has the user object been edited.
76 * @param bool $isLogin
78 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
79 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
80 $session = CRM_Core_Session
::singleton();
81 if (!is_object($session)) {
82 CRM_Core_Error
::fatal('wow, session is not an object?');
86 $userSystemID = $userSystem->getBestUFID($user);
87 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
89 // if the id of the object is zero (true for anon users in drupal)
90 // have we already processed this user, if so early
92 $userID = $session->get('userID');
93 $ufID = $session->get('ufID');
95 if (!$update && $ufID == $userSystemID) {
99 //check do we have logged in user.
100 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
102 // reset the session if we are a different user
103 if ($ufID && $ufID != $userSystemID) {
106 //get logged in user ids, and set to session.
107 if ($isUserLoggedIn) {
108 $userIds = self
::getUFValues();
109 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
110 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
111 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
116 if ($userSystemID == 0) {
120 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
125 //make sure we have session w/ consistent ids.
126 $ufID = $ufmatch->uf_id
;
127 $userID = $ufmatch->contact_id
;
129 if ($isUserLoggedIn) {
130 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
131 //are we processing logged in user.
132 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
133 $userIds = self
::getUFValues($loggedInUserUfID);
134 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
135 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
136 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
140 //set user ids to session.
141 $session->set('ufID', $ufID);
142 $session->set('userID', $userID);
143 $session->set('ufUniqID', $ufUniqID);
145 // add current contact to recently viewed
146 if ($ufmatch->contact_id
) {
147 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl)
148 = CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
150 $otherRecent = array(
151 'imageUrl' => $contactImageUrl,
152 'subtype' => $contactSubtype,
153 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
156 CRM_Utils_Recent
::add($displayName,
157 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
158 $ufmatch->contact_id
,
160 $ufmatch->contact_id
,
168 * Synchronize the object with the UF Match entry. Can be called stand-alone from
169 * the drupalUsers script
171 * @param Object $user
172 * The drupal user object.
173 * @param string $userKey
174 * The id of the user from the uf object.
175 * @param string $uniqId
176 * The OpenID of the user.
178 * The name of the user framework.
180 * Returns the status if user created or already exits (used for CMS sync).
181 * @param string $ctype
183 * @param bool $isLogin
185 * @return CRM_Core_DAO_UFMatch|bool
187 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
188 $config = CRM_Core_Config
::singleton();
190 if (!CRM_Utils_Rule
::email($uniqId)) {
191 $retVal = $status ?
NULL : FALSE;
197 // make sure that a contact id exists for this user id
198 $ufmatch = new CRM_Core_DAO_UFMatch();
199 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
200 $ufmatch->uf_id
= $userKey;
202 if (!$ufmatch->find(TRUE)) {
203 $transaction = new CRM_Core_Transaction();
206 if (!empty($_POST) && !$isLogin) {
208 $params['email'] = $uniqId;
210 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, 'Individual', 'Unsupervised', array(), FALSE);
212 if (!empty($ids) && Civi
::settings()->get('uniq_email_per_site')) {
213 // restrict dupeIds to ones that belong to current domain/site.
214 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
215 foreach ($ids as $index => $dupeId) {
216 if (!in_array($dupeId, $siteContacts)) {
220 // re-index the array
221 $ids = array_values($ids);
224 $dao = new CRM_Core_DAO();
225 $dao->contact_id
= $ids[0];
229 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
234 // ensure there does not exists a contact_id / uf_id pair
235 // in the DB. This might be due to multiple emails per contact
239 FROM civicrm_uf_match
240 WHERE contact_id = %1
244 1 => array($dao->contact_id
, 'Integer'),
245 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
247 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
251 $ufmatch->contact_id
= $dao->contact_id
;
252 $ufmatch->uf_name
= $uniqId;
257 // Not sure why we're testing for this. Is there ever a case
258 // in which $user is not an object?
259 if (is_object($user)) {
260 if ($config->userSystem
->is_drupal
) {
261 $primary_email = $uniqId;
263 elseif ($uf == 'WordPress') {
264 $primary_email = $user->user_email
;
267 $primary_email = $user->email
;
269 $params = array('email-Primary' => $primary_email);
272 if ($ctype == 'Organization') {
273 $params['organization_name'] = $uniqId;
275 elseif ($ctype == 'Household') {
276 $params['household_name'] = $uniqId;
280 $ctype = "Individual";
282 $params['contact_type'] = $ctype;
284 // extract first / middle / last name
286 if ($uf == 'Joomla' && $user->name
) {
287 CRM_Utils_String
::extractName($user->name
, $params);
290 if ($uf == 'WordPress') {
291 if ($user->first_name
) {
292 $params['first_name'] = $user->first_name
;
295 if ($user->last_name
) {
296 $params['last_name'] = $user->last_name
;
300 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
301 $ufmatch->contact_id
= $contactId;
302 $ufmatch->uf_name
= $uniqId;
305 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
306 // user has two e-mails and there is a cms match for each of them
307 // the gets rid of the nasty fata error but still reports the error
310 FROM civicrm_uf_match
311 WHERE ( contact_id = %1
317 1 => array($ufmatch->contact_id
, 'Integer'),
318 2 => array($ufmatch->uf_name
, 'String'),
319 3 => array($ufmatch->uf_id
, 'Integer'),
320 4 => array($ufmatch->domain_id
, 'Integer'),
323 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
326 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
329 $transaction->commit();
332 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
334 1 => $ufmatch->contact_id
,
336 3 => $ufmatch->uf_id
,
353 * Update the uf_name in the user object.
355 * @param int $contactId
356 * Id of the contact to update.
358 public static function updateUFName($contactId) {
359 if (!Civi
::settings()->get('syncCMSEmail') ||
!$contactId) {
363 $config = CRM_Core_Config
::singleton();
364 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
372 // 1.do check for contact Id.
373 $ufmatch = new CRM_Core_DAO_UFMatch();
374 $ufmatch->contact_id
= $contactId;
375 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
376 if (!$ufmatch->find(TRUE)) {
379 if ($ufmatch->uf_name
!= $ufName) {
384 // 2.do check for duplicate ufName.
385 $ufDupeName = new CRM_Core_DAO_UFMatch();
386 $ufDupeName->uf_name
= $ufName;
387 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
388 if ($ufDupeName->find(TRUE) &&
389 $ufDupeName->contact_id
!= $contactId
398 // save the updated ufmatch object
399 $ufmatch->uf_name
= $ufName;
401 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
405 * Update the email value for the contact and user profile.
407 * @param int $contactId
408 * Contact ID of the user.
409 * @param string $emailAddress
410 * Email to be modified for the user.
412 public static function updateContactEmail($contactId, $emailAddress) {
413 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
414 $emailAddress = $strtolower($emailAddress);
416 $ufmatch = new CRM_Core_DAO_UFMatch();
417 $ufmatch->contact_id
= $contactId;
418 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
419 if ($ufmatch->find(TRUE)) {
420 // Save the email in UF Match table
421 $ufmatch->uf_name
= $emailAddress;
422 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
424 // If CMS integration is disabled skip Civi email update if CMS user email is changed
425 if (Civi
::settings()->get('syncCMSEmail') == FALSE) {
429 //check if the primary email for the contact exists
430 //$contactDetails[1] - email
431 //$contactDetails[3] - email id
432 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
434 if (trim($contactDetails[1])) {
435 $emailID = $contactDetails[3];
436 //update if record is found
437 $query = "UPDATE civicrm_email
441 1 => array($emailAddress, 'String'),
442 2 => array($emailID, 'Integer'),
444 $dao = CRM_Core_DAO
::executeQuery($query, $p);
447 //else insert a new email record
448 $email = new CRM_Core_DAO_Email();
449 $email->contact_id
= $contactId;
450 $email->is_primary
= 1;
451 $email->email
= $emailAddress;
453 $emailID = $email->id
;
456 CRM_Core_BAO_Log
::register($contactId,
464 * Delete the object records that are associated with this cms user.
467 * Id of the user to delete.
469 public static function deleteUser($ufID) {
470 $ufmatch = new CRM_Core_DAO_UFMatch();
472 $ufmatch->uf_id
= $ufID;
473 $ufmatch->domain_id
= $domainId = CRM_Core_Config
::domainID();
477 Civi
::$statics[__CLASS__
][$domainId] = [];
481 * Get the contact_id given a uf_id.
484 * Id of UF for which related contact_id is required.
487 * contact_id on success, null otherwise
489 public static function getContactId($ufID) {
493 $domainId = CRM_Core_Config
::domainID();
495 if (!isset(Civi
::$statics[__CLASS__
][$domainId])) {
496 Civi
::$statics[__CLASS__
][$domainId] = [];
498 $contactId = array_search($ufID, Civi
::$statics[__CLASS__
][$domainId]);
502 $ufmatch = new CRM_Core_DAO_UFMatch();
503 $ufmatch->uf_id
= $ufID;
504 $ufmatch->domain_id
= $domainId;
505 if ($ufmatch->find(TRUE)) {
506 $contactId = (int) $ufmatch->contact_id
;
507 Civi
::$statics[__CLASS__
][$domainId][$contactId] = (int) $ufID;
514 * Get the uf_id given a contact_id.
516 * @param int $contactID
517 * ID of the contact for which related uf_id is required.
520 * uf_id of the given contact_id on success, null otherwise
522 public static function getUFId($contactID) {
526 $domainId = CRM_Core_Config
::domainID();
527 $contactID = (int) $contactID;
529 if (empty(Civi
::$statics[__CLASS__
][$domainId]) ||
!array_key_exists($contactID, Civi
::$statics[__CLASS__
][$domainId])) {
530 Civi
::$statics[__CLASS__
][$domainId][$contactID] = NULL;
531 $ufmatch = new CRM_Core_DAO_UFMatch();
532 $ufmatch->contact_id
= $contactID;
533 $ufmatch->domain_id
= $domainId;
534 if ($ufmatch->find(TRUE)) {
535 Civi
::$statics[__CLASS__
][$domainId][$contactID] = (int) $ufmatch->uf_id
;
538 return Civi
::$statics[__CLASS__
][$domainId][$contactID];
544 public static function isEmptyTable() {
545 $sql = "SELECT count(id) FROM civicrm_uf_match";
546 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
550 * Get the list of contact_id.
554 * contact_id on success, null otherwise
556 public static function getContactIDs() {
558 $dao = new CRM_Core_DAO_UFMatch();
560 while ($dao->fetch()) {
561 $id[] = $dao->contact_id
;
567 * See if this user exists, and if so, if they're allowed to login
573 * true if allowed to login, false otherwise
575 public static function getAllowedToLogin($openId) {
576 $ufmatch = new CRM_Core_DAO_UFMatch();
577 $ufmatch->uf_name
= $openId;
578 $ufmatch->allowed_to_login
= 1;
579 if ($ufmatch->find(TRUE)) {
586 * Get the next unused uf_id value, since the standalone UF doesn't
587 * have id's (it uses OpenIDs, which go in a different field)
591 * next highest unused value for uf_id
593 public static function getNextUfIdValue() {
594 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
595 $dao = CRM_Core_DAO
::executeQuery($query);
597 $ufId = $dao->next_uf_id
;
611 public static function isDuplicateUser($email) {
612 $session = CRM_Core_Session
::singleton();
613 $contactID = $session->get('userID');
614 if (!empty($email) && isset($contactID)) {
615 $dao = new CRM_Core_DAO_UFMatch();
616 $dao->uf_name
= $email;
617 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
625 * Get uf match values for given uf id or logged in user.
633 public static function getUFValues($ufID = NULL) {
635 //get logged in user uf id.
636 $ufID = CRM_Utils_System
::getLoggedInUfID();
643 if ($ufID && !isset($ufValues[$ufID])) {
644 $ufmatch = new CRM_Core_DAO_UFMatch();
645 $ufmatch->uf_id
= $ufID;
646 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
647 if ($ufmatch->find(TRUE)) {
648 $ufValues[$ufID] = array(
649 'uf_id' => $ufmatch->uf_id
,
650 'uf_name' => $ufmatch->uf_name
,
651 'contact_id' => $ufmatch->contact_id
,
652 'domain_id' => $ufmatch->domain_id
,
656 return $ufValues[$ufID];
662 public function addSelectWhereClause() {
663 // Prevent default behavior of joining ACLs onto the contact_id field
665 CRM_Utils_Hook
::selectWhereClause($this, $clauses);