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 * this file contains functions for synchronizing cms users with CiviCRM contacts
40 require_once 'DB.php';
43 * Class CRM_Core_BAO_CMSUser
45 class CRM_Core_BAO_CMSUser
{
48 * Function for synchronizing cms users with CiviCRM contacts
50 * @param bool $is_interactive whether to show statuses & perform redirects
51 * This behavior is misplaced in the BAO layer, but we'll preserve it to avoid
52 * contract changes in the middle of the support cycle. In the next major
53 * release, we should remove & document it.
60 static function synchronize($is_interactive = TRUE) {
61 //start of schronization code
62 $config = CRM_Core_Config
::singleton();
64 // Build an array of rows from UF users table.
66 if ($config->userSystem
->is_drupal
== '1') {
71 $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
73 if ($config->userFramework
== 'Drupal') {
74 while ($row = $result->fetchAssoc()) {
78 elseif ($config->userFramework
== 'Drupal6') {
79 while ($row = db_fetch_array($result)) {
84 elseif ($config->userFramework
== 'Joomla') {
88 // TODO: Insert code here to populate $rows for Joomla;
90 elseif ($config->userFramework
== 'WordPress') {
95 CRM_Core_Error
::fatal('CMS user creation not supported for this framework');
100 if ($config->userSystem
->is_drupal
== '1') {
101 $user = new StdClass();
102 $uf = $config->userFramework
;
105 $contactMatching = 0;
106 foreach ($rows as $row) {
107 $user->$id = $row[$id];
108 $user->$mail = $row[$mail];
109 $user->$name = $row[$name];
111 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
117 if (is_object($match)) {
122 elseif ($config->userFramework
== 'Joomla') {
124 $JUserTable = &JTable
::getInstance('User', 'JTable');
126 $db = $JUserTable->getDbo();
127 $query = $db->getQuery(TRUE);
128 $query->select($id . ', ' . $mail . ', ' . $name);
129 $query->from($JUserTable->getTableName());
130 $query->where($mail != '');
132 $db->setQuery($query, 0, $limit);
133 $users = $db->loadObjectList();
135 $user = new StdClass();
136 $uf = $config->userFramework
;
139 $contactMatching = 0;
140 for ($i = 0; $i < count($users); $i++
) {
141 $user->$id = $users[$i]->$id;
142 $user->$mail = $users[$i]->$mail;
143 $user->$name = $users[$i]->$name;
145 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user,
158 if (is_object($match)) {
163 elseif ($config->userFramework
== 'WordPress') {
164 $uf = $config->userFramework
;
167 $contactMatching = 0;
170 $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
172 foreach ($wpUserIds as $wpUserId) {
173 $wpUserData = get_userdata($wpUserId);
175 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($wpUserData,
188 if (is_object($match)) {
193 //end of synchronization code
195 if ($is_interactive) {
196 $status = ts('Synchronize Users to Contacts completed.');
197 $status .= ' ' . ts('Checked one user record.',
199 'count' => $contactCount,
200 'plural' => 'Checked %count user records.'
203 if ($contactMatching) {
204 $status .= ' ' . ts('Found one matching contact record.',
206 'count' => $contactMatching,
207 'plural' => 'Found %count matching contact records.'
212 $status .= ' ' . ts('Created one new contact record.',
214 'count' => $contactCreated,
215 'plural' => 'Created %count new contact records.'
218 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
219 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
224 * Function to create CMS user using Profile
226 * @param array $params associated array
227 * @param string $mail email id for cms user
229 * @return int contact id that has been created
233 static function create(&$params, $mail) {
234 $config = CRM_Core_Config
::singleton();
236 $ufID = $config->userSystem
->createUser($params, $mail);
238 //if contact doesn't already exist create UF Match
239 if ($ufID !== FALSE &&
240 isset($params['contactID'])
242 // create the UF Match record
243 $ufmatch = new CRM_Core_DAO_UFMatch();
244 $ufmatch->domain_id
= CRM_Core_Config
::domainID();
245 $ufmatch->uf_id
= $ufID;
246 $ufmatch->contact_id
= $params['contactID'];
247 $ufmatch->uf_name
= $params[$mail];
249 if (!$ufmatch->find(TRUE)) {
258 * Function to create Form for CMS user using Profile
260 * @param object $form
261 * @param integer $gid id of group of profile
262 * @param bool $emailPresent true if the profile field has email(primary)
263 * @param \const|int $action
265 * @return FALSE|void WTF
270 static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action
::NONE
) {
271 $config = CRM_Core_Config
::singleton();
274 $isDrupal = $config->userSystem
->is_drupal
;
275 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
276 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
278 //if CMS is configured for not to allow creating new CMS user,
279 //don't build the form,Fixed for CRM-4036
281 $userParams = JComponentHelper
::getParams('com_users');
282 if (!$userParams->get('allowUserRegistration')) {
286 elseif ($isDrupal && !variable_get('user_register', TRUE)) {
289 elseif ($isWordPress && !get_option('users_can_register')) {
294 $isCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user');
297 // $cms is true when there is email(primary location) is set in the profile field.
298 $session = CRM_Core_Session
::singleton();
299 $userID = $session->get('userID');
300 $showUserRegistration = FALSE;
302 $showUserRegistration = TRUE;
304 elseif (!$action && !$userID) {
305 $showUserRegistration = TRUE;
308 if ($isCMSUser && $emailPresent) {
309 if ($showUserRegistration) {
310 if ($isCMSUser != 2) {
312 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
314 $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra);
318 $form->add('hidden', 'cms_create_account', 1);
322 $form->assign('isCMS', $required);
323 if (!$userID ||
$action & CRM_Core_Action
::PREVIEW ||
$action & CRM_Core_Action
::PROFILE
) {
324 $form->add('text', 'cms_name', ts('Username'), NULL, $required);
325 if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) {
326 $form->add('password', 'cms_pass', ts('Password'));
327 $form->add('password', 'cms_confirm_pass', ts('Confirm Password'));
330 $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form);
336 $destination = $config->userSystem
->getLoginDestination($form);
337 $loginURL = $config->userSystem
->getLoginURL($destination);
338 $form->assign('loginURL', $loginURL);
339 $form->assign('showCMS', $showCMS);
343 * Checks that there is a valid username & email
344 * optionally checks password is present & matches DB & gets the CMS to validate
346 * @params array $fields Posted values of form
347 * @param array $files uploaded files if any
348 * @param array $self reference to form object
358 static function formRule($fields, $files, $self) {
359 if (empty($fields['cms_create_account'])) {
363 $config = CRM_Core_Config
::singleton();
365 $isDrupal = $config->userSystem
->is_drupal
;
366 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
367 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
370 if ($isDrupal ||
$isJoomla ||
$isWordPress) {
372 if (!empty($self->_bltID
) && array_key_exists("email-{$self->_bltID}", $fields)) {
373 // this is a transaction related page
374 $emailName = 'email-' . $self->_bltID
;
376 // find the email field in a profile page
377 foreach ($fields as $name => $dontCare) {
378 if (substr($name, 0, 5) == 'email') {
385 if ($emailName == NULL) {
386 $errors['_qf_default'] == ts('Could not find an email address.');
390 if (empty($fields['cms_name'])) {
391 $errors['cms_name'] = ts('Please specify a username.');
394 if (empty($fields[$emailName])) {
395 $errors[$emailName] = ts('Please specify a valid email address.');
398 if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) {
399 if (empty($fields['cms_pass']) ||
400 empty($fields['cms_confirm_pass'])
402 $errors['cms_pass'] = ts('Please enter a password.');
404 if ($fields['cms_pass'] != $fields['cms_confirm_pass']) {
405 $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.');
409 if (!empty($errors)) {
413 // now check that the cms db does not have the user name and/or email
414 if ($isDrupal OR $isJoomla OR $isWordPress) {
416 'name' => $fields['cms_name'],
417 'mail' => $fields[$emailName],
421 $config->userSystem
->checkUserNameEmailExists($params, $errors, $emailName);
423 return (!empty($errors)) ?
$errors : TRUE;
427 * Function to check if a cms user already exists.
429 * @param Array $contact array of contact-details
431 * @return uid if user exists, false otherwise
436 static function userExists(&$contact) {
437 $config = CRM_Core_Config
::singleton();
439 $isDrupal = $config->userSystem
->is_drupal
;
440 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
441 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
443 if (!$isDrupal && !$isJoomla && !$isWordPress) {
444 die('Unknown user framework');
447 // Use UF native framework to fetch data from UF user table
450 "SELECT uid FROM {users} where mail = :email",
451 array(':email' => $contact['email'])
455 $contact['user_exists'] = TRUE;
460 $mail = $contact['email'];
462 $JUserTable = &JTable
::getInstance('User', 'JTable');
464 $db = $JUserTable->getDbo();
465 $query = $db->getQuery(TRUE);
466 $query->select('username, email');
467 $query->from($JUserTable->getTableName());
468 $query->where('(LOWER(email) = LOWER(\'' . $email . '\'))');
469 $db->setQuery($query, 0, $limit);
470 $users = $db->loadAssocList();
478 $uid = CRM_Utils_Array
::value('id', $row);
479 $contact['user_exists'] = TRUE;
483 elseif ($isWordPress) {
484 if (email_exists($params['mail'])) {
485 $contact['user_exists'] = TRUE;
486 $userObj = get_user_by('email', $params['mail']);
499 static function &dbHandle(&$config) {
500 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
501 $db_uf = DB
::connect($config->userFrameworkDSN
);
506 $session = CRM_Core_Session
::singleton();
507 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
508 CRM_Core_Error
::statusBounce(ts("Cannot connect to UF db via %1. Please check the CIVICRM_UF_DSN value in your civicrm.settings.php file",
509 array(1 => $db_uf->getMessage())
512 $db_uf->query('/*!40101 SET NAMES utf8 */');