3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
49 * @param array $params
51 * @return CRM_Core_DAO_UFMatch
53 public static function create($params) {
54 $hook = empty($params['id']) ?
'create' : 'edit';
55 CRM_Utils_Hook
::pre($hook, 'UFMatch', CRM_Utils_Array
::value('id', $params), $params);
56 if(empty($params['domain_id'])) {
57 $params['domain_id'] = CRM_Core_Config
::domainID();
59 $dao = new CRM_Core_DAO_UFMatch();
60 $dao->copyValues($params);
62 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
68 * Given a UF user object, make sure there is a contact
69 * object for this user. If the user has new values, we need
70 * to update the CRM DB with the new values
73 * The drupal user object.
75 * Has the user object been edited.
79 * @param bool $isLogin
84 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
85 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
86 $session = CRM_Core_Session
::singleton();
87 if (!is_object($session)) {
88 CRM_Core_Error
::fatal('wow, session is not an object?');
92 $userSystemID = $userSystem->getBestUFID($user);
93 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
95 // if the id of the object is zero (true for anon users in drupal)
96 // have we already processed this user, if so early
98 $userID = $session->get('userID');
99 $ufID = $session->get('ufID');
101 if (!$update && $ufID == $userSystemID) {
105 //check do we have logged in user.
106 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
108 // reset the session if we are a different user
109 if ($ufID && $ufID != $userSystemID) {
112 //get logged in user ids, and set to session.
113 if ($isUserLoggedIn) {
114 $userIds = self
::getUFValues();
115 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
116 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
117 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
122 if ($userSystemID == 0) {
126 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
131 //make sure we have session w/ consistent ids.
132 $ufID = $ufmatch->uf_id
;
133 $userID = $ufmatch->contact_id
;
135 if ($isUserLoggedIn) {
136 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
137 //are we processing logged in user.
138 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
139 $userIds = self
::getUFValues($loggedInUserUfID);
140 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
141 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
142 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
146 //set user ids to session.
147 $session->set('ufID', $ufID);
148 $session->set('userID', $userID);
149 $session->set('ufUniqID', $ufUniqID);
151 // add current contact to recently viewed
152 if ($ufmatch->contact_id
) {
153 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) =
154 CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
156 $otherRecent = array(
157 'imageUrl' => $contactImageUrl,
158 'subtype' => $contactSubtype,
159 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
162 CRM_Utils_Recent
::add($displayName,
163 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
164 $ufmatch->contact_id
,
166 $ufmatch->contact_id
,
174 * Synchronize the object with the UF Match entry. Can be called stand-alone from
175 * the drupalUsers script
177 * @param Object $user
178 * The drupal user object.
179 * @param string $userKey
180 * The id of the user from the uf object.
181 * @param string $uniqId
182 * The OpenID of the user.
184 * The name of the user framework.
186 * Returns the status if user created or already exits (used for CMS sync).
189 * @param bool $isLogin
191 * @return the ufmatch object that was found or created
194 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
195 $config = CRM_Core_Config
::singleton();
197 if (!CRM_Utils_Rule
::email($uniqId)) {
198 $retVal = $status ?
NULL : FALSE;
204 // make sure that a contact id exists for this user id
205 $ufmatch = new CRM_Core_DAO_UFMatch();
206 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
207 $ufmatch->uf_id
= $userKey;
209 if (!$ufmatch->find(TRUE)) {
210 $transaction = new CRM_Core_Transaction();
213 if (!empty($_POST) && !$isLogin) {
215 $params['email'] = $uniqId;
217 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
218 $dedupeParams['check_permission'] = FALSE;
219 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
222 CRM_Core_BAO_Setting
::getItem(
223 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
224 'uniq_email_per_site'
227 // restrict dupeIds to ones that belong to current domain/site.
228 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
229 foreach ($ids as $index => $dupeId) {
230 if (!in_array($dupeId, $siteContacts)) {
234 // re-index the array
235 $ids = array_values($ids);
238 $dao = new CRM_Core_DAO();
239 $dao->contact_id
= $ids[0];
243 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
248 // ensure there does not exists a contact_id / uf_id pair
249 // in the DB. This might be due to multiple emails per contact
253 FROM civicrm_uf_match
254 WHERE contact_id = %1
258 1 => array($dao->contact_id
, 'Integer'),
259 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
261 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
265 $ufmatch->contact_id
= $dao->contact_id
;
266 $ufmatch->uf_name
= $uniqId;
271 if ($config->userSystem
->is_drupal
) {
274 elseif ($uf == 'WordPress') {
275 $mail = 'user_email';
281 if (is_object($user)) {
282 $params = array('email-Primary' => $user->$mail);
285 if ($ctype == 'Organization') {
286 $params['organization_name'] = $uniqId;
288 elseif ($ctype == 'Household') {
289 $params['household_name'] = $uniqId;
293 $ctype = "Individual";
295 $params['contact_type'] = $ctype;
297 // extract first / middle / last name
299 if ($uf == 'Joomla' && $user->name
) {
300 CRM_Utils_String
::extractName($user->name
, $params);
303 if ($uf == 'WordPress') {
304 if ($user->first_name
) {
305 $params['first_name'] = $user->first_name
;
308 if ($user->last_name
) {
309 $params['last_name'] = $user->last_name
;
313 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
314 $ufmatch->contact_id
= $contactId;
315 $ufmatch->uf_name
= $uniqId;
318 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
319 // user has two e-mails and there is a cms match for each of them
320 // the gets rid of the nasty fata error but still reports the error
323 FROM civicrm_uf_match
324 WHERE ( contact_id = %1
330 1 => array($ufmatch->contact_id
, 'Integer'),
331 2 => array($ufmatch->uf_name
, 'String'),
332 3 => array($ufmatch->uf_id
, 'Integer'),
333 4 => array($ufmatch->domain_id
, 'Integer'),
336 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
339 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
342 $transaction->commit();
345 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
347 1 => $ufmatch->contact_id
,
349 3 => $ufmatch->uf_id
,
366 * Update the uf_name in the user object
368 * @param int $contactId
369 * Id of the contact to update.
374 public static function updateUFName($contactId) {
378 $config = CRM_Core_Config
::singleton();
379 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
387 // 1.do check for contact Id.
388 $ufmatch = new CRM_Core_DAO_UFMatch();
389 $ufmatch->contact_id
= $contactId;
390 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
391 if (!$ufmatch->find(TRUE)) {
394 if ($ufmatch->uf_name
!= $ufName) {
399 // 2.do check for duplicate ufName.
400 $ufDupeName = new CRM_Core_DAO_UFMatch();
401 $ufDupeName->uf_name
= $ufName;
402 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
403 if ($ufDupeName->find(TRUE) &&
404 $ufDupeName->contact_id
!= $contactId
413 // save the updated ufmatch object
414 $ufmatch->uf_name
= $ufName;
416 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
420 * Update the email value for the contact and user profile
423 * Int Contact ID of the user.
424 * @param $emailAddress
425 * Email to be modified for the user.
430 public static function updateContactEmail($contactId, $emailAddress) {
431 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
432 $emailAddress = $strtolower($emailAddress);
434 $ufmatch = new CRM_Core_DAO_UFMatch();
435 $ufmatch->contact_id
= $contactId;
436 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
437 if ($ufmatch->find(TRUE)) {
438 // Save the email in UF Match table
439 $ufmatch->uf_name
= $emailAddress;
440 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
442 //check if the primary email for the contact exists
443 //$contactDetails[1] - email
444 //$contactDetails[3] - email id
445 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
447 if (trim($contactDetails[1])) {
448 $emailID = $contactDetails[3];
449 //update if record is found
450 $query = "UPDATE civicrm_email
454 1 => array($emailAddress, 'String'),
455 2 => array($emailID, 'Integer'),
457 $dao = CRM_Core_DAO
::executeQuery($query, $p);
460 //else insert a new email record
461 $email = new CRM_Core_DAO_Email();
462 $email->contact_id
= $contactId;
463 $email->is_primary
= 1;
464 $email->email
= $emailAddress;
466 $emailID = $email->id
;
469 CRM_Core_BAO_Log
::register($contactId,
477 * Delete the object records that are associated with this cms user
480 * Id of the user to delete.
485 public static function deleteUser($ufID) {
486 $ufmatch = new CRM_Core_DAO_UFMatch();
488 $ufmatch->uf_id
= $ufID;
489 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
494 * Get the contact_id given a uf_id
497 * Id of UF for which related contact_id is required.
499 * @return int contact_id on success, null otherwise
502 public static function getContactId($ufID) {
507 $ufmatch = new CRM_Core_DAO_UFMatch();
509 $ufmatch->uf_id
= $ufID;
510 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
511 if ($ufmatch->find(TRUE)) {
512 return (int ) $ufmatch->contact_id
;
518 * Get the uf_id given a contact_id
520 * @param int $contactID
521 * ID of the contact for which related uf_id is required.
523 * @return int uf_id of the given contact_id on success, null otherwise
526 public static function getUFId($contactID) {
527 if (!isset($contactID)) {
530 $domain = CRM_Core_BAO_Domain
::getDomain();
531 $ufmatch = new CRM_Core_DAO_UFMatch();
533 $ufmatch->contact_id
= $contactID;
534 $ufmatch->domain_id
= $domain->id
;
535 if ($ufmatch->find(TRUE)) {
536 return $ufmatch->uf_id
;
544 public static function isEmptyTable() {
545 $sql = "SELECT count(id) FROM civicrm_uf_match";
546 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
550 * Get the list of contact_id
553 * @return int contact_id on success, null otherwise
556 public 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
572 * @return bool true if allowed to login, false otherwise
575 public static function getAllowedToLogin($openId) {
576 $ufmatch = new CRM_Core_DAO_UFMatch();
577 $ufmatch->uf_name
= $openId;
578 $ufmatch->allowed_to_login
= 1;
579 if ($ufmatch->find(TRUE)) {
586 * Get the next unused uf_id value, since the standalone UF doesn't
587 * have id's (it uses OpenIDs, which go in a different field)
590 * @return int next highest unused value for uf_id
593 public 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
;
611 public static function isDuplicateUser($email) {
612 $session = CRM_Core_Session
::singleton();
613 $contactID = $session->get('userID');
614 if (!empty($email) && isset($contactID)) {
615 $dao = new CRM_Core_DAO_UFMatch();
616 $dao->uf_name
= $email;
617 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
625 * Get uf match values for given uf id or logged in user.
630 * return array $ufValues uf values.
635 public static function getUFValues($ufID = NULL) {
637 //get logged in user uf id.
638 $ufID = CRM_Utils_System
::getLoggedInUfID();
645 if ($ufID && !isset($ufValues[$ufID])) {
646 $ufmatch = new CRM_Core_DAO_UFMatch();
647 $ufmatch->uf_id
= $ufID;
648 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
649 if ($ufmatch->find(TRUE)) {
650 $ufValues[$ufID] = array(
651 'uf_id' => $ufmatch->uf_id
,
652 'uf_name' => $ufmatch->uf_name
,
653 'contact_id' => $ufmatch->contact_id
,
654 'domain_id' => $ufmatch->domain_id
,
658 return $ufValues[$ufID];