You are here

function _fb_user_process_authorized_user in Drupal for Facebook 6.3

Same name and namespace in other branches
  1. 7.3 fb_user.module \_fb_user_process_authorized_user()

Create local account or account map for a facebook user who has authorized the application.

2 calls to _fb_user_process_authorized_user()
fb_user_fb in ./fb_user.module
Implementation of hook_fb.
fb_user_footer in ./fb_user.module

File

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

Code

function _fb_user_process_authorized_user() {
  $fbu = fb_facebook_user();
  $mapped = FALSE;
  if ($fbu && (!variable_get(FB_USER_VAR_CHECK_SESSION, FALSE) || _fb_user_check_session($fbu))) {
    $fb_app = $GLOBALS['_fb_app'];

    // First check if map already exists.
    $account = fb_user_get_local_user($fbu, $fb_app);
    $config = _fb_user_get_config($fb_app);
    if (!$account) {
      if ($GLOBALS['user']->uid > 0 && $config['map_account'][FB_USER_OPTION_MAP_ALWAYS]) {

        // Create map for logged in user.
        $mapped = _fb_user_create_map();
      }
      if (!$mapped && $config['map_account'][FB_USER_OPTION_MAP_EMAIL]) {

        // Create map if email matches.
        $mapped = _fb_user_create_map_by_email();
      }
      if (!$mapped && $config['create_account'] == FB_USER_OPTION_CREATE_LOGIN) {

        // Create new local account with map.
        $mapped = _fb_user_create_local_account();
      }
      if ($mapped) {
        $account = fb_user_get_local_user($fbu, $fb_app);
      }
    }
    if ($account) {

      // Ensure the user has any roles associated with this app.
      $rid = $config['new_user_rid'];
      if ($account && $rid && (!isset($account->roles[$rid]) || !$account->roles[$rid])) {

        // there should be an API for this...
        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
        watchdog('fb_user', "Added role %role to existing user !username for application %app", array(
          '!username' => theme('username', $account),
          '%app' => $fb_app->label,
          '%role' => $rid,
        ));
      }

      // Login as facebook user, if not already.
      _fb_user_external_login($account);
    }
  }
}