You are here

function fb_user_create_local_user in Drupal for Facebook 6.2

Same name and namespace in other branches
  1. 5.2 fb_user.module \fb_user_create_local_user()
  2. 5 fb_user.module \fb_user_create_local_user()
  3. 6.3 fb_user.module \fb_user_create_local_user()
  4. 7.3 fb_user.module \fb_user_create_local_user()

Creates a local Drupal account for the specified facebook user id.

Parameters

fbu: The facebook user id corresponding to this account.

edit: An associative array with user configuration. As would be passed to user_save().

1 call to fb_user_create_local_user()
fb_user_fb in ./fb_user.module
Implementation of hook_fb.

File

./fb_user.module, line 675
This module manages relations between local Drupal user accounts and their accounts on facebook.com.

Code

function fb_user_create_local_user($fb, $fb_app, $fbu, $edit = array()) {

  // Ensure $fbu is a real facebook user id.
  if (!$fbu || !is_numeric($fbu)) {
    return;
  }
  list($module, $authname) = _fb_user_get_authmap($fb_app, $fbu);
  $account = fb_user_get_local_user($fbu, $fb_app);
  if (!$account) {

    // Create a new user in our system
    // Learn some details from facebook.
    $infos = fb_users_getInfo(array(
      $fbu,
    ), $fb);
    $info = $infos[0];

    // All Drupal users get authenticated user role.
    $edit['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';

    // Ensure unique username.  Append "_N" if necessary.
    if (isset($edit['name']) && $edit['name']) {
      $username = $edit['name'];
    }
    else {
      $username = "{$fbu}@facebook";
      $edit['name'] = $username;
    }
    $i = 1;
    while (db_result(db_query("SELECT name FROM {users} WHERE name='%s'", $edit['name']))) {
      $i++;
      $edit['name'] = $username . '_' . $i;
    }

    // Allow third-party module to adjust any of our data before we create
    // the user.
    $edit = fb_invoke(FB_OP_PRE_USER, array(
      'fbu' => $fbu,
      'fb' => $GLOBALS['_fb'],
      'fb_app' => $fb_app,
      'info' => $info,
    ), $edit);

    // Fill in any default that are missing.
    $defaults = array(
      'pass' => user_password(),
      'init' => db_escape_string($edit['name']),
      'status' => 1,
      'authname_fb_user' => $authname,
    );

    // Mail available only if user has granted extended permission.
    if (isset($info['email']) && $info['email'] != $info['proxied_email']) {
      $defaults['mail'] = $info['email'];
    }

    // Merge defaults
    $edit = array_merge($defaults, $edit);

    // Confirm username is not taken.  FB_OP_PRE_USER may have changed it.
    if ($uid = db_result(db_query("SELECT uid FROM {users} WHERE name='%s'", $user_data_sanitized['name']))) {

      // The desired name is taken.
      watchdog('fb_user', 'Failed to create new user %name.  That name is already in the users table.', array(
        '%name' => $user_data_sanitized['name'],
      ), WATCHDOG_ERROR, l(t('view user'), 'user/' . $uid));
    }
    else {
      $account = user_save('', $edit);
      watchdog('fb_user', 'New user: %name %email.', array(
        '%name' => $account->name,
        '%email' => '<' . $account->mail . '>',
      ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

      // Allow third-party modules to act after account creation.
      fb_invoke(FB_OP_POST_USER, array(
        'account' => $account,
        'fb_app' => $fb_app,
        'fb' => $fb,
      ));
    }
  }
  return $account;
}