You are here

function _fb_user_process_authorized_user in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.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
Implements hook_fb.
_fb_user_check_and_goto in ./fb_user.module
Detect whether facebook indicates the user has changed. If so, redirect.

File

./fb_user.module, line 477
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...
        $query = db_insert('users_roles')
          ->fields(array(
          'uid' => $account->uid,
          'rid' => $rid,
        ))
          ->execute();
        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);
    }
  }
}