diff --git a/connector.api.php b/connector.api.php index 3770993..fb59b41 100644 --- a/connector.api.php +++ b/connector.api.php @@ -63,4 +63,18 @@ function hook_connector_action() { 'no external uid' => NULL, ), ); -} \ No newline at end of file +} + +/** + * Modify the incoming values on a user account. +**/ +function hook_connector_user_info_alter(&$info, $wrapper) { + $info['name']['value'] = strtolower($info['name']['value']); +} + +/** + * Field value handler to store the field on the user entity wrapper or in the edit array. +**/ +function hook_connector_field_value_handler_FIELDNAME(&$field, &$wrapper, &$edit) { + $edit[$field['sync']] = $field['value']; +} diff --git a/connector.info b/connector.info index dea3b3d..0a2b1a9 100644 --- a/connector.info +++ b/connector.info @@ -2,3 +2,5 @@ name = Connector description = Provides base functionality for one-click login with eg. Facebook Connect and Twitter Connect core = 7.x package = "Connector" + +dependencies[] = entity \ No newline at end of file diff --git a/connector.module b/connector.module index e58dcba..51a7b82 100644 --- a/connector.module +++ b/connector.module @@ -210,6 +210,51 @@ function connector_user_cancel($edit, $account, $method) { } /** + * Implements hook_connector_user_info_alter(). + */ +function connect_connector_user_info_alter(&$info, $wrapper) { + //Save external avatar image. + if (isset($info['avatar']) && $info['avatar']['value']) { + if (!$wrapper->value()->uid) { + //This is a new account, so don't grab the avatar yet. We'll do that upon login. + $info['avatar']['value'] = NULL; + return; + } + $filename = basename($field['value']); + $picture_result = file_get_contents($field['value']); + + if (!$picture_result) { + $info['avatar']['value'] = NULL; + return; + } + + $picture_file = file_save_data($picture_result, 'temporary://' . $filename, FILE_EXISTS_RENAME); + $picture_file->uid = $wrapper->getIdentifier(); + $picture_file->status = 0; + $picture_file = file_save($picture_file); + + if (!$picture_file) { + $info['avatar']['value'] = NULL; + return; + } + + $info['avatar']['value'] = $picture_file; + } +} + +/** + * Implements hook_connector_field_value_handler_FIELDNAME(). + */ +function connector_connector_field_value_handler_avatar(&$field, &$wrapper, &$edit) { + if($field['sync'] == 'picture') { + $edit['picture'] = $picture_file; + } + else { + $wrapper->{$field['sync']} = $picture_file; + } +} + +/** * Implements hook_user_logout(). */ function connector_user_logout($account) { @@ -425,8 +470,18 @@ function _connector_log_in($connector_name, $cid = NULL, $consumer = NULL, $acce } } else { + // Save data. + if (!empty($connector['information callback']) && is_callable($connector['information callback'])) { + $edit = array(); + $info = $connector['information callback']($connector, $cid, array(), $access_token); + $account_user = user_load($account->uid); + $wrapper = entity_metadata_wrapper('user', $account_user); + drupal_alter('connector_user_info', $info, $wrapper); + _connector_store_account_values($info, $wrapper, $edit); + user_save($wrapper->value(), $edit); + } //Log in user - $form_state['uid'] = $account->uid; + $form_state['uid'] = $wrapper->getIdentifier(); user_login_submit(array(), $form_state); return TRUE; } @@ -452,7 +507,7 @@ function _connector_create_account($connector_name, $cid = NULL, $consumer = NUL $authname = $connector_name . '__' . $cid; if (variable_get('user_register', 1)) { - $userinfo = array( + $account_info = array( 'name' => $authname, 'pass' => user_password(), 'init' => $authname, @@ -465,21 +520,34 @@ function _connector_create_account($connector_name, $cid = NULL, $consumer = NUL if (!empty($connector['information callback']) && is_callable($connector['information callback'])) { $info = $connector['information callback']($connector, $cid, array(), $access_token); } - $allowed_fields = array('name', 'mail'); - foreach ($info as $field) { - if (isset($field['sync']) && $field['sync'] && in_array($field['sync'], $allowed_fields)) { - $exists = db_select('users', 'u') - ->fields('u') - ->condition($field['sync'], $field['value']) - ->execute() - ->rowCount(); - if ($exists < 1) { - $userinfo[$field['sync']] = $field['value']; + + $edit = array(); + $account = entity_create('user', $account_info); + $wrapper = entity_metadata_wrapper('user', $account); + drupal_alter('connector_user_info', $info, $wrapper); + + $unique_fields = array('name', 'mail'); + foreach ($info as $field_name => $field) { + if (isset($field['sync']) && $field['sync']) { + if (in_array($field['sync'], $unique_fields)) { + $exists = db_select('users', 'u') + ->fields('u') + ->condition($field['sync'], $field['value']) + ->execute() + ->rowCount(); + if ($exists >= 1) { + // @TODO - Better error here about the unique field issue. + drupal_set_message(t("Error creating user account! A user already exists with that username or email."), 'error'); + return FALSE; + } } } } - $new_account = user_save('', $userinfo); + _connector_store_account_values($info, $wrapper, $edit); + + $new_account = user_save($wrapper->value(), $edit); + // Terminate if an error occured during user_save(). if (!$new_account) { drupal_set_message(t("Error saving user account."), 'error'); @@ -496,6 +564,30 @@ function _connector_create_account($connector_name, $cid = NULL, $consumer = NUL } } +function _connector_store_account_values($info, &$wrapper, &$edit) { + foreach ($info as $field_name => $field) { + //No destination field. + if (!isset($field['sync']) || !$field['sync']) { + continue; + } + + //The value is empty. + if (!$field['value']) { + continue; + } + + foreach (module_implements('connector_field_value_handler_' . $field_name) as $module) { + $function = $module . '_connector_field_value_handler_' . $field_name; + $function($field, $wrapper, $edit); + $field['stored'] = isset($field['stored']) ? $field['stored'] : TRUE; + } + + //This field hasn't been stored, so we store it in the simplest way possible. + if (!isset($field['stored']) || !$field['stored']) { + $wrapper->{$field['sync']}->set($field['value']); + } + } +} function _connector_add_connection($connector_name, $cid = NULL, $uid = NULL) { global $user;