This repository was archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdosomething_northstar.module
More file actions
287 lines (244 loc) · 8.42 KB
/
dosomething_northstar.module
File metadata and controls
287 lines (244 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* @file
* Code for the dosomething_northstar module.
*/
include_once('dosomething_northstar.admin.inc');
// Config vars, if not set.
define('NORTHSTAR_URL', variable_get('dosomething_northstar_url', 'http://northstar.dev'));
define('NORTHSTAR_PORT', variable_get('dosomething_northstar_port', '8000'));
define('NORTHSTAR_VERSION', variable_get('dosomething_northstar_version', 'v1'));
define('NORTHSTAR_APP_ID', variable_get('dosomething_northstar_app_id', '456'));
define('NORTHSTAR_APP_KEY', variable_get('dosomething_northstar_app_key', 'abc4324'));
/**
* Implements hook_menu().
*/
function dosomething_northstar_menu() {
$items = array();
$items['admin/config/services/northstar'] = array(
'title' => 'Northstar',
'description' => 'Manage Northstar connection settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('dosomething_northstar_config_form'),
'access arguments' => array('administer modules'),
'file' => 'dosomething_northstar.admin.inc',
);
return $items;
}
/**
* Implements hook_libraries_info().
*/
function dosomething_northstar_libraries_info() {
$libraries['guzzle'] = array(
'name' => 'guzzle',
'path' => 'vendor',
'files' => ['php' => ['autoload.php']],
'version' => 5.0,
);
return $libraries;
}
/**
* Verify the given credentials against Northstar, returning the authorized
* user account if one exists.
*
* @param array $credentials
* @return mixed|null
*/
function dosomething_northstar_verify_user($credentials) {
global $user;
$client = _dosomething_northstar_build_http_client();
$response = drupal_http_request($client['base_url'] . '/auth/verify', [
'headers' => $client['headers'],
'method' => 'POST',
'data' => json_encode($credentials),
]);
// Add to request log if enabled.
dosomething_northstar_log_request('verify_user', $user, $credentials, $response);
if($response->code !== '200') {
return null;
}
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) {
$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);
$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),
));
// 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 Northstar calls.
* @returns array
*/
function _dosomething_northstar_build_http_client() {
$base_url = NORTHSTAR_URL;
if (getenv('DS_ENVIRONMENT') === 'local') {
$base_url .= ":" . NORTHSTAR_PORT;
}
$base_url .= '/' . NORTHSTAR_VERSION;
$client = array(
'base_url' => $base_url,
'headers' => array(
'X-DS-REST-API-Key' => NORTHSTAR_APP_KEY,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
);
return $client;
}
/**
* 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'];
$id = $user->uid;
$ns_user = dosomething_northstar_build_ns_user($user, $form_state);
$client = _dosomething_northstar_build_http_client();
$response = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $id, array(
'headers' => $client['headers'],
'method' => 'PUT',
'data' => json_encode($ns_user),
));
if ($response->code === '200' && module_exists('stathat')) {
stathat_send_ez_count('drupal - Northstar - user updated - count', 1);
}
elseif ($response->code !== '200') {
watchdog('dosomething_northstar', 'User not updated : ' . $response->code, NULL, WATCHDOG_ERROR);
}
// Add to request log if enabled.
dosomething_northstar_log_request('update_user', $user, $ns_user, $response);
}
/**
* Get user profile data from Northstar for the given Drupal ID.
*
* @param int $drupal_id Drupal user id.
* @return object
*/
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/' . $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'", $drupal_id, $northstar_user_info->error);
watchdog_exception('northstar', new Exception($error));
} else {
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 in the format that Northstar expects.
*
* @param $user - Drupal user object
* @return array
*/
function dosomething_northstar_build_ns_user($user, $form_state) {
// Optional fields
$optional = [
'mobile' => 'field_mobile',
'birthdate' => 'field_birthdate',
'first_name' => 'field_first_name',
];
// Address fields
$address = [
'country' => 'country',
'addr_street1' => 'thoroughfare',
'addr_street2' => 'premise',
'addr_city' => 'locality',
'addr_state' => 'administrative_area',
'addr_zip' => 'postal_code',
];
$ns_user = [
'email' => $user->mail,
'drupal_id' => $user->uid,
'language' => $user->language,
];
// Set values in ns_user if they are set.
foreach ($optional as $ns_key => $drupal_key) {
$field = $user->$drupal_key;
if (isset($field[LANGUAGE_NONE][0]['value'])) {
$ns_user[$ns_key] = $field[LANGUAGE_NONE][0]['value'];
}
}
foreach ($address as $ns_key => $drupal_key) {
$field = $user->field_address[LANGUAGE_NONE][0];
if ($drupal_key == 'country') {
$ns_user[$ns_key] = $field[$drupal_key];
} else if (isset($field[$drupal_key]['value'])) {
$ns_user[$ns_key] = $field[$drupal_key]['value'];
}
}
// Don't send blank passwords from the user update screen.
if (!empty($form_state['values']['pass'])) {
$ns_user['password'] = $form_state['values']['pass'];
}
// Return fully built northstar user object.
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;
// 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();
}