Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit f574b1c

Browse files
committed
Merge pull request #6495 from DFurnes/at-email-sync
Fix syncing `@email` users.
2 parents bd9b5fc + 29be76c commit f574b1c

File tree

2 files changed

+41
-69
lines changed

2 files changed

+41
-69
lines changed

lib/modules/dosomething/dosomething_northstar/dosomething_northstar.module

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function dosomething_northstar_update_user($user, $payload) {
158158

159159
// Add to request log if enabled.
160160
dosomething_northstar_log_request('update_user', $user, $payload, $response);
161-
161+
162162
if ($response->code === '200' && module_exists('stathat')) {
163163
stathat_send_ez_count('drupal - Northstar - user updated - count', 1);
164164
}
@@ -205,12 +205,12 @@ function dosomething_northstar_get_northstar_user($drupal_id) {
205205
}
206206

207207
/**
208-
* Build a user JSON object in the format that Northstar expects.
208+
* Transform Drupal user fields into the appropriate schema for Northstar.
209209
*
210210
* @param $user - Drupal user object
211211
* @return array
212212
*/
213-
function dosomething_northstar_build_ns_user($user, $form_state) {
213+
function dosomething_northstar_transform_user($user) {
214214
// Optional fields
215215
$optional = [
216216
'mobile' => 'field_mobile',
@@ -229,42 +229,61 @@ function dosomething_northstar_build_ns_user($user, $form_state) {
229229
'addr_zip' => 'postal_code',
230230
];
231231

232-
$ns_user = [
232+
$northstar_user = [
233233
'email' => $user->mail,
234234
'drupal_id' => $user->uid,
235235
'language' => $user->language,
236236
];
237237

238238
// Set values in ns_user if they are set.
239239
foreach ($optional as $ns_key => $drupal_key) {
240-
$field = $user->$drupal_key;
240+
$field = $user->$drupal_key;
241241
if (isset($field[LANGUAGE_NONE][0]['value'])) {
242-
$ns_user[$ns_key] = $field[LANGUAGE_NONE][0]['value'];
242+
$northstar_user[$ns_key] = $field[LANGUAGE_NONE][0]['value'];
243243
}
244244
}
245245
foreach ($address as $ns_key => $drupal_key) {
246246
$field = $user->field_address[LANGUAGE_NONE][0];
247247

248248
if ($drupal_key == 'country') {
249-
$ns_user[$ns_key] = $field[$drupal_key];
249+
$northstar_user[$ns_key] = $field[$drupal_key];
250250
} else if (isset($field[$drupal_key]['value'])) {
251-
$ns_user[$ns_key] = $field[$drupal_key]['value'];
251+
$northstar_user[$ns_key] = $field[$drupal_key]['value'];
252252
}
253253
}
254254

255-
// Don't send blank passwords from the user update screen.
256-
if (!empty($form_state['values']['pass'])) {
257-
$ns_user['password'] = $form_state['values']['pass'];
255+
// If user has a "1234565555@mobile" placeholder email, don't send
256+
// that to Northstar (since it will cause a validation error and Northstar
257+
// doesn't require every account to have an email like Drupal does).
258+
if(preg_match('/^[0-9]+@mobile$/', $northstar_user['email'])) {
259+
unset($northstar_user['email']);
258260
}
259261

260262
// Set the "source" for this user to Phoenix if they weren't
261263
// programmatically created through the API.
262-
if(empty($ns_user['source'])) {
263-
$ns_user['source'] = 'phoenix';
264+
if(empty($northstar_user['source'])) {
265+
$northstar_user['source'] = 'phoenix';
266+
}
267+
268+
return $northstar_user;
269+
}
270+
271+
/**
272+
* Build a Northstar user from a Drupal form submission.
273+
*
274+
* @param $user - Drupal user object
275+
* @param $form_state - Form fields from registration/profile form.
276+
* @return array
277+
*/
278+
function dosomething_northstar_build_ns_user($user, $form_state) {
279+
$northstar_user = dosomething_northstar_transform_user($user);
280+
281+
// Don't send blank passwords from the user update screen.
282+
if (!empty($form_state['values']['pass'])) {
283+
$northstar_user['password'] = $form_state['values']['pass'];
264284
}
265285

266-
// Return fully built northstar user object.
267-
return $ns_user;
286+
return $northstar_user;
268287
}
269288

270289
/**

scripts/export-users-to-northstar.php

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -55,60 +55,13 @@
5555
* Build a Northstar request from the $user global variable.
5656
*/
5757
function build_northstar_user($user) {
58-
// Optional fields
59-
$optional = [
60-
'mobile' => 'field_mobile',
61-
'birthdate' => 'field_birthdate',
62-
'first_name' => 'field_first_name',
63-
'last_name' => 'field_last_name',
64-
'source' => 'field_user_registration_source',
65-
'school_id' => 'field_school_id',
66-
];
58+
$northstar_user = dosomething_northstar_transform_user($user);
6759

68-
// Address fields
69-
$address = [
70-
'country' => 'country',
71-
'addr_street1' => 'thoroughfare',
72-
'addr_street2' => 'premise',
73-
'addr_city' => 'locality',
74-
'addr_state' => 'administrative_area',
75-
'addr_zip' => 'postal_code',
76-
];
60+
// Since we're sending an existing user, we'll also attach their hashed password
61+
// (which Northstar can understand thanks to it's DrupalPasswordHash class), and
62+
// the created_at timestamp on the original account.
63+
$northstar_user['drupal_password'] = $user->pass;
64+
$northstar_user['created_at'] = $user->created;
7765

78-
$ns_user = [
79-
'email' => $user->mail,
80-
'drupal_id' => $user->uid,
81-
'drupal_password' => $user->pass,
82-
'created_at' => $user->created,
83-
];
84-
85-
// Set values in ns_user if they are set.
86-
foreach ($optional as $ns_key => $drupal_key) {
87-
$field = $user->$drupal_key;
88-
if (!empty($field[LANGUAGE_NONE][0]['value'])) {
89-
$ns_user[$ns_key] = $field[LANGUAGE_NONE][0]['value'];
90-
}
91-
}
92-
// Set address values.
93-
foreach ($address as $ns_key => $drupal_key) {
94-
$field = $user->field_address[LANGUAGE_NONE][0];
95-
if (!empty($field[$drupal_key]['value'])) {
96-
$ns_user[$ns_key] = $field[$drupal_key]['value'];
97-
}
98-
}
99-
100-
// If user has a "1234565555@mobile" placeholder username, don't send
101-
// that to Northstar (since it will cause a validation error and Northstar
102-
// doesn't require every account to have an email like Drupal does).
103-
if(preg_match('/^[0-9]+@mobile$/', $ns_user['email'])) {
104-
unset($ns_user['email']);
105-
}
106-
107-
// Set the "source" for this user to Phoenix if they weren't
108-
// programmatically created through the API.
109-
if(empty($ns_user['source'])) {
110-
$ns_user['source'] = 'phoenix';
111-
}
112-
113-
return $ns_user;
66+
return $northstar_user;
11467
}

0 commit comments

Comments
 (0)