-
Notifications
You must be signed in to change notification settings - Fork 1
/
queueit.admin.inc
274 lines (258 loc) · 11.5 KB
/
queueit.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
<?php
/**
* @file
* Configuration of the Queue-it module.
*/
/**
* Form builder.
*/
function queueit_settings_form($form, &$form_state) {
$known_user = new QueueitKnownUser();
/* Main configuration */
$form['config'] = [
'#title' => 'Queue-it configuration',
'#type' => 'fieldset',
];
// Implementation type.
// * Integration - Validate a user via integration config.
// * JavaScript - Validate a user via JavaScript on the client side.
// * Code - Specify the configuration in code without using the Trigger/Action paradigm.
$form['config']['queueit_mode'] = [
'#type' => 'radios',
'#title' => t('Queue-it mode'),
'#options' => [
'integration' => 'Integration config',
'js' => 'JavaScript',
'code' => 'Configuration in code',
],
'#description' => t('Select the method used to integrate Queue-it:<br>Integration Config - used to load the configuration from the Go Queue-it platform.<br>JavaScript - protects pages by the queue on the client side.<br>Configuration in code - recommended for testing specific event or when your application server is not allowed to do external GET requests (this is without using the Trigger/Action paradigm).'),
'#default_value' => variable_get('queueit_mode', 'integration'),
];
$form['config']['queueit_debug'] = [
'#type' => 'checkbox',
'#title' => t('Enable debugging'),
'#description' => t('Displays Queue-it response on each qualifying page for admin users only.'),
'#default_value' => variable_get('queueit_debug', FALSE),
];
/* Integration mode */
$form['integration_config'] = [
'#title' => t('Integration settings'),
'#description' => t("Settings related to <i>Integration config</i> mode as defined in Queue-it configuration store and controlled via the Queue-it Go platform. The configuration file consists of Triggers and Actions to determine which pages to protect and which queues to use."),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => variable_get('queueit_mode', 'integration') <> 'integration',
'#disabled' => variable_get('queueit_mode', 'integration') <> 'integration',
'#states' => [
'disabled' => [':input[name="queueit_mode"]' => ['!value' => 'integration']],
'collapsed' => [':input[name="queueit_mode"]' => ['!value' => 'integration']],
]
];
$form['integration_config']['config'] = [
'#type' => 'item',
'#prefix' => t('Current configuration:'),
'#markup' => '<pre>' . $known_user->getIntegrationConfig() . '</pre>',
];
$form['integration_config']['refresh'] = [
'#type' => 'submit',
'#value' => t('Refresh'),
'#executes_submit_callback' => FALSE,
'#suffix' => sprintf(t('Last update: %s<br>'), strftime("%c", $known_user->getIntegrationConfigTimestamp()))
. sprintf(t('Endpoint: <a href="%s">%s</a><br>'), $known_user->getIntegrationConfigUrl(), $known_user->getIntegrationConfigUrl())
. t("<br>Use <i>Refresh</i> button to fetch the latest integration config from Queue-it Go platform. The config is cached with a TTL of 5 mins, after which the new config is loaded on any page load. Clicking the <i>Refresh</i> button more than once every 5 seconds will not trigger another fetch."),
];
/* Configuration using code */
$form['event_config'] = [
'#title' => 'Event config',
'#description' => t('When configuration in code is selected, you can specify the following parameters without using the Trigger/Action paradigm. Useful when your application server is not allowed to do external GET requests.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => variable_get('queueit_mode', 'integration') <> 'code',
'#disabled' => variable_get('queueit_mode', 'integration') <> 'code',
'#states' => [
'disabled' => [':input[name="queueit_mode"]' => ['!value' => 'code']],
'collapsed' => [':input[name="queueit_mode"]' => ['!value' => 'code']],
]
];
$form['event_config']['queueit_event_id'] = [
'#type' => 'textfield',
'#title' => t('Event ID'),
'#description' => t('Specify ID of the queue to use.'),
'#default_value' => variable_get('queueit_event_id'),
];
$form['event_config']['queueit_queue_domain'] = [
'#type' => 'textfield',
'#title' => t('Existing domain name of the queue - usually in the format [CustomerId].queue-it.net.'),
'#description' => t('Specify ID of the queue to use.'),
'#default_value' => variable_get('queueit_queue_domain'),
];
$form['event_config']['queueit_cookie_validity'] = [
'#type' => 'textfield',
'#title' => t('Validity of the Queue-it session cookie.'),
'#description' => t('Optional. Validity of the Queue-it session cookie. Default is 10 minutes.'),
'#default_value' => variable_get('queueit_cookie_validity'),
'#element_validate' => ['element_validate_integer_positive'],
];
$form['event_config']['queueit_extend_cookie_validity'] = [
'#type' => 'checkbox',
'#title' => t('Extended validity of session cookie.'),
'#description' => t('Should the Queue-it session cookie validity time be extended each time the validation runs? By default it is enabled.'),
'#default_value' => variable_get('queueit_extend_cookie_validity', TRUE),
];
$form['event_config']['queueit_layout_name'] = [
'#type' => 'textfield',
'#title' => t('Name of the queue ticket layout.'),
'#description' => t('Optional. E.g. "Default layout by Queue-it. Default is to take what is specified on the Event.'),
'#default_value' => variable_get('queueit_layout_name'),
];
$form['event_config']['queueit_culture_of_layout'] = [
'#type' => 'textfield',
'#title' => t('Culture of the queue ticket layout.'),
'#description' => t('Culture of the queue ticket layout in the format specified <a href="!url">here</a>.',
['!url' => 'https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx']
),
'#default_value' => variable_get('queueit_culture_of_layout'),
];
/* Validation parameters */
$form['validate'] = [
'#title' => 'Validation logic',
'#type' => 'fieldset',
];
// Exclude Queue-it on specific pages.
$form['validate']['queueit_exclude_pages'] = [
'#type' => 'textarea',
'#title' => t('Exclude Queue-it on specific pages.'),
'#default_value' => variable_get('queueit_exclude_pages', QUEUEIT_EXCLUDE),
'#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. It is recommended to exclude resources and AJAX calls."),
];
$form['validate']['queueit_ignore_post'] = [
'#type' => 'checkbox',
'#title' => t('Ignore queue validation for the POST requests.'),
'#description' => t('Ignores the KnownUser validation for pages sent via HTTP POST request.'),
'#default_value' => variable_get('queueit_ignore_post', TRUE),
];
$form['validate']['queueit_ignore_cli'] = [
'#type' => 'checkbox',
'#title' => t('Ignore queue validation for the command-line interface.'),
'#description' => t('Ignores the KnownUser validation when invoked via CLI (such as using drush).'),
'#default_value' => variable_get('queueit_ignore_cli', TRUE),
];
/* Credentials */
$form['creds'] = [
'#title' => 'Queue-it Credentials',
'#type' => 'fieldset',
];
$form['creds']['queueit_customer_id'] = [
'#type' => 'textfield',
'#title' => t('Customer ID'),
'#description' => t('Your Queue-it customer ID.'),
'#default_value' => variable_get('queueit_customer_id'),
'#required' => TRUE,
];
$form['creds']['queueit_api_key'] = [
'#type' => 'textfield',
'#title' => t('API Key'),
'#description' => t('Specify the Api-key which can be supplied through the query string parameter. The key can be found in the GO Queue-it Platform.'),
'#default_value' => variable_get('queueit_api_key'),
'#states' => [
'required' => [':input[name="queueit_mode"]' => ['!value' => 'js']],
]
];
$form['creds']['queueit_secret_key'] = [
'#type' => 'textfield',
'#title' => t('Secret key'),
'#description' => t('Your 72 char secret key as specified in Go Queue-it self-service platform.'),
'#default_value' => variable_get('queueit_secret_key'),
'#states' => [
'required' => [':input[name="queueit_mode"]' => ['!value' => 'js']],
]
];
return system_settings_form($form);
}
/**
* Form builder.
*/
function queueit_settings_form_validate($form, &$form_state) {
// Fail in case of any existing form validation errors.
if (form_get_errors()) {
return FALSE;
}
// Initialize required variables.
$int_method = $form_state['values']['queueit_mode'];
$customer_id = $form_state['values']['queueit_customer_id'];
$api_key = $form_state['values']['queueit_api_key'];
$secret_key = $form_state['values']['queueit_secret_key'];
// Check the backend method requirements.
if ($int_method <> 'js') {
module_load_include('install', 'queueit');
$requirements = module_invoke('queueit', 'requirements', 'runtime');
$severity = drupal_requirements_severity($requirements);
if ($severity == REQUIREMENT_ERROR) {
form_set_error(NULL, 'KnownUser.V3 library not installed!');
return FALSE;
}
}
// Manually load the file classes.
module_load_include('php', 'queueit', 'src/classes/QueueitBase');
module_load_include('php', 'queueit', 'src/classes/QueueitKnownUser');
// Validate the configuration.
try {
switch ($int_method) {
case 'js':
$result = QueueitBase::validateJsEndpoints();
if (!$result) {
drupal_set_message(t('Cannot access JS endpoints'), 'warning');
}
break;
case 'code':
$known_user = new QueueitKnownUser($int_method, $customer_id, $secret_key, $api_key);
if (!$known_user->validateConfig()) {
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
}
$result = $known_user->resolveRequestByLocalEventConfig();
break;
case 'integration':
$known_user = new QueueitKnownUser($int_method, $customer_id, $secret_key, $api_key);
if (!$known_user->validateConfig()) {
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
}
$config = $known_user->getIntegrationConfig();
if (!$config) {
drupal_set_message(
t('Cannot fetch the integration configuration from !url.',
['!url' => $known_user->getIntegrationConfigUrl()]),
'warning');
}
elseif (!json_decode($config)) {
drupal_set_message(
t('Cannot decode the integration configuration from !url.',
['!url' => $known_user->getIntegrationConfigUrl()]),
'warning');
}
$result = $known_user->validateRequestByIntegrationConfig();
break;
default:
form_set_error('queueit_mode', $e->getMessage());
drupal_set_message(
t('Invalid integration method: !method.',
['!method' => $int_method]),
'error');
}
}
catch (\Exception $e) {
drupal_set_message(t('Exception error:') . ' ' . $e->getMessage(), 'error');
watchdog_exception('queueit', $e);
form_set_error('queueit_mode', $e->getMessage());
}
if (!empty($result) && $result) {
$op = $form_state['triggering_element']['#value'];
if ($op == 'Refresh' && $known_user->getIntegrationConfig(5)) {
drupal_set_message(t('Configuration successfully refreshed.'), 'status');
}
else {
drupal_set_message(t('Configuration successfully validated.'), 'status');
}
}
else {
drupal_set_message(t('Configuration not valid. Please verify your configuration.'), 'warning');
}
}