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 $dao = new CRM_Core_DAO_UFMatch();
51 $dao->copyValues($params);
53 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
59 * Given a UF user object, make sure there is a contact
60 * object for this user. If the user has new values, we need
61 * to update the CRM DB with the new values
63 * @param Object $user the drupal user object
64 * @param boolean $update has the user object been edited
71 static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
72 $config = CRM_Core_Config
::singleton();
73 $session = CRM_Core_Session
::singleton();
74 if (!is_object($session)) {
75 CRM_Core_Error
::fatal('wow, session is not an object?');
79 if ($config->userSystem
->is_drupal
) {
84 elseif ($uf == 'Joomla') {
88 if (!isset($user->id
) ||
!isset($user->email
)) {
89 $user = JFactory
::getUser();
92 elseif ($uf == 'WordPress') {
94 $login = 'user_login';
98 CRM_Core_Error
::statusBounce(ts('Please set the user framework variable'));
101 // if the id of the object is zero (true for anon users in drupal)
102 // have we already processed this user, if so early
104 $userID = $session->get('userID');
105 $ufID = $session->get('ufID');
107 if (!$update && $ufID == $user->$key) {
111 //check do we have logged in user.
112 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
114 // reset the session if we are a different user
115 if ($ufID && $ufID != $user->$key) {
118 //get logged in user ids, and set to session.
119 if ($isUserLoggedIn) {
120 $userIds = self
::getUFValues();
121 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
122 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
123 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
128 if ($user->$key == 0) {
132 if (!isset($uniqId) ||
!$uniqId) {
133 $uniqId = $user->$mail;
136 $ufmatch = self
::synchronizeUFMatch($user, $user->$key, $uniqId, $uf, NULL, $ctype, $isLogin);
141 //make sure we have session w/ consistent ids.
142 $ufID = $ufmatch->uf_id
;
143 $userID = $ufmatch->contact_id
;
145 if ($isUserLoggedIn) {
146 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
147 //are we processing logged in user.
148 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
149 $userIds = self
::getUFValues($loggedInUserUfID);
150 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
151 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
152 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
156 //set user ids to session.
157 $session->set('ufID', $ufID);
158 $session->set('userID', $userID);
159 $session->set('ufUniqID', $ufUniqID);
161 // add current contact to recently viewed
162 if ($ufmatch->contact_id
) {
163 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) =
164 CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
166 $otherRecent = array(
167 'imageUrl' => $contactImageUrl,
168 'subtype' => $contactSubtype,
169 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
172 CRM_Utils_Recent
::add($displayName,
173 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
174 $ufmatch->contact_id
,
176 $ufmatch->contact_id
,
184 * Synchronize the object with the UF Match entry. Can be called stand-alone from
185 * the drupalUsers script
187 * @param Object $user the drupal user object
188 * @param string $userKey the id of the user from the uf object
189 * @param string $uniqId the OpenID of the user
190 * @param string $uf the name of the user framework
191 * @param integer $status returns the status if user created or already exits (used for CMS sync)
193 * @return the ufmatch object that was found or created
197 static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
198 $config = CRM_Core_Config
::singleton();
200 if (!CRM_Utils_Rule
::email($uniqId)) {
201 $retVal = $status ?
NULL : FALSE;
207 // make sure that a contact id exists for this user id
208 $ufmatch = new CRM_Core_DAO_UFMatch();
209 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
210 $ufmatch->uf_id
= $userKey;
212 if (!$ufmatch->find(TRUE)) {
213 $transaction = new CRM_Core_Transaction();
216 if (!empty($_POST) && !$isLogin) {
218 $params['email'] = $uniqId;
220 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
221 $dedupeParams['check_permission'] = FALSE;
222 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
225 CRM_Core_BAO_Setting
::getItem(
226 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
227 'uniq_email_per_site'
230 // restrict dupeIds to ones that belong to current domain/site.
231 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
232 foreach ($ids as $index => $dupeId) {
233 if (!in_array($dupeId, $siteContacts)) {
237 // re-index the array
238 $ids = array_values($ids);
241 $dao = new CRM_Core_DAO();
242 $dao->contact_id
= $ids[0];
246 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
251 // ensure there does not exists a contact_id / uf_id pair
252 // in the DB. This might be due to multiple emails per contact
256 FROM civicrm_uf_match
257 WHERE contact_id = %1
261 1 => array($dao->contact_id
, 'Integer'),
262 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
264 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
268 $ufmatch->contact_id
= $dao->contact_id
;
269 $ufmatch->uf_name
= $uniqId;
274 if ($config->userSystem
->is_drupal
) {
277 elseif ($uf == 'WordPress') {
278 $mail = 'user_email';
284 if (is_object($user)) {
285 $params = array('email-Primary' => $user->$mail);
288 if ($ctype == 'Organization') {
289 $params['organization_name'] = $uniqId;
291 elseif ($ctype == 'Household') {
292 $params['household_name'] = $uniqId;
296 $ctype = "Individual";
298 $params['contact_type'] = $ctype;
300 // extract first / middle / last name
302 if ($uf == 'Joomla' && $user->name
) {
303 CRM_Utils_String
::extractName($user->name
, $params);
306 if ($uf == 'WordPress') {
307 if ($user->first_name
) {
308 $params['first_name'] = $user->first_name
;
311 if ($user->last_name
) {
312 $params['last_name'] = $user->last_name
;
316 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
317 $ufmatch->contact_id
= $contactId;
318 $ufmatch->uf_name
= $uniqId;
321 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
322 // user has two e-mails and there is a cms match for each of them
323 // the gets rid of the nasty fata error but still reports the error
326 FROM civicrm_uf_match
327 WHERE ( contact_id = %1
333 1 => array($ufmatch->contact_id
, 'Integer'),
334 2 => array($ufmatch->uf_name
, 'String'),
335 3 => array($ufmatch->uf_id
, 'Integer'),
336 4 => array($ufmatch->domain_id
, 'Integer'),
339 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
342 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
345 $transaction->commit();
348 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
350 1 => $ufmatch->contact_id
,
352 3 => $ufmatch->uf_id
,
369 * update the uf_name in the user object
371 * @param int $contactId id of the contact to update
377 static function updateUFName($contactId) {
381 $config = CRM_Core_Config
::singleton();
382 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
390 // 1.do check for contact Id.
391 $ufmatch = new CRM_Core_DAO_UFMatch();
392 $ufmatch->contact_id
= $contactId;
393 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
394 if (!$ufmatch->find(TRUE)) {
397 if ($ufmatch->uf_name
!= $ufName) {
402 // 2.do check for duplicate ufName.
403 $ufDupeName = new CRM_Core_DAO_UFMatch();
404 $ufDupeName->uf_name
= $ufName;
405 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
406 if ($ufDupeName->find(TRUE) &&
407 $ufDupeName->contact_id
!= $contactId
416 // save the updated ufmatch object
417 $ufmatch->uf_name
= $ufName;
419 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
423 * Update the email value for the contact and user profile
425 * @param $contactId Int Contact ID of the user
426 * @param $email String email to be modified for the user
432 static function updateContactEmail($contactId, $emailAddress) {
433 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
434 $emailAddress = $strtolower($emailAddress);
436 $ufmatch = new CRM_Core_DAO_UFMatch();
437 $ufmatch->contact_id
= $contactId;
438 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
439 if ($ufmatch->find(TRUE)) {
440 // Save the email in UF Match table
441 $ufmatch->uf_name
= $emailAddress;
442 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
444 //check if the primary email for the contact exists
445 //$contactDetails[1] - email
446 //$contactDetails[3] - email id
447 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
449 if (trim($contactDetails[1])) {
450 $emailID = $contactDetails[3];
451 //update if record is found
452 $query = "UPDATE civicrm_email
456 1 => array($emailAddress, 'String'),
457 2 => array($emailID, 'Integer'),
459 $dao = CRM_Core_DAO
::executeQuery($query, $p);
462 //else insert a new email record
463 $email = new CRM_Core_DAO_Email();
464 $email->contact_id
= $contactId;
465 $email->is_primary
= 1;
466 $email->email
= $emailAddress;
468 $emailID = $email->id
;
471 CRM_Core_BAO_Log
::register($contactId,
479 * Delete the object records that are associated with this cms user
481 * @param int $ufID id of the user to delete
487 static function deleteUser($ufID) {
488 $ufmatch = new CRM_Core_DAO_UFMatch();
490 $ufmatch->uf_id
= $ufID;
491 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
496 * get the contact_id given a uf_id
498 * @param int $ufID Id of UF for which related contact_id is required
500 * @return int contact_id on success, null otherwise
504 static function getContactId($ufID) {
509 $ufmatch = new CRM_Core_DAO_UFMatch();
511 $ufmatch->uf_id
= $ufID;
512 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
513 if ($ufmatch->find(TRUE)) {
514 return (int ) $ufmatch->contact_id
;
520 * get the uf_id given a contact_id
522 * @param int $contactID ID of the contact for which related uf_id is required
524 * @return int uf_id of the given contact_id on success, null otherwise
528 static function getUFId($contactID) {
529 if (!isset($contactID)) {
532 $domain = CRM_Core_BAO_Domain
::getDomain();
533 $ufmatch = new CRM_Core_DAO_UFMatch();
535 $ufmatch->contact_id
= $contactID;
536 $ufmatch->domain_id
= $domain->id
;
537 if ($ufmatch->find(TRUE)) {
538 return $ufmatch->uf_id
;
543 static function isEmptyTable() {
544 $sql = "SELECT count(id) FROM civicrm_uf_match";
545 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
549 * get the list of contact_id
552 * @return int contact_id on success, null otherwise
556 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
570 * @return bool true if allowed to login, false otherwise
574 static function getAllowedToLogin($openId) {
575 $ufmatch = new CRM_Core_DAO_UFMatch();
576 $ufmatch->uf_name
= $openId;
577 $ufmatch->allowed_to_login
= 1;
578 if ($ufmatch->find(TRUE)) {
585 * get the next unused uf_id value, since the standalone UF doesn't
586 * have id's (it uses OpenIDs, which go in a different field)
589 * @return int next highest unused value for uf_id
593 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
;
606 static function isDuplicateUser($email) {
607 $session = CRM_Core_Session
::singleton();
608 $contactID = $session->get('userID');
609 if (!empty($email) && isset($contactID)) {
610 $dao = new CRM_Core_DAO_UFMatch();
611 $dao->uf_name
= $email;
612 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
620 * Get uf match values for given uf id or logged in user.
622 * @param int $ufID uf id.
624 * return array $ufValues uf values.
626 static function getUFValues($ufID = NULL) {
628 //get logged in user uf id.
629 $ufID = CRM_Utils_System
::getLoggedInUfID();
636 if ($ufID && !isset($ufValues[$ufID])) {
637 $ufmatch = new CRM_Core_DAO_UFMatch();
638 $ufmatch->uf_id
= $ufID;
639 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
640 if ($ufmatch->find(TRUE)) {
641 $ufValues[$ufID] = array(
642 'uf_id' => $ufmatch->uf_id
,
643 'uf_name' => $ufmatch->uf_name
,
644 'contact_id' => $ufmatch->contact_id
,
645 'domain_id' => $ufmatch->domain_id
,
649 return $ufValues[$ufID];