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
71 * @param bool $isLogin
77 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?');
85 $userSystemID = $userSystem->getBestUFID($user);
86 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
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
91 $userID = $session->get('userID');
92 $ufID = $session->get('ufID');
94 if (!$update && $ufID == $userSystemID) {
98 //check do we have logged in user.
99 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
101 // reset the session if we are a different user
102 if ($ufID && $ufID != $userSystemID) {
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, ''));
115 if ($userSystemID == 0) {
119 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
124 //make sure we have session w/ consistent ids.
125 $ufID = $ufmatch->uf_id
;
126 $userID = $ufmatch->contact_id
;
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, '');
139 //set user ids to session.
140 $session->set('ufID', $ufID);
141 $session->set('userID', $userID);
142 $session->set('ufUniqID', $ufUniqID);
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);
149 $otherRecent = array(
150 'imageUrl' => $contactImageUrl,
151 'subtype' => $contactSubtype,
152 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
155 CRM_Utils_Recent
::add($displayName,
156 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
157 $ufmatch->contact_id
,
159 $ufmatch->contact_id
,
167 * Synchronize the object with the UF Match entry. Can be called stand-alone from
168 * the drupalUsers script
170 * @param Object $user the drupal user object
171 * @param string $userKey the id of the user from the uf object
172 * @param string $uniqId the OpenID of the user
173 * @param string $uf the name of the user framework
174 * @param integer $status returns the status if user created or already exits (used for CMS sync)
177 * @param bool $isLogin
179 * @return the ufmatch object that was found or created
183 static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
184 $config = CRM_Core_Config
::singleton();
186 if (!CRM_Utils_Rule
::email($uniqId)) {
187 $retVal = $status ?
NULL : FALSE;
193 // make sure that a contact id exists for this user id
194 $ufmatch = new CRM_Core_DAO_UFMatch();
195 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
196 $ufmatch->uf_id
= $userKey;
198 if (!$ufmatch->find(TRUE)) {
199 $transaction = new CRM_Core_Transaction();
202 if (!empty($_POST) && !$isLogin) {
204 $params['email'] = $uniqId;
206 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
207 $dedupeParams['check_permission'] = FALSE;
208 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
211 CRM_Core_BAO_Setting
::getItem(
212 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
213 'uniq_email_per_site'
216 // restrict dupeIds to ones that belong to current domain/site.
217 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
218 foreach ($ids as $index => $dupeId) {
219 if (!in_array($dupeId, $siteContacts)) {
223 // re-index the array
224 $ids = array_values($ids);
227 $dao = new CRM_Core_DAO();
228 $dao->contact_id
= $ids[0];
232 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
237 // ensure there does not exists a contact_id / uf_id pair
238 // in the DB. This might be due to multiple emails per contact
242 FROM civicrm_uf_match
243 WHERE contact_id = %1
247 1 => array($dao->contact_id
, 'Integer'),
248 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
250 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
254 $ufmatch->contact_id
= $dao->contact_id
;
255 $ufmatch->uf_name
= $uniqId;
260 if ($config->userSystem
->is_drupal
) {
263 elseif ($uf == 'WordPress') {
264 $mail = 'user_email';
270 if (is_object($user)) {
271 $params = array('email-Primary' => $user->$mail);
274 if ($ctype == 'Organization') {
275 $params['organization_name'] = $uniqId;
277 elseif ($ctype == 'Household') {
278 $params['household_name'] = $uniqId;
282 $ctype = "Individual";
284 $params['contact_type'] = $ctype;
286 // extract first / middle / last name
288 if ($uf == 'Joomla' && $user->name
) {
289 CRM_Utils_String
::extractName($user->name
, $params);
292 if ($uf == 'WordPress') {
293 if ($user->first_name
) {
294 $params['first_name'] = $user->first_name
;
297 if ($user->last_name
) {
298 $params['last_name'] = $user->last_name
;
302 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
303 $ufmatch->contact_id
= $contactId;
304 $ufmatch->uf_name
= $uniqId;
307 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
308 // user has two e-mails and there is a cms match for each of them
309 // the gets rid of the nasty fata error but still reports the error
312 FROM civicrm_uf_match
313 WHERE ( contact_id = %1
319 1 => array($ufmatch->contact_id
, 'Integer'),
320 2 => array($ufmatch->uf_name
, 'String'),
321 3 => array($ufmatch->uf_id
, 'Integer'),
322 4 => array($ufmatch->domain_id
, 'Integer'),
325 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
328 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
331 $transaction->commit();
334 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
336 1 => $ufmatch->contact_id
,
338 3 => $ufmatch->uf_id
,
355 * update the uf_name in the user object
357 * @param int $contactId id of the contact to update
363 static function updateUFName($contactId) {
367 $config = CRM_Core_Config
::singleton();
368 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
376 // 1.do check for contact Id.
377 $ufmatch = new CRM_Core_DAO_UFMatch();
378 $ufmatch->contact_id
= $contactId;
379 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
380 if (!$ufmatch->find(TRUE)) {
383 if ($ufmatch->uf_name
!= $ufName) {
388 // 2.do check for duplicate ufName.
389 $ufDupeName = new CRM_Core_DAO_UFMatch();
390 $ufDupeName->uf_name
= $ufName;
391 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
392 if ($ufDupeName->find(TRUE) &&
393 $ufDupeName->contact_id
!= $contactId
402 // save the updated ufmatch object
403 $ufmatch->uf_name
= $ufName;
405 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
409 * Update the email value for the contact and user profile
411 * @param $contactId Int Contact ID of the user
412 * @param $email String email to be modified for the user
418 static function updateContactEmail($contactId, $emailAddress) {
419 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
420 $emailAddress = $strtolower($emailAddress);
422 $ufmatch = new CRM_Core_DAO_UFMatch();
423 $ufmatch->contact_id
= $contactId;
424 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
425 if ($ufmatch->find(TRUE)) {
426 // Save the email in UF Match table
427 $ufmatch->uf_name
= $emailAddress;
428 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
430 //check if the primary email for the contact exists
431 //$contactDetails[1] - email
432 //$contactDetails[3] - email id
433 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
435 if (trim($contactDetails[1])) {
436 $emailID = $contactDetails[3];
437 //update if record is found
438 $query = "UPDATE civicrm_email
442 1 => array($emailAddress, 'String'),
443 2 => array($emailID, 'Integer'),
445 $dao = CRM_Core_DAO
::executeQuery($query, $p);
448 //else insert a new email record
449 $email = new CRM_Core_DAO_Email();
450 $email->contact_id
= $contactId;
451 $email->is_primary
= 1;
452 $email->email
= $emailAddress;
454 $emailID = $email->id
;
457 CRM_Core_BAO_Log
::register($contactId,
465 * Delete the object records that are associated with this cms user
467 * @param int $ufID id of the user to delete
473 static function deleteUser($ufID) {
474 $ufmatch = new CRM_Core_DAO_UFMatch();
476 $ufmatch->uf_id
= $ufID;
477 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
482 * get the contact_id given a uf_id
484 * @param int $ufID Id of UF for which related contact_id is required
486 * @return int contact_id on success, null otherwise
490 static function getContactId($ufID) {
495 $ufmatch = new CRM_Core_DAO_UFMatch();
497 $ufmatch->uf_id
= $ufID;
498 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
499 if ($ufmatch->find(TRUE)) {
500 return (int ) $ufmatch->contact_id
;
506 * get the uf_id given a contact_id
508 * @param int $contactID ID of the contact for which related uf_id is required
510 * @return int uf_id of the given contact_id on success, null otherwise
514 static function getUFId($contactID) {
515 if (!isset($contactID)) {
518 $domain = CRM_Core_BAO_Domain
::getDomain();
519 $ufmatch = new CRM_Core_DAO_UFMatch();
521 $ufmatch->contact_id
= $contactID;
522 $ufmatch->domain_id
= $domain->id
;
523 if ($ufmatch->find(TRUE)) {
524 return $ufmatch->uf_id
;
529 static function isEmptyTable() {
530 $sql = "SELECT count(id) FROM civicrm_uf_match";
531 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
535 * get the list of contact_id
538 * @return int contact_id on success, null otherwise
542 static function getContactIDs() {
544 $dao = new CRM_Core_DAO_UFMatch();
546 while ($dao->fetch()) {
547 $id[] = $dao->contact_id
;
553 * see if this user exists, and if so, if they're allowed to login
556 * @return bool true if allowed to login, false otherwise
560 static function getAllowedToLogin($openId) {
561 $ufmatch = new CRM_Core_DAO_UFMatch();
562 $ufmatch->uf_name
= $openId;
563 $ufmatch->allowed_to_login
= 1;
564 if ($ufmatch->find(TRUE)) {
571 * get the next unused uf_id value, since the standalone UF doesn't
572 * have id's (it uses OpenIDs, which go in a different field)
575 * @return int next highest unused value for uf_id
579 static function getNextUfIdValue() {
580 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
581 $dao = CRM_Core_DAO
::executeQuery($query);
583 $ufId = $dao->next_uf_id
;
592 static function isDuplicateUser($email) {
593 $session = CRM_Core_Session
::singleton();
594 $contactID = $session->get('userID');
595 if (!empty($email) && isset($contactID)) {
596 $dao = new CRM_Core_DAO_UFMatch();
597 $dao->uf_name
= $email;
598 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
606 * Get uf match values for given uf id or logged in user.
608 * @param int $ufID uf id.
610 * return array $ufValues uf values.
615 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];