You are here

function fb_user_create_local_user in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 5 fb_user.module \fb_user_create_local_user()
  2. 6.3 fb_user.module \fb_user_create_local_user()
  3. 6.2 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.

config: An associative array with user configuration. Possible values include: 'app_specific' - Set to true if the same facebook id might correspond to different local accounts, depending on which apps the user has used. Set to false if the user shares one local account across facebook apps. 'roles' - an array with keys corresponding to role ids the new user should receive.

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

File

./fb_user.module, line 680
This module allows Drupal user records to be associated with Facebook user ids. It can create local user accounts when Facebook users visit an application's canvas pages.

Code

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

  // TODO: ensure $fbu is a real user, not FB_FB_ANY or FB_FBU_CURRENT
  // debugging.

  //drupal_set_message("Facebook knows you as $username ($fbu)");
  list($module, $authname) = _fb_user_get_authmap($fb_app, $fbu);
  $account = user_external_load($authname);
  if (!$account) {

    // Create a new user in our system
    // Default username is authname, usernames must be unique.
    $config['username'] = $authname;

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

    // TODO: double-check that username is not taken.
    $user_default = array(
      'name' => $config['username'],
      'pass' => user_password(),
      'init' => db_escape_string($config['username']),
      'status' => 1,
      'authname_fb_user' => $authname,
    );

    // Allow $config to set other values, including mail
    $user_default = array_merge($user_default, $config);
    $user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
    if (count($config['roles'])) {
      foreach ($config['roles'] as $rid => $value) {
        if ($rid) {
          $user_default['roles'][$rid] = $value;
        }
      }
    }
    $user_default['fbu'] = $fbu;

    // Will get saved as user data.
    $account = user_save('', $user_default);
    watchdog('fb_user', t('New user: %name %email.', array(
      '%name' => theme('username', $account),
      '%email' => '<' . $account->mail . '>',
    )), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

    // Allow third-party modules to act after account creation.

    //$config = fb_invoke($fb_app, FB_OP_POST_USER, NULL, array('account' => $account));
    fb_invoke(FB_OP_POST_USER, array(
      'account' => $account,
      'fb_app' => $fb_app,
      'fb' => $fb,
    ));

    // TODO: move this to fb_action.  Temporarily disabled.
    if (FALSE) {

      // Prepare to send an email.
      $base = url('<front>', NULL, NULL, TRUE);
      $variables = array(
        '!username' => $account->name,
        '!site' => variable_get('site_name', 'Drupal'),
        '!password' => $user_default['pass'],
        '!uri' => $base,
        '!uri_brief' => substr($base, strlen('http://')),
        '!mailto' => $mail,
        '!date' => format_date(time()),
        '!login_uri' => url('user', NULL, NULL, TRUE),
        '!edit_uri' => url('user/' . $account->uid . '/edit', NULL, NULL, TRUE),
        '!login_url' => user_pass_reset_url($account),
      );
      $subject = _user_mail_text('welcome_subject', $variables);
      $body = _user_mail_text('welcome_body', $variables);

      // Fix links
      $subject = fb_scrub_urls($subject);
      $body = fb_scrub_urls($body);

      // TODO: make it configurable whether an email is sent, and what it contains.
      $fb->api_client
        ->notifications_sendEmail($fbu, $subject, $body, $body);
    }
  }
  if (!$account->fbu) {

    // This should only happen on older, automatically created accounts.
    $account->fbu = $fbu;
    user_save($account, array(
      'fbu' => $fbu,
    ));
  }
  return $account;
}