You are here

function joomla_authenticate in Joomla to Drupal 7

Same name and namespace in other branches
  1. 6 joomla.module \joomla_authenticate()
  2. 7.2 joomla.module \joomla_authenticate()
1 call to joomla_authenticate()
joomla_login_validate in ./joomla.module

File

./joomla.module, line 978
The joomla module used for migrate Joomla to Drupal.

Code

function joomla_authenticate($form_values = array(), &$form_uid) {
  global $user;
  if (!empty($user->uid) || $form_uid) {

    // User has already sucessfully authenticated
    return;
  }
  if (form_get_errors() || empty($form_values['name']) || empty($form_values['pass'])) {
    return;
  }
  $account = user_load_by_name($form_values['name']);

  // The user doesn't exist
  if (!$account) {
    return;
  }

  // See if the user has a password record from Joomla import
  $joomla_user = db_query('SELECT * FROM {joomla_users} WHERE uid = :uid', array(
    ':uid' => $account->uid,
  ))
    ->fetch();
  if (!$joomla_user) {
    return;
  }

  /**
   * If the password doesn't contain a colon, it is an unsalted password.
   * It will have been inserted into the drupal users table during the
   * import, and to get here the Drupal login must have already failed
   * against it, so nothing left to do
   */
  if (strpos($joomla_user->password, ':')) {
    list($password, $salt) = explode(':', $joomla_user->password, 2);
  }
  else {
    $password = $joomla_user->password;
    $salt = '';
  }

  // Check the supplied password against the md5sum
  if (md5($form_values['pass'] . $salt) == $password || !$salt && md5($form_values['pass']) == $password) {
    $user = $account;
    watchdog('joomla', 'Converting password for user @name (Joomla id @juid)', array(
      '@name' => $user->name,
      '@juid' => $joomla_user->juid,
    ));

    // Update the users Drupal password
    user_save($user, array(
      'pass' => $form_values['pass'],
    ));
    $joomla_user->converted = 1;
    drupal_write_record('joomla_users', $joomla_user, array(
      'uid',
    ));
    $form_uid = $user->uid;
    user_login_finalize($form_values);
    return $user;
  }
}