This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbs_passport.admin.inc
404 lines (372 loc) · 12.3 KB
/
pbs_passport.admin.inc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* @file
* Admin form functions for settings form for OAuth and Membership Vault API.
*/
/**
* Email templates for module emails.
*
* @return array
* System settings form render array.
*/
function pbs_passport_settings_email_templates() {
$form = array();
$form['tokens'] = array(
'#type' => 'fieldset',
'#title' => t('Tokens'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if (module_exists('token')) {
$form['tokens']['tree'] = array(
'#theme' => 'token_tree',
);
}
else {
$form['tokens']['no_tokens'] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Enable the <a href="@drupal-token">Token module</a>
to view the token browser.', array(
'@drupal-token' => 'http://drupal.org/project/token',
)
),
);
}
$form['lookup'] = array(
'#type' => 'fieldset',
'#title' => t('Membership Lookup'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['lookup']['description'] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('These templates are used in emails generated by the
<a href="@url">Membership Lookup</a> form.', array(
'@url' => '/pbs-passport/lookup',
)
),
);
$form['lookup']['pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_ACTIVATED] = array(
'#type' => 'textarea',
'#title' => t('Activated'),
'#description' => t('Sent to a user with an activated membership.'),
'#default_value' => variable_get(
'pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_ACTIVATED
),
'#element_validate' => array('token_element_validate'),
);
$form['lookup']['pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_NOT_ACTIVATED] = array(
'#type' => 'textarea',
'#title' => t('Not activated'),
'#description' => t('Sent to a user with an available membership
that <strong>has not been</strong> activated.'),
'#default_value' => variable_get(
'pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_NOT_ACTIVATED
),
'#element_validate' => array('token_element_validate'),
);
$form['lookup']['pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_GRACE_PERIOD] = array(
'#type' => 'textarea',
'#title' => t('Grace period'),
'#description' => t('Sent to a user with an active membership that is
currently in a grace period after expiration.'),
'#default_value' => variable_get(
'pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_GRACE_PERIOD
),
'#element_validate' => array('token_element_validate'),
);
$form['lookup']['pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_EXPIRED] = array(
'#type' => 'textarea',
'#title' => t('Expired'),
'#description' => t('Sent to a user with an expired membership.'),
'#default_value' => variable_get(
'pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_EXPIRED
),
'#element_validate' => array('token_element_validate'),
);
$form['lookup']['pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_UNKNOWN] = array(
'#type' => 'textarea',
'#title' => t('Unknown'),
'#description' => t('Sent to a user when membership state cannot be
determined. This may be caused by a system error or unknown API state.'),
'#default_value' => variable_get(
'pbs_passport_lookup_email_' . PBS_PASSPORT_MEMBERSHIP_UNKNOWN
),
'#element_validate' => array('token_element_validate'),
);
return system_settings_form($form);
}
/**
* General module settings.
*
* @return array
* System settings form render array.
*/
function pbs_passport_settings_general() {
$form = array();
$form['pbs_passport_callsign'] = array(
'#title' => t('Station Call Sign'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => variable_get('pbs_passport_callsign'),
'#description' => t('Call letters as used by PBS to identify the
station, eg "WNET" or "WNJT"'),
);
$form['pbs_passport_station_id'] = array(
'#title' => t('Station ID'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_station_id'),
'#required' => TRUE,
'#description' => t('ID as used by PBS to identify the station.'),
'#prefix' => '<div id="station-id-wrapper">',
'#suffix' => '</div>',
);
$form['get_station_id'] = array(
'#type' => 'button',
'#value' => t('Get Station ID'),
'#ajax' => array(
'callback' => '_pbs_passport_station_id_callback',
'wrapper' => 'station-id-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['restricted_roles'] = array(
'#type' => 'fieldset',
'#title' => t('Restricted Roles'),
'#tree' => FALSE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$default_value = variable_get('pbs_passport_restricted_roles');
if (empty($default_value)) {
$default_value = array();
}
$form['restricted_roles']['pbs_passport_restricted_roles'] = array(
'#title' => t('Selected roles will not be able to log in using PBS
Passport authorization.'),
'#type' => 'checkboxes',
'#options' => user_roles(TRUE),
'#default_value' => $default_value,
'#description' => t('PBS.org does not use email verification for new
accounts so <strong>it is highly recommended that any roles with
administrative permissions be restricted</strong>.'),
);
$form['restricted_roles']['pbs_passport_restricted_roles_message'] = array(
'#title' => t('Login denied message'),
'#type' => 'textarea',
'#default_value' => variable_get('pbs_passport_restricted_roles_message'),
'#description' => t('Message to appear to a user with a restricted role
after login is denied.'),
);
return system_settings_form($form);
}
/**
* AJAX callback: get station ID from callsign.
*
* @param array $form
* Form array.
* @param array $form_state
* Submitted form state.
*
* @return array
* Returns a response array.
*
* @throws \Exception
*/
function _pbs_passport_station_id_callback(array $form, array &$form_state) {
$element = $form['pbs_passport_station_id'];
$callsign = $form_state['values']['pbs_passport_callsign'];
if (!empty($callsign)) {
$response = pbs_passport_get_station_data(NULL, $callsign);
if (!empty($response)) {
$element['#value'] = $response['id'];
drupal_set_message(t('<strong>Station ID</strong> updated.'));
}
else {
drupal_set_message(
t('<strong>Station ID</strong> not found. Verify
<strong>Station Call Sign</strong>.'),
'error'
);
}
}
else {
drupal_set_message(
t('<strong>Station Call Sign</strong> must be set to get
<strong>Station ID</strong>.'),
'error'
);
}
$commands = array();
$commands[] = ajax_command_replace(
'#station-id-wrapper',
drupal_render($element)
);
$commands[] = ajax_command_remove('#messages');
$commands[] = ajax_command_prepend(
NULL,
'<div id="messages">' . theme('status_messages') . '</div>'
);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* OAuth2-specific settings.
*
* @return array
* System settings form render array.
*/
function pbs_passport_settings_oauth() {
$form = array();
$form['help'] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('See <a href="@uri" target="_blank">Integrating PBS
Account with your website or app</a> for guidance on how to request this
information from PBS. A <strong>redirect URI</strong> must be provided to
PBS as part of the request process. The redirect URI for this site is
<strong>@redirect_uri</strong>.', array(
'@uri' => 'https://docs.pbs.org/display/uua/Integrating+PBS+Account+with+your+website+or+app',
'@redirect_uri' => PBS_PASSPORT_REDIRECT_URI,
)
),
);
$form['pbs_passport_oauth_client_id'] = array(
'#title' => t('LAAS Client ID'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_oauth_client_id'),
'#required' => TRUE,
'#description' => t('Client ID for authentication. Provided by
PBS.'),
);
$form['pbs_passport_oauth_client_secret'] = array(
'#title' => t('LAAS Client secret'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_oauth_client_secret'),
'#required' => TRUE,
'#description' => t('Client Secret for authentication. Provided by
PBS.'),
);
$form['pbs_passport_oauth_scope'] = array(
'#title' => t('OAuth Scope'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_oauth_scope'),
'#required' => TRUE,
'#description' => t('Scope for OAuth2 grant. Provided by PBS, will
typically look like "account wxyz". Case-sensitive.'),
);
return system_settings_form($form);
}
/**
* MVault-specific settings.
*
* @return array
* System settings form render array.
*/
function pbs_passport_settings_mvault() {
$form = array();
$form['help'] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('See <a href="@uri" target="_blank">Membership Vault
API: How do I obtain authentication credentials?</a> for guidance on how
to request this information from PBS.', array(
'@uri' => 'https://docs.pbs.org/display/MV/Membership+Vault+API#MembershipVaultAPI-HowdoIobtainauthenticationcredentials?',
)
),
);
$endpoints = array(
'prod' => 'Production',
'qa' => 'QA',
);
$form['pbs_passport_endpoint'] = array(
'#title' => t('MVault API Endpoint'),
'#type' => 'select',
'#options' => $endpoints,
'#default_value' => variable_get('pbs_passport_endpoint'),
'#description' => t('Endpoint to use for MVault requests. This should
almost always be "Production".'),
);
foreach ($endpoints as $endpoint => $endpoint_label) {
$form['pbs_passport_key_' . $endpoint] = array(
'#title' => t('API Key') . " ($endpoint_label)",
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_key_' . $endpoint),
'#description' => t('@label API Key for the MVault service.
Provided by PBS.', array('@label' => $endpoint_label)),
'#states' => array(
'visible' => array(
':input[name="pbs_passport_endpoint"]' => array('value' => $endpoint),
),
),
);
$form['pbs_passport_secret_' . $endpoint] = array(
'#title' => t('API Secret') . " ($endpoint_label)",
'#type' => 'textfield',
'#default_value' => variable_get('pbs_passport_secret_' . $endpoint),
'#description' => t('@label API Secret for the MVault service.
Provided by PBS.', array('@label' => $endpoint_label)),
'#states' => array(
'visible' => array(
':input[name="pbs_passport_endpoint"]' => array('value' => $endpoint),
),
),
);
}
$form['test_button'] = array(
'#type' => 'button',
'#value' => t('Test API Connection'),
'#ajax' => array(
'callback' => '_pbs_passport_api_connection_test_callback',
'wrapper' => 'test-results-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
// Test results will be displayed here.
$form['test_results'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => '',
'#attributes' => array(
'id' => 'test-results-wrapper',
'class' => array('element-hidden'),
),
);
return system_settings_form($form);
}
/**
* Test form submit handler for AJAX Callback.
*
* @param array $form
* Form array.
* @param array $form_state
* Submitted form state.
*
* @return array
* Returns a response array.
*/
function _pbs_passport_api_connection_test_callback(array $form, array &$form_state) {
$endpoint = $form_state['values']['pbs_passport_endpoint'];
$client = pbs_passport_get_mvault_client(
$endpoint,
$form_state['values']['pbs_passport_key_' . $endpoint],
$form_state['values']['pbs_passport_secret_' . $endpoint]
);
$response = $client->getMembershipByUid(0);
$element = $form['test_results'];
$element['#attributes']['class'] = array('messages');
if ($response === FALSE) {
$status = t('Connection attempt failed. Check API credentials.
Additional details may be found in site logs.');
$element['#attributes']['class'][] = 'error';
}
else {
$status = t('Connection test succeeded.');
$element['#attributes']['class'][] = 'status';
};
$element['#value'] = $status;
return $element;
}