You are here

function fb_user_create_local_user in Drupal for Facebook 7.3

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. 6.2 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_create_local_account in ./fb_user.module
Helper function to create local account for the currently authorized user.

File

./fb_user.module, line 1006
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;
  }
  $account = fb_user_get_local_user($fbu);
  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';
    if (isset($edit['name']) && $edit['name']) {
      $username = $edit['name'];
    }
    else {

      // Fallback, should never be reached.
      $username = "{$fbu}@facebook";
      $edit['name'] = $username;
    }
    $i = 0;

    // Keep looking until we find a username_n that isn't being used.
    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(
      'fbu' => $fbu,
      'fb' => $GLOBALS['_fb'],
      'fb_app' => $fb_app,
      'info' => $info,
    ), $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' => $fbu . '@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($info['email'])) {
        $defaults['mail'] = $info['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, $fbu);
        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,
          'fb_app' => $fb_app,
          'fb' => $fb,
        ), NULL, 'fb_user');
      }
    }
  }
  return $account;
}