From fd9f6486103a2efebe6f9ca8138c7ce2a635e0c2 Mon Sep 17 00:00:00 2001
From: Ben McIlwain <cydeweys@gmail.com>
Date: Mon, 2 Apr 2012 17:41:37 -0400
Subject: [PATCH] Adds configuration option to not auto-create new user, and
 pre-fills reg form.

---
 fboauth.install              |    1 +
 fboauth.module               |   55 ++++++++++++++++++++++++++++++++++++++++++
 includes/fboauth.fboauth.inc |    6 ++++
 includes/fboauth.pages.inc   |    7 +++++
 includes/fboauth.profile.inc |   25 +++++++++++++++++++
 5 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/fboauth.install b/fboauth.install
index 3829f0f..fa640ee 100644
--- a/fboauth.install
+++ b/fboauth.install
@@ -23,6 +23,7 @@ function fboauth_uninstall() {
   drupal_uninstall_schema('fboauth');
   variable_del('fboauth_id');
   variable_del('fboauth_secret');
+  variable_del('fboauth_auto_create_account');
   variable_del('fboauth_user_email');
   variable_del('fboauth_user_username');
   variable_del('fboauth_user_profile');
diff --git a/fboauth.module b/fboauth.module
index da65d3d..71201a8 100644
--- a/fboauth.module
+++ b/fboauth.module
@@ -111,6 +111,10 @@ function fboauth_user($op, &$edit, &$account, $category = NULL) {
 function fboauth_user_insert(&$edit, &$account, $category) {
   if (isset($edit['fboauth_fbid'])) {
     fboauth_save($account->uid, $edit['fboauth_fbid']);
+    // Unset the session var if we used it to create the new user.
+    if (isset($_SESSION['fboauth_fbuser'])) {
+      unset($_SESSION['fboauth_fbuser']);
+    }
   }
 }
 
@@ -429,3 +433,54 @@ function theme_fboauth_user_form_connect($uid, $fbid) {
   }
   return $output;
 }
+
+/**
+ * Modifies the user register form to populate the fields with data retrieved from
+ * Facebook after the user has authenticated with Facebook during attempted registration.
+ * Implements hook_form_alter().
+ */
+function fboauth_form_user_register_alter(&$form, &$form_state) {
+  if (variable_get('fboauth_auto_create_account', 1)) {
+    // Don't change the register form if users are being automatically created.
+    return $form;
+  }
+
+  global $user;
+  if ($user->uid > 0) {
+    // Don't modify the form for admins attempting to add users.
+    return $form;
+  }
+
+  if (!isset($_SESSION['fboauth_fbuser'])) {
+    // Don't modify the form if the fbuser isn't found.
+    return $form;
+  }
+
+  $fbuser = $_SESSION['fboauth_fbuser'];
+
+  // Go through all of the fields in the form and populate them with data on fbuser.
+  $profile_field_values = array();
+  $profile_field_values['mail'] = $fbuser->email;
+  if (variable_get('fboauth_user_username', 'username') == 'username' && !empty($fbuser->username)) {
+    $profile_field_values['name'] = $fbuser->username;
+  }
+  else {
+    $profile_field_values['name'] = $fbuser->name;
+  }
+
+  // Load additional data for fields from the Profile module if it's enabled.
+  if (module_exists('profile')) {
+    module_load_include('inc', 'fboauth', 'includes/fboauth.profile');
+    fboauth_profile_create_user($profile_field_values, $fbuser);
+    $form = _fboauth_set_register_form_fields($form, $profile_field_values);
+  }
+
+  // Pass in the FBID as a hidden value so the hook_user implementation that associates
+  // the FB user and the Drupal user can access it.
+  $form['fboauth_fbid'] = array(
+    '#type' => 'value',
+    '#value' => $fbuser->id,
+  );
+  
+  return $form;
+}
diff --git a/includes/fboauth.fboauth.inc b/includes/fboauth.fboauth.inc
index f10fb18..ae12bc4 100644
--- a/includes/fboauth.fboauth.inc
+++ b/includes/fboauth.fboauth.inc
@@ -96,6 +96,12 @@ function fboauth_action_connect($app_id, $access_token) {
     }
     // Register a new user only if allowed.
     elseif (variable_get('user_register', 1)) {
+      // If accounts aren't automatically created, then save the fbuser to the session and go to the
+      // user registration page.
+      if (!variable_get('fboauth_auto_create_account', 1)) {
+        $_SESSION['fboauth_fbuser'] = $fbuser;
+        drupal_goto('user/register');
+      }
       $account = fboauth_create_user($fbuser);
       // Load the account fresh just to have a fully-loaded object.
       $account = user_load($account->uid);
diff --git a/includes/fboauth.pages.inc b/includes/fboauth.pages.inc
index e2525fa..19de067 100644
--- a/includes/fboauth.pages.inc
+++ b/includes/fboauth.pages.inc
@@ -27,6 +27,12 @@ function fboauth_settings_form(&$form_state) {
     '#description' => t('To use Facebook connect, a Facebook Application must be created. Set up your app in <a href="http://www.facebook.com/developers/apps.php">my apps</a> on Facebook.') . ' ' . t('Enter your App Secret here.'),
     '#default_value' => variable_get('fboauth_secret', ''),
   );
+  $form['fboauth_auto_create_account'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Automatically create account when users register through Facebook'),
+    '#description' => t('If checked, then the user account will automatically be created when the user registers using Facebook, using whatever profile fields are configured.  Otherwise, the normal user registration form is displayed with profile fields pre-filled with Facebook values, and the actual user account is not created until that form is submitted.'),
+    '#default_value' => variable_get('fboauth_auto_create_account', TRUE),
+  );
 
   $form['fboauth_basic_mapping'] = array(
     '#type' => 'fieldset',
@@ -127,6 +133,7 @@ function fboauth_settings_form_validate($form, &$form_state) {
 function fboauth_settings_form_submit($form, &$form_state) {
   variable_set('fboauth_id', $form_state['values']['fboauth_id']);
   variable_set('fboauth_secret', $form_state['values']['fboauth_secret']);
+  variable_set('fboauth_auto_create_account', $form_state['values']['fboauth_auto_create_account']);
 
   variable_set('fboauth_user_email', $form_state['values']['fboauth_user_email']);
   variable_set('fboauth_user_username', $form_state['values']['fboauth_user_username']);
diff --git a/includes/fboauth.profile.inc b/includes/fboauth.profile.inc
index 78d00bb..fdf3305 100644
--- a/includes/fboauth.profile.inc
+++ b/includes/fboauth.profile.inc
@@ -131,3 +131,28 @@ function fboauth_profile_create_user(&$edit, $fbuser) {
     }
   }
 }
+
+/**
+* Recursively traverses a Drupal form array (specified by $form), searching for any
+* field specified in $profile_field_values, and sets the default_value for every one
+* it finds.
+ * @param $form
+ *   The current form array that is being recursed through (either the entire form
+ *   or some part of it).
+ * @param $profile_field_values
+ *   An associative array that maps names of profile fields to values returned from
+ *   the API call to Facebook. @see $edit as used by hook_user.
+ * @return The $form that was passed in, with modified #default_values.
+ */
+function _fboauth_set_register_form_fields(&$form, &$profile_field_values) {
+  foreach ($form as $key => $value) {
+    if (in_array($key, array_keys($profile_field_values))) {
+      $form[$key]['#default_value'] = $profile_field_values[$key];
+    }
+    elseif (is_array($value)) {
+      $form[$key] = _fboauth_set_register_form_fields($value, $profile_field_values);
+    }
+  }
+
+  return $form;
+}
-- 
1.7.4.5

