3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 $config = CRM_Core_Config
::singleton();
76 $session = CRM_Core_Session
::singleton();
77 if (!is_object($session)) {
78 CRM_Core_Error
::fatal('wow, session is not an object?');
82 if ($config->userSystem
->is_drupal
) {
87 elseif ($uf == 'Joomla') {
91 if (!isset($user->id
) ||
!isset($user->email
)) {
92 $user = JFactory
::getUser();
95 elseif ($uf == 'WordPress') {
97 $login = 'user_login';
101 CRM_Core_Error
::statusBounce(ts('Please set the user framework variable'));
104 // if the id of the object is zero (true for anon users in drupal)
105 // have we already processed this user, if so early
107 $userID = $session->get('userID');
108 $ufID = $session->get('ufID');
110 if (!$update && $ufID == $user->$key) {
114 //check do we have logged in user.
115 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
117 // reset the session if we are a different user
118 if ($ufID && $ufID != $user->$key) {
121 //get logged in user ids, and set to session.
122 if ($isUserLoggedIn) {
123 $userIds = self
::getUFValues();
124 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
125 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
126 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
131 if ($user->$key == 0) {
135 if (!isset($uniqId) ||
!$uniqId) {
136 $uniqId = $user->$mail;
139 $ufmatch = self
::synchronizeUFMatch($user, $user->$key, $uniqId, $uf, NULL, $ctype, $isLogin);
144 //make sure we have session w/ consistent ids.
145 $ufID = $ufmatch->uf_id
;
146 $userID = $ufmatch->contact_id
;
148 if ($isUserLoggedIn) {
149 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
150 //are we processing logged in user.
151 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
152 $userIds = self
::getUFValues($loggedInUserUfID);
153 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
154 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
155 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
159 //set user ids to session.
160 $session->set('ufID', $ufID);
161 $session->set('userID', $userID);
162 $session->set('ufUniqID', $ufUniqID);
164 // add current contact to recently viewed
165 if ($ufmatch->contact_id
) {
166 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) =
167 CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
169 $otherRecent = array(
170 'imageUrl' => $contactImageUrl,
171 'subtype' => $contactSubtype,
172 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
175 CRM_Utils_Recent
::add($displayName,
176 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
177 $ufmatch->contact_id
,
179 $ufmatch->contact_id
,
187 * Synchronize the object with the UF Match entry. Can be called stand-alone from
188 * the drupalUsers script
190 * @param Object $user the drupal user object
191 * @param string $userKey the id of the user from the uf object
192 * @param string $uniqId the OpenID of the user
193 * @param string $uf the name of the user framework
194 * @param integer $status returns the status if user created or already exits (used for CMS sync)
196 * @return the ufmatch object that was found or created
200 static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
201 $config = CRM_Core_Config
::singleton();
203 if (!CRM_Utils_Rule
::email($uniqId)) {
204 $retVal = $status ?
NULL : FALSE;
210 // make sure that a contact id exists for this user id
211 $ufmatch = new CRM_Core_DAO_UFMatch();
212 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
213 $ufmatch->uf_id
= $userKey;
215 if (!$ufmatch->find(TRUE)) {
216 $transaction = new CRM_Core_Transaction();
219 if (!empty($_POST) && !$isLogin) {
221 $params['email'] = $uniqId;
223 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
224 $dedupeParams['check_permission'] = FALSE;
225 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
228 CRM_Core_BAO_Setting
::getItem(
229 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
230 'uniq_email_per_site'
233 // restrict dupeIds to ones that belong to current domain/site.
234 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
235 foreach ($ids as $index => $dupeId) {
236 if (!in_array($dupeId, $siteContacts)) {
240 // re-index the array
241 $ids = array_values($ids);
244 $dao = new CRM_Core_DAO();
245 $dao->contact_id
= $ids[0];
249 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
254 // ensure there does not exists a contact_id / uf_id pair
255 // in the DB. This might be due to multiple emails per contact
259 FROM civicrm_uf_match
260 WHERE contact_id = %1
264 1 => array($dao->contact_id
, 'Integer'),
265 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
267 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
271 $ufmatch->contact_id
= $dao->contact_id
;
272 $ufmatch->uf_name
= $uniqId;
277 if ($config->userSystem
->is_drupal
) {
280 elseif ($uf == 'WordPress') {
281 $mail = 'user_email';
287 if (is_object($user)) {
288 $params = array('email-Primary' => $user->$mail);
291 if ($ctype == 'Organization') {
292 $params['organization_name'] = $uniqId;
294 elseif ($ctype == 'Household') {
295 $params['household_name'] = $uniqId;
299 $ctype = "Individual";
301 $params['contact_type'] = $ctype;
303 // extract first / middle / last name
305 if ($uf == 'Joomla' && $user->name
) {
306 CRM_Utils_String
::extractName($user->name
, $params);
309 if ($uf == 'WordPress') {
310 if ($user->first_name
) {
311 $params['first_name'] = $user->first_name
;
314 if ($user->last_name
) {
315 $params['last_name'] = $user->last_name
;
319 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
320 $ufmatch->contact_id
= $contactId;
321 $ufmatch->uf_name
= $uniqId;
324 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
325 // user has two e-mails and there is a cms match for each of them
326 // the gets rid of the nasty fata error but still reports the error
329 FROM civicrm_uf_match
330 WHERE ( contact_id = %1
336 1 => array($ufmatch->contact_id
, 'Integer'),
337 2 => array($ufmatch->uf_name
, 'String'),
338 3 => array($ufmatch->uf_id
, 'Integer'),
339 4 => array($ufmatch->domain_id
, 'Integer'),
342 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
345 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
348 $transaction->commit();
351 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
353 1 => $ufmatch->contact_id
,
355 3 => $ufmatch->uf_id
,
372 * update the uf_name in the user object
374 * @param int $contactId id of the contact to update
380 static function updateUFName($contactId) {
384 $config = CRM_Core_Config
::singleton();
385 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
393 // 1.do check for contact Id.
394 $ufmatch = new CRM_Core_DAO_UFMatch();
395 $ufmatch->contact_id
= $contactId;
396 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
397 if (!$ufmatch->find(TRUE)) {
400 if ($ufmatch->uf_name
!= $ufName) {
405 // 2.do check for duplicate ufName.
406 $ufDupeName = new CRM_Core_DAO_UFMatch();
407 $ufDupeName->uf_name
= $ufName;
408 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
409 if ($ufDupeName->find(TRUE) &&
410 $ufDupeName->contact_id
!= $contactId
419 // save the updated ufmatch object
420 $ufmatch->uf_name
= $ufName;
422 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
426 * Update the email value for the contact and user profile
428 * @param $contactId Int Contact ID of the user
429 * @param $email String email to be modified for the user
435 static function updateContactEmail($contactId, $emailAddress) {
436 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
437 $emailAddress = $strtolower($emailAddress);
439 $ufmatch = new CRM_Core_DAO_UFMatch();
440 $ufmatch->contact_id
= $contactId;
441 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
442 if ($ufmatch->find(TRUE)) {
443 // Save the email in UF Match table
444 $ufmatch->uf_name
= $emailAddress;
445 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
447 //check if the primary email for the contact exists
448 //$contactDetails[1] - email
449 //$contactDetails[3] - email id
450 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
452 if (trim($contactDetails[1])) {
453 $emailID = $contactDetails[3];
454 //update if record is found
455 $query = "UPDATE civicrm_email
459 1 => array($emailAddress, 'String'),
460 2 => array($emailID, 'Integer'),
462 $dao = CRM_Core_DAO
::executeQuery($query, $p);
465 //else insert a new email record
466 $email = new CRM_Core_DAO_Email();
467 $email->contact_id
= $contactId;
468 $email->is_primary
= 1;
469 $email->email
= $emailAddress;
471 $emailID = $email->id
;
474 CRM_Core_BAO_Log
::register($contactId,
482 * Delete the object records that are associated with this cms user
484 * @param int $ufID id of the user to delete
490 static function deleteUser($ufID) {
491 $ufmatch = new CRM_Core_DAO_UFMatch();
493 $ufmatch->uf_id
= $ufID;
494 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
499 * get the contact_id given a uf_id
501 * @param int $ufID Id of UF for which related contact_id is required
503 * @return int contact_id on success, null otherwise
507 static function getContactId($ufID) {
512 $ufmatch = new CRM_Core_DAO_UFMatch();
514 $ufmatch->uf_id
= $ufID;
515 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
516 if ($ufmatch->find(TRUE)) {
517 return (int ) $ufmatch->contact_id
;
523 * get the uf_id given a contact_id
525 * @param int $contactID ID of the contact for which related uf_id is required
527 * @return int uf_id of the given contact_id on success, null otherwise
531 static function getUFId($contactID) {
532 if (!isset($contactID)) {
535 $domain = CRM_Core_BAO_Domain
::getDomain();
536 $ufmatch = new CRM_Core_DAO_UFMatch();
538 $ufmatch->contact_id
= $contactID;
539 $ufmatch->domain_id
= $domain->id
;
540 if ($ufmatch->find(TRUE)) {
541 return $ufmatch->uf_id
;
546 static function isEmptyTable() {
547 $sql = "SELECT count(id) FROM civicrm_uf_match";
548 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
552 * get the list of contact_id
555 * @return int contact_id on success, null otherwise
559 static function getContactIDs() {
561 $dao = new CRM_Core_DAO_UFMatch();
563 while ($dao->fetch()) {
564 $id[] = $dao->contact_id
;
570 * see if this user exists, and if so, if they're allowed to login
573 * @return bool true if allowed to login, false otherwise
577 static function getAllowedToLogin($openId) {
578 $ufmatch = new CRM_Core_DAO_UFMatch();
579 $ufmatch->uf_name
= $openId;
580 $ufmatch->allowed_to_login
= 1;
581 if ($ufmatch->find(TRUE)) {
588 * get the next unused uf_id value, since the standalone UF doesn't
589 * have id's (it uses OpenIDs, which go in a different field)
592 * @return int next highest unused value for uf_id
596 static function getNextUfIdValue() {
597 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
598 $dao = CRM_Core_DAO
::executeQuery($query);
600 $ufId = $dao->next_uf_id
;
609 static function isDuplicateUser($email) {
610 $session = CRM_Core_Session
::singleton();
611 $contactID = $session->get('userID');
612 if (!empty($email) && isset($contactID)) {
613 $dao = new CRM_Core_DAO_UFMatch();
614 $dao->uf_name
= $email;
615 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
623 * Get uf match values for given uf id or logged in user.
625 * @param int $ufID uf id.
627 * return array $ufValues uf values.
629 static function getUFValues($ufID = NULL) {
631 //get logged in user uf id.
632 $ufID = CRM_Utils_System
::getLoggedInUfID();
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] = array(
645 'uf_id' => $ufmatch->uf_id
,
646 'uf_name' => $ufmatch->uf_name
,
647 'contact_id' => $ufmatch->contact_id
,
648 'domain_id' => $ufmatch->domain_id
,
652 return $ufValues[$ufID];