3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 * Synchronizing cms users with CiviCRM contacts.
50 * @param bool $is_interactive
51 * Whether to show statuses & perform redirects.
52 * This behavior is misplaced in the BAO layer, but we'll preserve it to avoid
53 * contract changes in the middle of the support cycle. In the next major
54 * release, we should remove & document it.
59 public static function synchronize($is_interactive = TRUE) {
60 //start of schronization code
61 $config = CRM_Core_Config
::singleton();
63 // Build an array of rows from UF users table.
65 if ($config->userSystem
->is_drupal
== '1') {
70 $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
72 if ($config->userFramework
== 'Drupal') {
73 while ($row = $result->fetchAssoc()) {
77 elseif ($config->userFramework
== 'Drupal6') {
78 while ($row = db_fetch_array($result)) {
83 elseif ($config->userFramework
== 'Joomla') {
87 // TODO: Insert code here to populate $rows for Joomla;
89 elseif ($config->userFramework
== 'WordPress') {
94 CRM_Core_Error
::fatal('CMS user creation not supported for this framework');
99 if ($config->userSystem
->is_drupal
== '1') {
100 $user = new StdClass();
101 $uf = $config->userFramework
;
104 $contactMatching = 0;
105 foreach ($rows as $row) {
106 $user->$id = $row[$id];
107 $user->$mail = $row[$mail];
108 $user->$name = $row[$name];
110 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
116 if (is_object($match)) {
121 elseif ($config->userFramework
== 'Joomla') {
123 $JUserTable = &JTable
::getInstance('User', 'JTable');
125 $db = $JUserTable->getDbo();
126 $query = $db->getQuery(TRUE);
127 $query->select($id . ', ' . $mail . ', ' . $name);
128 $query->from($JUserTable->getTableName());
129 $query->where($mail != '');
131 $db->setQuery($query, 0, $limit);
132 $users = $db->loadObjectList();
134 $user = new StdClass();
135 $uf = $config->userFramework
;
138 $contactMatching = 0;
139 for ($i = 0; $i < count($users); $i++
) {
140 $user->$id = $users[$i]->$id;
141 $user->$mail = $users[$i]->$mail;
142 $user->$name = $users[$i]->$name;
144 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,
189 if (is_object($match)) {
194 //end of synchronization code
196 if ($is_interactive) {
197 $status = ts('Synchronize Users to Contacts completed.');
198 $status .= ' ' . ts('Checked one user record.',
200 'count' => $contactCount,
201 'plural' => 'Checked %count user records.',
204 if ($contactMatching) {
205 $status .= ' ' . ts('Found one matching contact record.',
207 'count' => $contactMatching,
208 'plural' => 'Found %count matching contact records.',
213 $status .= ' ' . ts('Created one new contact record.',
215 'count' => $contactCreated,
216 'plural' => 'Created %count new contact records.',
219 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
220 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
225 * Create CMS user using Profile.
227 * @param array $params
228 * @param string $mail
229 * Email id for cms user.
232 * contact id that has been created
234 public static function create(&$params, $mail) {
235 $config = CRM_Core_Config
::singleton();
237 $ufID = $config->userSystem
->createUser($params, $mail);
239 //if contact doesn't already exist create UF Match
240 if ($ufID !== FALSE &&
241 isset($params['contactID'])
243 // create the UF Match record
244 $ufmatch['uf_id'] = $ufID;
245 $ufmatch['contact_id'] = $params['contactID'];
246 $ufmatch['uf_name'] = $params[$mail];
247 CRM_Core_BAO_UFMatch
::create($ufmatch);
254 * Create Form for CMS user using Profile.
256 * @param CRM_Core_Form $form
258 * Id of group of profile.
259 * @param bool $emailPresent
260 * True if the profile field has email(primary).
261 * @param \const|int $action
267 public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action
::NONE
) {
268 $config = CRM_Core_Config
::singleton();
271 $isDrupal = $config->userSystem
->is_drupal
;
272 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
273 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
275 //if CMS is configured for not to allow creating new CMS user,
276 //don't build the form,Fixed for CRM-4036
278 $userParams = JComponentHelper
::getParams('com_users');
279 if (!$userParams->get('allowUserRegistration')) {
283 elseif ($isDrupal && !variable_get('user_register', TRUE)) {
286 elseif ($isWordPress && !get_option('users_can_register')) {
291 $isCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user');
294 // $cms is true when there is email(primary location) is set in the profile field.
295 $session = CRM_Core_Session
::singleton();
296 $userID = $session->get('userID');
297 $showUserRegistration = FALSE;
299 $showUserRegistration = TRUE;
301 elseif (!$action && !$userID) {
302 $showUserRegistration = TRUE;
305 if ($isCMSUser && $emailPresent) {
306 if ($showUserRegistration) {
307 if ($isCMSUser != 2) {
309 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
311 $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra);
315 $form->add('hidden', 'cms_create_account', 1);
319 $form->assign('isCMS', $required);
320 if (!$userID ||
$action & CRM_Core_Action
::PREVIEW ||
$action & CRM_Core_Action
::PROFILE
) {
321 $form->add('text', 'cms_name', ts('Username'), NULL, $required);
322 if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) {
323 $form->add('password', 'cms_pass', ts('Password'));
324 $form->add('password', 'cms_confirm_pass', ts('Confirm Password'));
327 $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form);
333 $destination = $config->userSystem
->getLoginDestination($form);
334 $loginURL = $config->userSystem
->getLoginURL($destination);
335 $form->assign('loginURL', $loginURL);
336 $form->assign('showCMS', $showCMS);
340 * Checks that there is a valid username & email
341 * optionally checks password is present & matches DB & gets the CMS to validate
343 * @param array $fields
344 * Posted values of form.
345 * @param array $files
346 * Uploaded files if any.
347 * @param CRM_Core_Form $form
351 public static function formRule($fields, $files, $form) {
352 if (empty($fields['cms_create_account'])) {
356 $config = CRM_Core_Config
::singleton();
358 $isDrupal = $config->userSystem
->is_drupal
;
359 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
360 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
363 if ($isDrupal ||
$isJoomla ||
$isWordPress) {
365 if (!empty($form->_bltID
) && array_key_exists("email-{$form->_bltID}", $fields)) {
366 // this is a transaction related page
367 $emailName = 'email-' . $form->_bltID
;
370 // find the email field in a profile page
371 foreach ($fields as $name => $dontCare) {
372 if (substr($name, 0, 5) == 'email') {
379 if ($emailName == NULL) {
380 $errors['_qf_default'] == ts('Could not find an email address.');
384 if (empty($fields['cms_name'])) {
385 $errors['cms_name'] = ts('Please specify a username.');
388 if (empty($fields[$emailName])) {
389 $errors[$emailName] = ts('Please specify a valid email address.');
392 if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) {
393 if (empty($fields['cms_pass']) ||
394 empty($fields['cms_confirm_pass'])
396 $errors['cms_pass'] = ts('Please enter a password.');
398 if ($fields['cms_pass'] != $fields['cms_confirm_pass']) {
399 $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.');
403 if (!empty($errors)) {
407 // now check that the cms db does not have the user name and/or email
408 if ($isDrupal OR $isJoomla OR $isWordPress) {
410 'name' => $fields['cms_name'],
411 'mail' => $fields[$emailName],
415 $config->userSystem
->checkUserNameEmailExists($params, $errors, $emailName);
417 return (!empty($errors)) ?
$errors : TRUE;
422 * This function is not used anywhere
424 * @param array $contact
425 * Array of contact-details.
428 * uid if user exists, false otherwise
431 public static function userExists(&$contact) {
432 $config = CRM_Core_Config
::singleton();
434 $isDrupal = $config->userSystem
->is_drupal
;
435 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
436 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
438 if (!$isDrupal && !$isJoomla && !$isWordPress) {
439 die('Unknown user framework');
442 // Use UF native framework to fetch data from UF user table
445 "SELECT uid FROM {users} where mail = :email",
446 array(':email' => $contact['email'])
450 $contact['user_exists'] = TRUE;
455 $mail = $contact['email'];
457 $JUserTable = &JTable
::getInstance('User', 'JTable');
459 $db = $JUserTable->getDbo();
460 $query = $db->getQuery(TRUE);
461 $query->select('username, email');
462 $query->from($JUserTable->getTableName());
463 $query->where('(LOWER(email) = LOWER(\'' . $email . '\'))');
464 $db->setQuery($query, 0, $limit);
465 $users = $db->loadAssocList();
473 $uid = CRM_Utils_Array
::value('id', $row);
474 $contact['user_exists'] = TRUE;
478 elseif ($isWordPress) {
479 if (email_exists($params['mail'])) {
480 $contact['user_exists'] = TRUE;
481 $userObj = get_user_by('email', $params['mail']);
494 public static function &dbHandle(&$config) {
495 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
496 $db_uf = DB
::connect($config->userFrameworkDSN
);
501 $session = CRM_Core_Session
::singleton();
502 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
503 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",
504 array(1 => $db_uf->getMessage())
507 $db_uf->query('/*!40101 SET NAMES utf8 */');