You are here

function _fb_user_process_new_token in Drupal for Facebook 7.4

2 calls to _fb_user_process_new_token()
fb_user_fb in ./fb_user.module
Implements hook_fb().
fb_user_user_login in ./fb_user.module
Implements hook_user_login().

File

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

Code

function _fb_user_process_new_token($token) {
  if (user_access(FB_USER_PERM_NEVER_MAP)) {

    // The current user cannot be mapped to facebook accounts.
    if (user_access('access administration pages')) {
      drupal_set_message(t('fb_user.module ignoring facebook connect for administrator account.'));
    }
    return;
  }
  $map_policy = variable_get(FB_USER_VAR_MAP_POLICY, array(
    FB_USER_OPTION_MAP_LOGGED_IN,
    FB_USER_OPTION_MAP_EMAIL,
  ));
  $create_policy = variable_get(FB_USER_VAR_CREATE_POLICY, FB_USER_OPTION_CREATE_NEVER);
  try {
    $me = fb_graph('me', $token);
    $fbu = $me['id'];
    $fb_user_data = db_query("SELECT * FROM {fb_user} WHERE fbu=:fbu", array(
      ':fbu' => $fbu,
    ))
      ->fetchAssoc();
    if ($uid = $fb_user_data['uid']) {
      if ($uid != $GLOBALS['user']->uid) {

        // The user has connected to facebook and we should log that user into their local account.
        if ($account = user_load($uid)) {
          _fb_user_set_current_user($account);
          return;
        }
      }
    }
    else {

      // No user data in fb_user table.
      if (in_array(FB_USER_OPTION_MAP_EMAIL, $map_policy)) {
        if (!empty($me['email'])) {
          if ($account = user_load_by_mail($me['email'])) {
            _fb_user_set_map($account, $fbu);
            _fb_user_set_current_user($account);
            return;
          }
        }
      }
      if ($uid = $GLOBALS['user']->uid) {

        // User is logged in and connected to facebook.
        if (in_array(FB_USER_OPTION_MAP_LOGGED_IN, $map_policy)) {
          _fb_user_set_map($GLOBALS['user'], $fbu);
          return;
        }
      }
      else {

        // Anonymous user is connected to facebook.
        if ($create_policy == FB_USER_OPTION_CREATE_LOGIN) {

          // Create a new user account for this facebook user.
          if ($account = _fb_user_create_local_account($me)) {

            // Log in as the new user.
            _fb_user_set_current_user($account);
          }
          return;
        }
      }
    }
  } catch (Exception $e) {
    fb_log_exception($e, t('fb_user.module failed to process a new token.'));
  }
}