3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * The basic class that interfaces with the external user framework
39 class CRM_Core_BAO_UFMatch
extends CRM_Core_DAO_UFMatch
{
42 * Create UF Match, Note that thsi function is here in it's simplest form @ the moment
45 * @param array $params input parameters
47 static function create($params) {
48 $hook = empty($params['id']) ?
'create' : 'edit';
49 CRM_Utils_Hook
::pre($hook, 'UFMatch', CRM_Utils_Array
::value('id', $params), $params);
50 if(empty($params['domain_id'])) {
51 $params['domain_id'] = CRM_Core_Config
::domainID();
53 $dao = new CRM_Core_DAO_UFMatch();
54 $dao->copyValues($params);
56 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
62 * Given a UF user object, make sure there is a contact
63 * object for this user. If the user has new values, we need
64 * to update the CRM DB with the new values
66 * @param Object $user the drupal user object
67 * @param boolean $update has the user object been edited
74 static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
75 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
76 $session = CRM_Core_Session
::singleton();
77 if (!is_object($session)) {
78 CRM_Core_Error
::fatal('wow, session is not an object?');
82 $userSystemID = $userSystem->getBestUFID($user);
83 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
85 // if the id of the object is zero (true for anon users in drupal)
86 // have we already processed this user, if so early
88 $userID = $session->get('userID');
89 $ufID = $session->get('ufID');
91 if (!$update && $ufID == $userSystemID) {
95 //check do we have logged in user.
96 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
98 // reset the session if we are a different user
99 if ($ufID && $ufID != $userSystemID) {
102 //get logged in user ids, and set to session.
103 if ($isUserLoggedIn) {
104 $userIds = self
::getUFValues();
105 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
106 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
107 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
112 if ($userSystemID == 0) {
116 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
121 //make sure we have session w/ consistent ids.
122 $ufID = $ufmatch->uf_id
;
123 $userID = $ufmatch->contact_id
;
125 if ($isUserLoggedIn) {
126 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
127 //are we processing logged in user.
128 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
129 $userIds = self
::getUFValues($loggedInUserUfID);
130 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
131 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
132 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
136 //set user ids to session.
137 $session->set('ufID', $ufID);
138 $session->set('userID', $userID);
139 $session->set('ufUniqID', $ufUniqID);
141 // add current contact to recently viewed
142 if ($ufmatch->contact_id
) {
143 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) =
144 CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
146 $otherRecent = array(
147 'imageUrl' => $contactImageUrl,
148 'subtype' => $contactSubtype,
149 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
152 CRM_Utils_Recent
::add($displayName,
153 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
154 $ufmatch->contact_id
,
156 $ufmatch->contact_id
,
164 * Synchronize the object with the UF Match entry. Can be called stand-alone from
165 * the drupalUsers script
167 * @param Object $user the drupal user object
168 * @param string $userKey the id of the user from the uf object
169 * @param string $uniqId the OpenID of the user
170 * @param string $uf the name of the user framework
171 * @param integer $status returns the status if user created or already exits (used for CMS sync)
173 * @return the ufmatch object that was found or created
177 static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
178 $config = CRM_Core_Config
::singleton();
180 if (!CRM_Utils_Rule
::email($uniqId)) {
181 $retVal = $status ?
NULL : FALSE;
187 // make sure that a contact id exists for this user id
188 $ufmatch = new CRM_Core_DAO_UFMatch();
189 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
190 $ufmatch->uf_id
= $userKey;
192 if (!$ufmatch->find(TRUE)) {
193 $transaction = new CRM_Core_Transaction();
196 if (!empty($_POST) && !$isLogin) {
198 $params['email'] = $uniqId;
200 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
201 $dedupeParams['check_permission'] = FALSE;
202 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
205 CRM_Core_BAO_Setting
::getItem(
206 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
207 'uniq_email_per_site'
210 // restrict dupeIds to ones that belong to current domain/site.
211 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
212 foreach ($ids as $index => $dupeId) {
213 if (!in_array($dupeId, $siteContacts)) {
217 // re-index the array
218 $ids = array_values($ids);
221 $dao = new CRM_Core_DAO();
222 $dao->contact_id
= $ids[0];
226 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
231 // ensure there does not exists a contact_id / uf_id pair
232 // in the DB. This might be due to multiple emails per contact
236 FROM civicrm_uf_match
237 WHERE contact_id = %1
241 1 => array($dao->contact_id
, 'Integer'),
242 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
244 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
248 $ufmatch->contact_id
= $dao->contact_id
;
249 $ufmatch->uf_name
= $uniqId;
254 if ($config->userSystem
->is_drupal
) {
257 elseif ($uf == 'WordPress') {
258 $mail = 'user_email';
264 if (is_object($user)) {
265 $params = array('email-Primary' => $user->$mail);
268 if ($ctype == 'Organization') {
269 $params['organization_name'] = $uniqId;
271 elseif ($ctype == 'Household') {
272 $params['household_name'] = $uniqId;
276 $ctype = "Individual";
278 $params['contact_type'] = $ctype;
280 // extract first / middle / last name
282 if ($uf == 'Joomla' && $user->name
) {
283 CRM_Utils_String
::extractName($user->name
, $params);
286 if ($uf == 'WordPress') {
287 if ($user->first_name
) {
288 $params['first_name'] = $user->first_name
;
291 if ($user->last_name
) {
292 $params['last_name'] = $user->last_name
;
296 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
297 $ufmatch->contact_id
= $contactId;
298 $ufmatch->uf_name
= $uniqId;
301 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
302 // user has two e-mails and there is a cms match for each of them
303 // the gets rid of the nasty fata error but still reports the error
306 FROM civicrm_uf_match
307 WHERE ( contact_id = %1
313 1 => array($ufmatch->contact_id
, 'Integer'),
314 2 => array($ufmatch->uf_name
, 'String'),
315 3 => array($ufmatch->uf_id
, 'Integer'),
316 4 => array($ufmatch->domain_id
, 'Integer'),
319 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
322 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
325 $transaction->commit();
328 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
330 1 => $ufmatch->contact_id
,
332 3 => $ufmatch->uf_id
,
349 * update the uf_name in the user object
351 * @param int $contactId id of the contact to update
357 static function updateUFName($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 $contactId Int Contact ID of the user
406 * @param $email String email to be modified for the user
412 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 //check if the primary email for the contact exists
425 //$contactDetails[1] - email
426 //$contactDetails[3] - email id
427 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
429 if (trim($contactDetails[1])) {
430 $emailID = $contactDetails[3];
431 //update if record is found
432 $query = "UPDATE civicrm_email
436 1 => array($emailAddress, 'String'),
437 2 => array($emailID, 'Integer'),
439 $dao = CRM_Core_DAO
::executeQuery($query, $p);
442 //else insert a new email record
443 $email = new CRM_Core_DAO_Email();
444 $email->contact_id
= $contactId;
445 $email->is_primary
= 1;
446 $email->email
= $emailAddress;
448 $emailID = $email->id
;
451 CRM_Core_BAO_Log
::register($contactId,
459 * Delete the object records that are associated with this cms user
461 * @param int $ufID id of the user to delete
467 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
478 * @param int $ufID Id of UF for which related contact_id is required
480 * @return int contact_id on success, null otherwise
484 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 ID of the contact for which related uf_id is required
504 * @return int uf_id of the given contact_id on success, null otherwise
508 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
;
523 static function isEmptyTable() {
524 $sql = "SELECT count(id) FROM civicrm_uf_match";
525 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
529 * get the list of contact_id
532 * @return int contact_id on success, null otherwise
536 static function getContactIDs() {
538 $dao = new CRM_Core_DAO_UFMatch();
540 while ($dao->fetch()) {
541 $id[] = $dao->contact_id
;
547 * see if this user exists, and if so, if they're allowed to login
550 * @return bool true if allowed to login, false otherwise
554 static function getAllowedToLogin($openId) {
555 $ufmatch = new CRM_Core_DAO_UFMatch();
556 $ufmatch->uf_name
= $openId;
557 $ufmatch->allowed_to_login
= 1;
558 if ($ufmatch->find(TRUE)) {
565 * get the next unused uf_id value, since the standalone UF doesn't
566 * have id's (it uses OpenIDs, which go in a different field)
569 * @return int next highest unused value for uf_id
573 static function getNextUfIdValue() {
574 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
575 $dao = CRM_Core_DAO
::executeQuery($query);
577 $ufId = $dao->next_uf_id
;
586 static function isDuplicateUser($email) {
587 $session = CRM_Core_Session
::singleton();
588 $contactID = $session->get('userID');
589 if (!empty($email) && isset($contactID)) {
590 $dao = new CRM_Core_DAO_UFMatch();
591 $dao->uf_name
= $email;
592 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
600 * Get uf match values for given uf id or logged in user.
602 * @param int $ufID uf id.
604 * return array $ufValues uf values.
606 static function getUFValues($ufID = NULL) {
608 //get logged in user uf id.
609 $ufID = CRM_Utils_System
::getLoggedInUfID();
616 if ($ufID && !isset($ufValues[$ufID])) {
617 $ufmatch = new CRM_Core_DAO_UFMatch();
618 $ufmatch->uf_id
= $ufID;
619 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
620 if ($ufmatch->find(TRUE)) {
621 $ufValues[$ufID] = array(
622 'uf_id' => $ufmatch->uf_id
,
623 'uf_name' => $ufmatch->uf_name
,
624 'contact_id' => $ufmatch->contact_id
,
625 'domain_id' => $ufmatch->domain_id
,
629 return $ufValues[$ufID];