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

Commit 22974c4

Browse files
committed
Merge pull request #6262 from DFurnes/log-all-the-stars
Log all the stars
2 parents 049f58e + 30a2e2b commit 22974c4

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

lib/modules/dosomething/dosomething_northstar/dosomething_northstar.admin.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@ function dosomething_northstar_config_form($form, &$form_state) {
4949
'#default_value' => variable_get('dosomething_northstar_app_key', 'abc4324'),
5050
);
5151

52+
$form['log'] = [
53+
'#type' => 'fieldset',
54+
'#title' => t('Logging'),
55+
];
56+
$form['log']['dosomething_northstar_log'] = [
57+
'#type' => 'checkbox',
58+
'#title' => t('Log requests and responses.'),
59+
'#default_value' => variable_get('dosomething_northstar_log', FALSE),
60+
'#description' => t("Logs Northstar activity. This should be disabled on production."),
61+
];
62+
5263
return system_settings_form($form);
5364
}

lib/modules/dosomething/dosomething_northstar/dosomething_northstar.install

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,59 @@
1010
*/
1111
function dosomething_northstar_schema() {
1212
$schema['cache_dosomething_northstar'] = drupal_get_schema_unprocessed('system', 'cache');
13+
14+
$schema['dosomething_northstar_request_log'] = [
15+
'description' => 'Log of requests made to Northstar for debugging.',
16+
'fields' => [
17+
'uid' => [
18+
'description' => 'The user\'s Drupal UID',
19+
'type' => 'int',
20+
'not null' => TRUE,
21+
'default' => 0,
22+
],
23+
'op' => [
24+
'description' => 'The operation being performed',
25+
'type' => 'varchar',
26+
'length' => 255,
27+
'default' => '',
28+
],
29+
'user_lang' => [
30+
'description' => 'The user\'s language',
31+
'type' => 'varchar',
32+
'length' => 255,
33+
'default' => '',
34+
],
35+
'user_country' => [
36+
'description' => 'The user\'s country code',
37+
'type' => 'varchar',
38+
'length' => 255,
39+
'default' => '',
40+
],
41+
'fastly_country' => [
42+
'description' => 'The Fastly country code',
43+
'type' => 'varchar',
44+
'length' => 255,
45+
'default' => '',
46+
],
47+
'request_values' => [
48+
'description' => 'The JSON request made to Northstar',
49+
'type' => 'varchar',
50+
'length' => 5000,
51+
'default' => '',
52+
],
53+
'response_code' => [
54+
'description' => 'The HTTP response code from Northstar',
55+
'type' => 'int',
56+
'default' => 0,
57+
],
58+
'response_values' => [
59+
'description' => 'The JSON response from Northstar',
60+
'type' => 'varchar',
61+
'length' => 5000,
62+
],
63+
],
64+
];
65+
1366
return $schema;
1467
}
1568

@@ -24,3 +77,13 @@ function dosomething_northstar_update_7001() {
2477
db_create_table('cache_dosomething_northstar', $schema['cache_dosomething_northstar']);
2578
}
2679
}
80+
81+
/**
82+
* Create a new table "dosomething_northstar_request_log" for logging details of any
83+
* Northstar request errors that occur.
84+
*/
85+
function dosomething_northstar_update_7002() {
86+
$table_name = 'dosomething_northstar_request_log';
87+
$schema = dosomething_northstar_schema();
88+
db_create_table($table_name, $schema[$table_name]);
89+
}

lib/modules/dosomething/dosomething_northstar/dosomething_northstar.module

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function dosomething_northstar_libraries_info() {
5252
* @return mixed|null
5353
*/
5454
function dosomething_northstar_verify_user($credentials) {
55+
global $user;
5556
$client = _dosomething_northstar_build_http_client();
5657

5758
$response = drupal_http_request($client['base_url'] . '/auth/verify', [
@@ -60,6 +61,9 @@ function dosomething_northstar_verify_user($credentials) {
6061
'data' => json_encode($credentials),
6162
]);
6263

64+
// Add to request log if enabled.
65+
dosomething_northstar_log_request('verify_user', $user, $credentials, $response);
66+
6367
if($response->code !== '200') {
6468
return null;
6569
}
@@ -99,6 +103,8 @@ function dosomething_northstar_register_user($form_state) {
99103
watchdog('dosomething_northstar', 'User not migrated : ' . $response->code, NULL, WATCHDOG_ERROR);
100104
}
101105

106+
// Add to request log if enabled.
107+
dosomething_northstar_log_request('register_user', $user, $ns_user, $response);
102108
}
103109

104110
/**
@@ -132,7 +138,6 @@ function dosomething_northstar_update_user($form_state) {
132138
$id = $user->uid;
133139
$ns_user = dosomething_northstar_build_ns_user($user, $form_state);
134140

135-
$northstar = new Northstar();
136141
$client = _dosomething_northstar_build_http_client();
137142

138143
$response = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $id, array(
@@ -148,6 +153,38 @@ function dosomething_northstar_update_user($form_state) {
148153
watchdog('dosomething_northstar', 'User not updated : ' . $response->code, NULL, WATCHDOG_ERROR);
149154
}
150155

156+
// Add to request log if enabled.
157+
dosomething_northstar_log_request('update_user', $user, $ns_user, $response);
158+
}
159+
160+
/**
161+
* If the log is enabled, log this request to the database.
162+
*
163+
* @param string $op - label for the operation being performed
164+
* @param object $user - the Drupal user
165+
* @param array $request_body - the body of the request
166+
* @param string $response - response JSON
167+
*/
168+
function dosomething_northstar_log_request($op, $user, $request_body, $response) {
169+
if (!variable_get('dosomething_northstar_log')) return;
170+
171+
// Don't log plaintext passwords.
172+
if (isset($request_body['password'])) {
173+
$request_body['password'] = '*****';
174+
}
175+
176+
db_insert('dosomething_northstar_request_log')
177+
->fields([
178+
'op' => $op,
179+
'uid' => $user->uid,
180+
'user_lang' => $user->language,
181+
'user_country' => $user->field_address[LANGUAGE_NONE][0]['country'],
182+
'fastly_country' => dosomething_settings_get_geo_country_code(),
183+
'request_values' => json_encode($request_body),
184+
'response_code' => $response->code,
185+
'response_values' => $response->data,
186+
])
187+
->execute();
151188
}
152189

153190
/**

0 commit comments

Comments
 (0)