Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,51 @@ function dosomething_northstar_verify_user($credentials) {
return json_decode($response->data);
}

/**
* Save the user's Northstar ID to their local profile.
*
* @param $user - Drupal user
* @param $northstar_response - Northstar user JSON response
*/
function dosomething_northstar_save_id_field($user, $northstar_response) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have some getters and setters in the user module, maybe this should live there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use that method here in b27921d!

$northstar_id = !empty($northstar_response->data->id) ? $northstar_response->data->id : 'NONE';
user_save($user, ['field_northstar_id' => [LANGUAGE_NONE => [0 => ['value' => $northstar_id]]]]);
}

/**
* Send user registration events to northstar.
*/
function dosomething_northstar_register_user($form_state) {
global $user;
// Build a user that Northstar expects.
$ns_user = dosomething_northstar_build_ns_user($user, $form_state);
// Send that to NS.
// $northstar = new Northstar();
// try {
// $response = $northstar->updateUser($ns_user);
// if ($response->getStatusCode() === '200' && module_exists('stathat')) {
// stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
// }
// }
// catch (Exception $e) {

// watchdog('dosomething_northstar', 'User not migrated : ' . $e, NULL, WATCHDOG_ERROR);
// }

$client = _dosomething_northstar_build_http_client();
$response = drupal_http_request($client['base_url'] . '/users', array(
'headers' => $client['headers'],
'method' => 'POST',
'data' => json_encode($ns_user),
));
if ($response->code === '200' && module_exists('stathat')) {
stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
}
elseif ($response->code !== '200') {
watchdog('dosomething_northstar', 'User not migrated : ' . $response->code, NULL, WATCHDOG_ERROR);
}
));

// Save the newly registered user's ID to their local profile field.
dosomething_northstar_save_id_field($user, json_decode($response->data));

// Add to request log if enabled.
dosomething_northstar_log_request('register_user', $user, $ns_user, $response);

if ($response->code !== '200') {
watchdog('dosomething_northstar', 'User not migrated : ' . $response->code, NULL, WATCHDOG_ERROR);
return;
}

if (module_exists('stathat')) {
stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
}
}

/**
* Build the drupal_http_request object for nothstar calls.
* This will be depricated once guzzle is fixed...
* Build the drupal_http_request object for Northstar calls.
* @returns array
*/
function _dosomething_northstar_build_http_client() {
$base_url = NORTHSTAR_URL;
Expand All @@ -131,7 +137,10 @@ function _dosomething_northstar_build_http_client() {
}

/**
* Send user profile updates to northstar.
* Update a user's existing Northstar profile based on
* their Drupal ID.
*
* @return void
*/
function dosomething_northstar_update_user($form_state) {
$user = $form_state['user'];
Expand All @@ -158,74 +167,43 @@ function dosomething_northstar_update_user($form_state) {
}

/**
* If the log is enabled, log this request to the database.
* Get user profile data from Northstar for the given Drupal ID.
*
* @param string $op - label for the operation being performed
* @param object $user - the Drupal user
* @param array $request_body - the body of the request
* @param string $response - response JSON
*/
function dosomething_northstar_log_request($op, $user, $request_body, $response) {
if (!variable_get('dosomething_northstar_log')) return;

// Don't log plaintext passwords.
if (isset($request_body['password'])) {
$request_body['password'] = '*****';
}

db_insert('dosomething_northstar_request_log')
->fields([
'op' => $op,
'uid' => $user->uid,
'user_lang' => $user->language,
'user_country' => $user->field_address[LANGUAGE_NONE][0]['country'],
'fastly_country' => dosomething_settings_get_geo_country_code(),
'request_values' => json_encode($request_body),
'response_code' => $response->code,
'response_values' => $response->data,
])
->execute();
}

/**
* Get user data from northstar.
*
* @param int $id Drupal user id.
* @param int $drupal_id Drupal user id.
* @return object
*/
function dosomething_northstar_get_northstar_user($id) {
$northstar_user_info = cache_get('northstar_user_info_' . $id, 'cache_dosomething_northstar');
function dosomething_northstar_get_northstar_user($drupal_id) {
$northstar_user_info = cache_get('northstar_user_info_' . $drupal_id, 'cache_dosomething_northstar');

if ($northstar_user_info) {
$northstar_user_info = $northstar_user_info->data;
}
else {
$client = _dosomething_northstar_build_http_client();

$northstar_user_info = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $id, array(
$northstar_user_info = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $drupal_id, array(
'headers' => $client['headers'],
'method' => 'GET',
));

$northstar_user_info = $northstar_user_info->data;

if (!empty($northstar_user_info->error)) {
$error = sprintf("Error fetching Northstar user data, uid=%d: '%s'", $id, $northstar_user_info->error);
$error = sprintf("Error fetching Northstar user data, uid=%d: '%s'", $drupal_id, $northstar_user_info->error);
watchdog_exception('northstar', new Exception($error));
} else {
cache_set('northstar_user_info_' . $id, $northstar_user_info, 'cache_dosomething_northstar', REQUEST_TIME + 60*60*24);
cache_set('northstar_user_info_' . $drupal_id, $northstar_user_info, 'cache_dosomething_northstar', REQUEST_TIME + 60*60*24);
}
}

return $northstar_user_info;
}

/**
* Build a user json object, that's accepted by Northstar.
*
* @param obj $user
* Drupal user object
* Build a user JSON object in the format that Northstar expects.
*
* @param $user - Drupal user object
* @return array
*/
function dosomething_northstar_build_ns_user($user, $form_state) {
// Optional fields
Expand Down Expand Up @@ -277,44 +255,33 @@ function dosomething_northstar_build_ns_user($user, $form_state) {
return $ns_user;
}

/**
* If the log is enabled, log this request to the database.
*
* @param string $op - label for the operation being performed
* @param object $user - the Drupal user
* @param array $request_body - the body of the request
* @param string $response - response JSON
*/
function dosomething_northstar_log_request($op, $user, $request_body, $response) {
if (!variable_get('dosomething_northstar_log')) return;

class Northstar {

// protected $client;

// public function __construct() {

// $base_url = NORTHSTAR_URL;
// if (getenv('DS_ENVIRONMENT') === 'local') {
// $base_url .= ":" . NORTHSTAR_PORT;
// }
// $version = NORTHSTAR_VERSION;

// if (libraries_load('guzzle') == TRUE) {
// $client = new GuzzleHttp\Client(array(
// 'base_url' => array($base_url . '/{version}/', array('version' => $version)),
// 'defaults' => array(
// 'headers' => array(
// 'X-DS-Application-Id' => NORTHSTAR_APP_ID,
// 'X-DS-REST-API-Key' => NORTHSTAR_APP_KEY,
// 'Content-Type' => 'application/json',
// 'Accept' => 'application/json'
// ),
// ),
// ));
// $this->client = $client;

// }
// }

// public function updateUser($user) {
// // return $this->client->getUrl();
// $response = $this->client->post('users', array(
// 'body' => json_encode($user),
// ));

// // watchdog('dosomething_northstar', 'full url' . $response->getUrl(), NULL, WATCHDOG_ERROR);
// return $response;
// }
// Don't log plaintext passwords.
if (isset($request_body['password'])) {
$request_body['password'] = '*****';
}

db_insert('dosomething_northstar_request_log')
->fields([
'op' => $op,
'uid' => $user->uid,
'user_lang' => $user->language,
'user_country' => $user->field_address[LANGUAGE_NONE][0]['country'],
'fastly_country' => dosomething_settings_get_geo_country_code(),
'request_values' => json_encode($request_body),
'response_code' => $response->code,
'response_values' => $response->data,
])
->execute();
}

Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ private function build($data) {
$northstar_response = json_decode($northstar_response);

if (!empty($northstar_response->data) && !isset($northstar_response->error)) {
$northstar_user = $northstar_response->data;
user_save($drupal_user, ['field_northstar_id' => [LANGUAGE_NONE => [0 => ['value' => $northstar_user->id]]]]);
dosomething_northstar_save_id_field($drupal_user, $northstar_response);
}
}

Expand Down
Loading