You are here

function _connector_log_in in Connector 6

Same name and namespace in other branches
  1. 7 connector.module \_connector_log_in()

File

./connector.module, line 334
Connector module

Code

function _connector_log_in($connector_name, $cid = NULL) {
  global $user;
  if (user_is_logged_in()) {
    return FALSE;
  }
  $connector = _connector_get_connectors($connector_name);
  if (!$connector) {
    return FALSE;
  }

  //Fetch connector ID
  if ($cid === NULL && isset($connector['id callback']) && is_callable($connector['id callback'])) {
    $cid = call_user_func($connector['id callback'], $connector);
  }
  if ($cid !== NULL) {
    $authname = $connector_name . '__' . $cid;
    $account = user_external_load($authname);
    if (!$account) {
      if (variable_get('user_register', 1)) {

        // Mostly copied from user_external_login_register - because it doesn't check user_register
        // Register this new user.
        $userinfo = array(
          'name' => $authname,
          'pass' => user_password(),
          'init' => $authname,
          'status' => variable_get('user_register', 1) == 1,
          'access' => time(),
        );
        $new_account = user_save('', $userinfo);

        // Terminate if an error occured during user_save().
        if (!$new_account) {
          drupal_set_message(t("Error saving user account."), 'error');
        }
        else {
          db_query("INSERT INTO {authmap} (uid, authname, module) VALUES (%d, '%s','connector')", $new_account->uid, $authname);
          _connector_set_primary_connection($new_account->uid, $authname);
          _connector_information_update($new_account);
          if ($new_account->status) {
            $user = $new_account;
            return TRUE;
          }
          else {
            drupal_set_message(t('Your account is currently pending approval by the site administrator.'), 'warning');
            if (isset($connector['logout callback']) && is_callable($connector['logout callback'])) {
              call_user_func($connector['logout callback'], $connector, $connection->cid);
            }
          }
          watchdog('user', 'New external user: %name using module %module.', array(
            '%name' => $authname,
            '%module' => 'connector',
          ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $new_account->uid . '/edit'));
        }
      }
      else {
        drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
        if (isset($connector['logout callback']) && is_callable($connector['logout callback'])) {
          call_user_func($connector['logout callback'], $connector, $connection->cid);
        }
      }
    }
    else {

      //Log in user
      if ($account->status) {
        $result = user_external_login($account);
        if ($result) {
          return TRUE;
        }
      }
      else {
        drupal_set_message(t('Your account is currently pending approval by the site administrator.'), 'warning');
        if (isset($connector['logout callback']) && is_callable($connector['logout callback'])) {
          call_user_func($connector['logout callback'], $connector, $connection->cid);
        }
      }
    }
  }
  return FALSE;
}