You are here

function _fb_user_create_local_account in Drupal for Facebook 7.4

Same name and namespace in other branches
  1. 6.3 fb_user.module \_fb_user_create_local_account()
  2. 7.3 fb_user.module \_fb_user_create_local_account()
1 call to _fb_user_create_local_account()
_fb_user_process_new_token in ./fb_user.module

File

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

Code

function _fb_user_create_local_account($me, $edit = array()) {
  $username = !empty($edit['name']) ? $edit['name'] : NULL;
  if (!$username) {

    // Name the new user.
    if (variable_get(FB_USER_VAR_USERNAME_STYLE, FB_USER_OPTION_USERNAME_FULL) == FB_USER_OPTION_USERNAME_FULL && $me['name']) {
      $username = $me['name'];
    }
    elseif ($me['id']) {

      // Create a name that is likely to be unique.
      $username = $me['id'] . '@facebook';
    }
    else {
      throw new Exception(t('Failed to learn user name from facebook.'));
    }
  }

  // Facebook names are not unique but Drupal names must be.
  // Append number until we find a username_n that isn't being used.
  $edit['name'] = $username;
  while (db_query("SELECT 1 FROM {users} WHERE name = :name", array(
    ':name' => $edit['name'],
  ))
    ->fetchField(0)) {
    $i++;
    $edit['name'] = $username . '_' . $i;
  }

  // Give modules a way to suppress new account creation.
  $edit['fb_user_do_create'] = TRUE;

  // Allow third-party module to adjust any of our data before we create
  // the user.
  $edit = fb_invoke(FB_USER_OP_PRE_USER, array(
    'me' => $me,
  ), $edit, 'fb_user');
  if ($edit['fb_user_do_create']) {
    unset($edit['fb_user_do_create']);

    // Don't confuse user_save.
    // Fill in any default that are missing.
    $defaults = array(
      'pass' => user_password(),
      'init' => $me['id'] . '@facebook',
      // Supposed to be email, but we may not know it.
      'status' => 1,
    );

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

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

    // Confirm username is not taken. FB_USER_OP_PRE_USER may have changed it.
    if ($uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(
      ':name' => $edit['name'],
    ))
      ->fetchField(0)) {

      // The desired name is taken.
      watchdog('fb_user', 'Failed to create new user %name. That name is already in the users table.', array(
        '%name' => $edit['name'],
      ), WATCHDOG_ERROR, l(t('view user'), 'user/' . $uid));
    }
    else {
      $account = user_save('', $edit);
      _fb_user_set_map($account, $me['id']);
      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_USER_OP_POST_USER, array(
        'account' => $account,
      ), NULL, 'fb_user');
      return $account;
    }
  }
}