3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 if (!$dao->find(TRUE)) {
57 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
63 * Given a UF user object, make sure there is a contact
64 * object for this user. If the user has new values, we need
65 * to update the CRM DB with the new values
68 * The drupal user object.
70 * Has the user object been edited.
74 * @param bool $isLogin
76 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
77 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
78 $session = CRM_Core_Session
::singleton();
79 if (!is_object($session)) {
80 CRM_Core_Error
::fatal('wow, session is not an object?');
84 $userSystemID = $userSystem->getBestUFID($user);
85 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
87 // if the id of the object is zero (true for anon users in drupal)
88 // have we already processed this user, if so early
90 $userID = $session->get('userID');
91 $ufID = $session->get('ufID');
93 if (!$update && $ufID == $userSystemID) {
97 //check do we have logged in user.
98 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
100 // reset the session if we are a different user
101 if ($ufID && $ufID != $userSystemID) {
104 //get logged in user ids, and set to session.
105 if ($isUserLoggedIn) {
106 $userIds = self
::getUFValues();
107 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
108 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
109 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
114 if ($userSystemID == 0) {
118 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
123 //make sure we have session w/ consistent ids.
124 $ufID = $ufmatch->uf_id
;
125 $userID = $ufmatch->contact_id
;
127 if ($isUserLoggedIn) {
128 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
129 //are we processing logged in user.
130 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
131 $userIds = self
::getUFValues($loggedInUserUfID);
132 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
133 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
134 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
138 //set user ids to session.
139 $session->set('ufID', $ufID);
140 $session->set('userID', $userID);
141 $session->set('ufUniqID', $ufUniqID);
143 // add current contact to recently viewed
144 if ($ufmatch->contact_id
) {
145 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl)
146 = CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
148 $otherRecent = array(
149 'imageUrl' => $contactImageUrl,
150 'subtype' => $contactSubtype,
151 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
154 CRM_Utils_Recent
::add($displayName,
155 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
156 $ufmatch->contact_id
,
158 $ufmatch->contact_id
,
166 * Synchronize the object with the UF Match entry. Can be called stand-alone from
167 * the drupalUsers script
169 * @param Object $user
170 * The drupal user object.
171 * @param string $userKey
172 * The id of the user from the uf object.
173 * @param string $uniqId
174 * The OpenID of the user.
176 * The name of the user framework.
178 * Returns the status if user created or already exits (used for CMS sync).
179 * @param string $ctype
181 * @param bool $isLogin
183 * @return CRM_Core_DAO_UFMatch|bool
185 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
186 $config = CRM_Core_Config
::singleton();
188 if (!CRM_Utils_Rule
::email($uniqId)) {
189 $retVal = $status ?
NULL : FALSE;
195 // make sure that a contact id exists for this user id
196 $ufmatch = new CRM_Core_DAO_UFMatch();
197 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
198 $ufmatch->uf_id
= $userKey;
200 if (!$ufmatch->find(TRUE)) {
201 $transaction = new CRM_Core_Transaction();
204 if (!empty($_POST) && !$isLogin) {
206 $params['email'] = $uniqId;
208 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, 'Individual', 'Unsupervised', array(), FALSE);
210 if (!empty($ids) && Civi
::settings()->get('uniq_email_per_site')) {
211 // restrict dupeIds to ones that belong to current domain/site.
212 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
213 foreach ($ids as $index => $dupeId) {
214 if (!in_array($dupeId, $siteContacts)) {
218 // re-index the array
219 $ids = array_values($ids);
222 $dao = new CRM_Core_DAO();
223 $dao->contact_id
= $ids[0];
227 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
232 // ensure there does not exists a contact_id / uf_id pair
233 // in the DB. This might be due to multiple emails per contact
237 FROM civicrm_uf_match
238 WHERE contact_id = %1
242 1 => array($dao->contact_id
, 'Integer'),
243 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
245 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
249 $ufmatch->contact_id
= $dao->contact_id
;
250 $ufmatch->uf_name
= $uniqId;
255 // Not sure why we're testing for this. Is there ever a case
256 // in which $user is not an object?
257 if (is_object($user)) {
258 if ($config->userSystem
->is_drupal
) {
259 $primary_email = $uniqId;
261 elseif ($uf == 'WordPress') {
262 $primary_email = $user->user_email
;
265 $primary_email = $user->email
;
267 $params = array('email-Primary' => $primary_email);
270 if ($ctype == 'Organization') {
271 $params['organization_name'] = $uniqId;
273 elseif ($ctype == 'Household') {
274 $params['household_name'] = $uniqId;
278 $ctype = "Individual";
280 $params['contact_type'] = $ctype;
282 // extract first / middle / last name
284 if ($uf == 'Joomla' && $user->name
) {
285 CRM_Utils_String
::extractName($user->name
, $params);
288 if ($uf == 'WordPress') {
289 if ($user->first_name
) {
290 $params['first_name'] = $user->first_name
;
293 if ($user->last_name
) {
294 $params['last_name'] = $user->last_name
;
298 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
299 $ufmatch->contact_id
= $contactId;
300 $ufmatch->uf_name
= $uniqId;
303 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
304 // user has two e-mails and there is a cms match for each of them
305 // the gets rid of the nasty fata error but still reports the error
308 FROM civicrm_uf_match
309 WHERE ( contact_id = %1
315 1 => array($ufmatch->contact_id
, 'Integer'),
316 2 => array($ufmatch->uf_name
, 'String'),
317 3 => array($ufmatch->uf_id
, 'Integer'),
318 4 => array($ufmatch->domain_id
, 'Integer'),
321 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
324 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
327 $transaction->commit();
330 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
332 1 => $ufmatch->contact_id
,
334 3 => $ufmatch->uf_id
,
351 * Update the uf_name in the user object.
353 * @param int $contactId
354 * Id of the contact to update.
356 public static function updateUFName($contactId) {
357 if (!Civi
::settings()->get('syncCMSEmail') ||
!$contactId) {
361 $config = CRM_Core_Config
::singleton();
362 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
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)) {
377 if ($ufmatch->uf_name
!= $ufName) {
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
396 // save the updated ufmatch object
397 $ufmatch->uf_name
= $ufName;
399 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
403 * Update the email value for the contact and user profile.
405 * @param int $contactId
406 * Contact ID of the user.
407 * @param string $emailAddress
408 * Email to be modified for the user.
410 public static function updateContactEmail($contactId, $emailAddress) {
411 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
412 $emailAddress = $strtolower($emailAddress);
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);
422 // If CMS integration is disabled skip Civi email update if CMS user email is changed
423 if (Civi
::settings()->get('syncCMSEmail') == FALSE) {
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);
432 if (trim($contactDetails[1])) {
433 $emailID = $contactDetails[3];
434 //update if record is found
435 $query = "UPDATE civicrm_email
439 1 => array($emailAddress, 'String'),
440 2 => array($emailID, 'Integer'),
442 $dao = CRM_Core_DAO
::executeQuery($query, $p);
445 //else insert a new email record
446 $email = new CRM_Core_DAO_Email();
447 $email->contact_id
= $contactId;
448 $email->is_primary
= 1;
449 $email->email
= $emailAddress;
451 $emailID = $email->id
;
454 CRM_Core_BAO_Log
::register($contactId,
462 * Delete the object records that are associated with this cms user.
465 * Id of the user to delete.
467 public static function deleteUser($ufID) {
468 $ufmatch = new CRM_Core_DAO_UFMatch();
470 $ufmatch->uf_id
= $ufID;
471 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
476 * Get the contact_id given a uf_id.
479 * Id of UF for which related contact_id is required.
482 * contact_id on success, null otherwise
484 public static function getContactId($ufID) {
489 $ufmatch = new CRM_Core_DAO_UFMatch();
491 $ufmatch->uf_id
= $ufID;
492 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
493 if ($ufmatch->find(TRUE)) {
494 return (int ) $ufmatch->contact_id
;
500 * Get the uf_id given a contact_id.
502 * @param int $contactID
503 * ID of the contact for which related uf_id is required.
506 * uf_id of the given contact_id on success, null otherwise
508 public static function getUFId($contactID) {
509 if (!isset($contactID)) {
512 $domain = CRM_Core_BAO_Domain
::getDomain();
513 $ufmatch = new CRM_Core_DAO_UFMatch();
515 $ufmatch->contact_id
= $contactID;
516 $ufmatch->domain_id
= $domain->id
;
517 if ($ufmatch->find(TRUE)) {
518 return $ufmatch->uf_id
;
526 public static function isEmptyTable() {
527 $sql = "SELECT count(id) FROM civicrm_uf_match";
528 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
532 * Get the list of contact_id.
536 * contact_id on success, null otherwise
538 public static function getContactIDs() {
540 $dao = new CRM_Core_DAO_UFMatch();
542 while ($dao->fetch()) {
543 $id[] = $dao->contact_id
;
549 * See if this user exists, and if so, if they're allowed to login
555 * true if allowed to login, false otherwise
557 public static function getAllowedToLogin($openId) {
558 $ufmatch = new CRM_Core_DAO_UFMatch();
559 $ufmatch->uf_name
= $openId;
560 $ufmatch->allowed_to_login
= 1;
561 if ($ufmatch->find(TRUE)) {
568 * Get the next unused uf_id value, since the standalone UF doesn't
569 * have id's (it uses OpenIDs, which go in a different field)
573 * next highest unused value for uf_id
575 public static function getNextUfIdValue() {
576 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
577 $dao = CRM_Core_DAO
::executeQuery($query);
579 $ufId = $dao->next_uf_id
;
593 public static function isDuplicateUser($email) {
594 $session = CRM_Core_Session
::singleton();
595 $contactID = $session->get('userID');
596 if (!empty($email) && isset($contactID)) {
597 $dao = new CRM_Core_DAO_UFMatch();
598 $dao->uf_name
= $email;
599 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
607 * Get uf match values for given uf id or logged in user.
615 public static function getUFValues($ufID = NULL) {
617 //get logged in user uf id.
618 $ufID = CRM_Utils_System
::getLoggedInUfID();
625 if ($ufID && !isset($ufValues[$ufID])) {
626 $ufmatch = new CRM_Core_DAO_UFMatch();
627 $ufmatch->uf_id
= $ufID;
628 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
629 if ($ufmatch->find(TRUE)) {
630 $ufValues[$ufID] = array(
631 'uf_id' => $ufmatch->uf_id
,
632 'uf_name' => $ufmatch->uf_name
,
633 'contact_id' => $ufmatch->contact_id
,
634 'domain_id' => $ufmatch->domain_id
,
638 return $ufValues[$ufID];
644 public function addSelectWhereClause() {
645 // Prevent default behavior of joining ACLs onto the contact_id field
647 CRM_Utils_Hook
::selectWhereClause($this, $clauses);