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
46 * @return \CRM_Core_DAO_UFMatch
48 public static function create($params) {
49 $hook = empty($params['id']) ?
'create' : 'edit';
50 CRM_Utils_Hook
::pre($hook, 'UFMatch', CRM_Utils_Array
::value('id', $params), $params);
51 if (empty($params['domain_id'])) {
52 $params['domain_id'] = CRM_Core_Config
::domainID();
54 $dao = new CRM_Core_DAO_UFMatch();
55 $dao->copyValues($params);
57 CRM_Utils_Hook
::post($hook, 'UFMatch', $dao->id
, $dao);
63 * Given a UF user object, make sure there is a contact
64 * object for this user. If the user has new values, we need
65 * to update the CRM DB with the new values
68 * The drupal user object.
70 * Has the user object been edited.
74 * @param bool $isLogin
78 public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
79 $userSystem = CRM_Core_Config
::singleton()->userSystem
;
80 $session = CRM_Core_Session
::singleton();
81 if (!is_object($session)) {
82 CRM_Core_Error
::fatal('wow, session is not an object?');
86 $userSystemID = $userSystem->getBestUFID($user);
87 $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
89 // if the id of the object is zero (true for anon users in drupal)
90 // have we already processed this user, if so early
92 $userID = $session->get('userID');
93 $ufID = $session->get('ufID');
95 if (!$update && $ufID == $userSystemID) {
99 //check do we have logged in user.
100 $isUserLoggedIn = CRM_Utils_System
::isUserLoggedIn();
102 // reset the session if we are a different user
103 if ($ufID && $ufID != $userSystemID) {
106 //get logged in user ids, and set to session.
107 if ($isUserLoggedIn) {
108 $userIds = self
::getUFValues();
109 $session->set('ufID', CRM_Utils_Array
::value('uf_id', $userIds, ''));
110 $session->set('userID', CRM_Utils_Array
::value('contact_id', $userIds, ''));
111 $session->set('ufUniqID', CRM_Utils_Array
::value('uf_name', $userIds, ''));
116 if ($userSystemID == 0) {
120 $ufmatch = self
::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
125 //make sure we have session w/ consistent ids.
126 $ufID = $ufmatch->uf_id
;
127 $userID = $ufmatch->contact_id
;
129 if ($isUserLoggedIn) {
130 $loggedInUserUfID = CRM_Utils_System
::getLoggedInUfID();
131 //are we processing logged in user.
132 if ($loggedInUserUfID && $loggedInUserUfID != $ufID) {
133 $userIds = self
::getUFValues($loggedInUserUfID);
134 $ufID = CRM_Utils_Array
::value('uf_id', $userIds, '');
135 $userID = CRM_Utils_Array
::value('contact_id', $userIds, '');
136 $ufUniqID = CRM_Utils_Array
::value('uf_name', $userIds, '');
140 //set user ids to session.
141 $session->set('ufID', $ufID);
142 $session->set('userID', $userID);
143 $session->set('ufUniqID', $ufUniqID);
145 // add current contact to recently viewed
146 if ($ufmatch->contact_id
) {
147 list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl)
148 = CRM_Contact_BAO_Contact
::getDisplayAndImage($ufmatch->contact_id
, TRUE, TRUE);
150 $otherRecent = array(
151 'imageUrl' => $contactImageUrl,
152 'subtype' => $contactSubtype,
153 'editUrl' => CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"),
156 CRM_Utils_Recent
::add($displayName,
157 CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"),
158 $ufmatch->contact_id
,
160 $ufmatch->contact_id
,
168 * Synchronize the object with the UF Match entry. Can be called stand-alone from
169 * the drupalUsers script
171 * @param Object $user
172 * The drupal user object.
173 * @param string $userKey
174 * The id of the user from the uf object.
175 * @param string $uniqId
176 * The OpenID of the user.
178 * The name of the user framework.
180 * Returns the status if user created or already exits (used for CMS sync).
181 * @param string $ctype
183 * @param bool $isLogin
185 * @return CRM_Core_DAO_UFMatch|bool
187 public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) {
188 $config = CRM_Core_Config
::singleton();
190 if (!CRM_Utils_Rule
::email($uniqId)) {
191 $retVal = $status ?
NULL : FALSE;
197 // make sure that a contact id exists for this user id
198 $ufmatch = new CRM_Core_DAO_UFMatch();
199 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
200 $ufmatch->uf_id
= $userKey;
202 if (!$ufmatch->find(TRUE)) {
203 $transaction = new CRM_Core_Transaction();
206 if (!empty($_POST) && !$isLogin) {
208 $params['email'] = $uniqId;
210 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
211 $dedupeParams['check_permission'] = FALSE;
212 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
215 CRM_Core_BAO_Setting
::getItem(
216 CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
217 'uniq_email_per_site'
220 // restrict dupeIds to ones that belong to current domain/site.
221 $siteContacts = CRM_Core_BAO_Domain
::getContactList();
222 foreach ($ids as $index => $dupeId) {
223 if (!in_array($dupeId, $siteContacts)) {
227 // re-index the array
228 $ids = array_values($ids);
231 $dao = new CRM_Core_DAO();
232 $dao->contact_id
= $ids[0];
236 $dao = CRM_Contact_BAO_Contact
::matchContactOnEmail($uniqId, $ctype);
241 // ensure there does not exists a contact_id / uf_id pair
242 // in the DB. This might be due to multiple emails per contact
246 FROM civicrm_uf_match
247 WHERE contact_id = %1
251 1 => array($dao->contact_id
, 'Integer'),
252 2 => array(CRM_Core_Config
::domainID(), 'Integer'),
254 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
258 $ufmatch->contact_id
= $dao->contact_id
;
259 $ufmatch->uf_name
= $uniqId;
264 if ($config->userSystem
->is_drupal
) {
267 elseif ($uf == 'WordPress') {
268 $mail = 'user_email';
274 if (is_object($user)) {
275 $params = array('email-Primary' => $user->$mail);
278 if ($ctype == 'Organization') {
279 $params['organization_name'] = $uniqId;
281 elseif ($ctype == 'Household') {
282 $params['household_name'] = $uniqId;
286 $ctype = "Individual";
288 $params['contact_type'] = $ctype;
290 // extract first / middle / last name
292 if ($uf == 'Joomla' && $user->name
) {
293 CRM_Utils_String
::extractName($user->name
, $params);
296 if ($uf == 'WordPress') {
297 if ($user->first_name
) {
298 $params['first_name'] = $user->first_name
;
301 if ($user->last_name
) {
302 $params['last_name'] = $user->last_name
;
306 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
307 $ufmatch->contact_id
= $contactId;
308 $ufmatch->uf_name
= $uniqId;
311 // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm
312 // user has two e-mails and there is a cms match for each of them
313 // the gets rid of the nasty fata error but still reports the error
316 FROM civicrm_uf_match
317 WHERE ( contact_id = %1
323 1 => array($ufmatch->contact_id
, 'Integer'),
324 2 => array($ufmatch->uf_name
, 'String'),
325 3 => array($ufmatch->uf_id
, 'Integer'),
326 4 => array($ufmatch->domain_id
, 'Integer'),
329 $conflict = CRM_Core_DAO
::singleValueQuery($sql, $params);
332 $ufmatch = CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
335 $transaction->commit();
338 $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4",
340 1 => $ufmatch->contact_id
,
342 3 => $ufmatch->uf_id
,
359 * Update the uf_name in the user object
361 * @param int $contactId
362 * Id of the contact to update.
366 public static function updateUFName($contactId) {
370 $config = CRM_Core_Config
::singleton();
371 $ufName = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
379 // 1.do check for contact Id.
380 $ufmatch = new CRM_Core_DAO_UFMatch();
381 $ufmatch->contact_id
= $contactId;
382 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
383 if (!$ufmatch->find(TRUE)) {
386 if ($ufmatch->uf_name
!= $ufName) {
391 // 2.do check for duplicate ufName.
392 $ufDupeName = new CRM_Core_DAO_UFMatch();
393 $ufDupeName->uf_name
= $ufName;
394 $ufDupeName->domain_id
= CRM_Core_Config
::domainID();
395 if ($ufDupeName->find(TRUE) &&
396 $ufDupeName->contact_id
!= $contactId
405 // save the updated ufmatch object
406 $ufmatch->uf_name
= $ufName;
408 $config->userSystem
->updateCMSName($ufmatch->uf_id
, $ufName);
412 * Update the email value for the contact and user profile
414 * @param int $contactId
415 * Contact ID of the user.
416 * @param $emailAddress
417 * Email to be modified for the user.
421 public static function updateContactEmail($contactId, $emailAddress) {
422 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
423 $emailAddress = $strtolower($emailAddress);
425 $ufmatch = new CRM_Core_DAO_UFMatch();
426 $ufmatch->contact_id
= $contactId;
427 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
428 if ($ufmatch->find(TRUE)) {
429 // Save the email in UF Match table
430 $ufmatch->uf_name
= $emailAddress;
431 CRM_Core_BAO_UFMatch
::create((array) $ufmatch);
433 //check if the primary email for the contact exists
434 //$contactDetails[1] - email
435 //$contactDetails[3] - email id
436 $contactDetails = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
438 if (trim($contactDetails[1])) {
439 $emailID = $contactDetails[3];
440 //update if record is found
441 $query = "UPDATE civicrm_email
445 1 => array($emailAddress, 'String'),
446 2 => array($emailID, 'Integer'),
448 $dao = CRM_Core_DAO
::executeQuery($query, $p);
451 //else insert a new email record
452 $email = new CRM_Core_DAO_Email();
453 $email->contact_id
= $contactId;
454 $email->is_primary
= 1;
455 $email->email
= $emailAddress;
457 $emailID = $email->id
;
460 CRM_Core_BAO_Log
::register($contactId,
468 * Delete the object records that are associated with this cms user
471 * Id of the user to delete.
475 public static function deleteUser($ufID) {
476 $ufmatch = new CRM_Core_DAO_UFMatch();
478 $ufmatch->uf_id
= $ufID;
479 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
484 * Get the contact_id given a uf_id
487 * Id of UF for which related contact_id is required.
490 * contact_id on success, null otherwise
492 public static function getContactId($ufID) {
497 $ufmatch = new CRM_Core_DAO_UFMatch();
499 $ufmatch->uf_id
= $ufID;
500 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
501 if ($ufmatch->find(TRUE)) {
502 return (int ) $ufmatch->contact_id
;
508 * Get the uf_id given a contact_id
510 * @param int $contactID
511 * ID of the contact for which related uf_id is required.
514 * uf_id of the given contact_id on success, null otherwise
516 public static function getUFId($contactID) {
517 if (!isset($contactID)) {
520 $domain = CRM_Core_BAO_Domain
::getDomain();
521 $ufmatch = new CRM_Core_DAO_UFMatch();
523 $ufmatch->contact_id
= $contactID;
524 $ufmatch->domain_id
= $domain->id
;
525 if ($ufmatch->find(TRUE)) {
526 return $ufmatch->uf_id
;
534 public static function isEmptyTable() {
535 $sql = "SELECT count(id) FROM civicrm_uf_match";
536 return CRM_Core_DAO
::singleValueQuery($sql) > 0 ?
FALSE : TRUE;
540 * Get the list of contact_id
544 * contact_id on success, null otherwise
546 public static function getContactIDs() {
548 $dao = new CRM_Core_DAO_UFMatch();
550 while ($dao->fetch()) {
551 $id[] = $dao->contact_id
;
557 * See if this user exists, and if so, if they're allowed to login
563 * true if allowed to login, false otherwise
565 public static function getAllowedToLogin($openId) {
566 $ufmatch = new CRM_Core_DAO_UFMatch();
567 $ufmatch->uf_name
= $openId;
568 $ufmatch->allowed_to_login
= 1;
569 if ($ufmatch->find(TRUE)) {
576 * Get the next unused uf_id value, since the standalone UF doesn't
577 * have id's (it uses OpenIDs, which go in a different field)
581 * next highest unused value for uf_id
583 public static function getNextUfIdValue() {
584 $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match";
585 $dao = CRM_Core_DAO
::executeQuery($query);
587 $ufId = $dao->next_uf_id
;
601 public static function isDuplicateUser($email) {
602 $session = CRM_Core_Session
::singleton();
603 $contactID = $session->get('userID');
604 if (!empty($email) && isset($contactID)) {
605 $dao = new CRM_Core_DAO_UFMatch();
606 $dao->uf_name
= $email;
607 if ($dao->find(TRUE) && $contactID != $dao->contact_id
) {
615 * Get uf match values for given uf id or logged in user.
623 public static function getUFValues($ufID = NULL) {
625 //get logged in user uf id.
626 $ufID = CRM_Utils_System
::getLoggedInUfID();
633 if ($ufID && !isset($ufValues[$ufID])) {
634 $ufmatch = new CRM_Core_DAO_UFMatch();
635 $ufmatch->uf_id
= $ufID;
636 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
637 if ($ufmatch->find(TRUE)) {
638 $ufValues[$ufID] = array(
639 'uf_id' => $ufmatch->uf_id
,
640 'uf_name' => $ufmatch->uf_name
,
641 'contact_id' => $ufmatch->contact_id
,
642 'domain_id' => $ufmatch->domain_id
,
646 return $ufValues[$ufID];