Skip to content

Commit df5b4e5

Browse files
committed
Implements the main redirect logic
1 parent bc8f785 commit df5b4e5

File tree

4 files changed

+221
-52
lines changed

4 files changed

+221
-52
lines changed

queueit.admin.inc

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,104 @@
99
* Form builder.
1010
*/
1111
function queueit_settings_form($form, &$form_state) {
12-
// Configuration parameters.
12+
/* Main configuration */
1313
$form['config'] = array(
1414
'#title' => 'Queue-it Configuration',
1515
'#type' => 'fieldset',
1616
);
17-
$form['config']['queueit_event_id'] = array(
17+
// Implementation type.
18+
// * Integration - Validate a user via integration config.
19+
// * Code - Specify the configuration in code without using the Trigger/Action paradigm.
20+
$form['config']['queueit_mode'] = array(
21+
'#type' => 'radios',
22+
'#title' => t('Queue-it mode.'),
23+
'#options' => [
24+
'integration' => 'Integration config',
25+
'code' => 'Configuration in code',
26+
//'js' => 'JavaScript', // @todo
27+
],
28+
'#description' => t('Select the type of Queue-it implementation.<br>Select Integration Config to load the configuration from the Go Queue-it platform.<br>Select Configuration in code if your application server is not allowed to do external GET requests (this is without using the Trigger/Action paradigm).'),
29+
'#default_value' => variable_get('queueit_mode', 'code'),
30+
);
31+
$form['config']['queueit_debug'] = array(
32+
'#type' => 'checkbox',
33+
'#title' => t('Enable debugging'),
34+
'#description' => t('Displays Queue-it response on each qualifying page for admin users only.'),
35+
'#default_value' => variable_get('queueit_debug', FALSE),
36+
);
37+
/* Configuration using code */
38+
$form['event_config'] = array(
39+
'#title' => 'Event config',
40+
'#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.'),
41+
'#type' => 'fieldset',
42+
'#collapsible' => TRUE,
43+
'#collapsed' => variable_get('queueit_mode', 'code') == 'integration',
44+
'#disabled' => variable_get('queueit_mode', 'code') == 'integration',
45+
);
46+
$form['event_config']['queueit_event_id'] = array(
1847
'#type' => 'textfield',
1948
'#title' => t('Event ID'),
2049
'#description' => t('Specify ID of the queue to use.'),
2150
'#default_value' => variable_get('queueit_event_id'),
2251
);
23-
$form['config']['queueit_queue_domain'] = array(
52+
$form['event_config']['queueit_queue_domain'] = array(
2453
'#type' => 'textfield',
2554
'#title' => t('Domain name of the queue - usually in the format [CustomerId].queue-it.net.'),
2655
'#description' => t('Specify ID of the queue to use.'),
2756
'#default_value' => variable_get('queueit_queue_domain'),
2857
);
29-
$form['config']['queueit_cookie_validity'] = array(
58+
$form['event_config']['queueit_cookie_validity'] = array(
3059
'#type' => 'textfield',
3160
'#title' => t('Validity of the Queue-it session cookie.'),
3261
'#description' => t('Optional. Validity of the Queue-it session cookie. Default is 10 minutes.'),
3362
'#default_value' => variable_get('queueit_cookie_validity'),
3463
'#element_validate' => ['element_validate_integer_positive'],
3564
);
36-
$form['config']['queueit_extend_cookie_validity'] = array(
65+
$form['event_config']['queueit_extend_cookie_validity'] = array(
3766
'#type' => 'checkbox',
3867
'#title' => t('Extended validity of session cookie.'),
3968
'#description' => t('Should the Queue-it session cookie validity time be extended each time the validation runs? By default it is enabled.'),
4069
'#default_value' => variable_get('queueit_extend_cookie_validity', TRUE),
4170
);
42-
$form['config']['queueit_layout_name'] = array(
71+
$form['event_config']['queueit_layout_name'] = array(
4372
'#type' => 'textfield',
4473
'#title' => t('Name of the queue ticket layout.'),
4574
'#description' => t('Optional. E.g. "Default layout by Queue-it. Default is to take what is specified on the Event.'),
4675
'#default_value' => variable_get('queueit_layout_name'),
4776
);
48-
$form['config']['queueit_culture_of_layout'] = array(
77+
$form['event_config']['queueit_culture_of_layout'] = array(
4978
'#type' => 'textfield',
5079
'#title' => t('Culture of the queue ticket layout.'),
5180
'#description' => t('Culture of the queue ticket layout in the format specified <a href="!url">here</a>.',
5281
[ '!url' => 'https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx' ]
5382
),
5483
'#default_value' => variable_get('queueit_culture_of_layout'),
84+
);
85+
/* Validation parameters */
86+
$form['validate'] = array(
87+
'#title' => 'Validation logic',
88+
'#type' => 'fieldset',
5589
);
5690
// Exclude Queue-it on specific pages.
57-
$form['config']['queueit_exclude_pages'] = array(
91+
$form['validate']['queueit_exclude_pages'] = array(
5892
'#type' => 'textarea',
5993
'#title' => t('Exclude Queue-it on specific pages.'),
60-
'#default_value' => variable_get('queueit_exclude_pages', "admin\nadmin/*\nsystem/ajax/*"),
61-
'#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard."),
94+
'#default_value' => variable_get('queueit_exclude_pages', QUEUEIT_EXCLUDE),
95+
'#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."),
6296
);
63-
// Credentials.
97+
$form['validate']['queueit_ignore_post'] = array(
98+
'#type' => 'checkbox',
99+
'#title' => t('Ignore queue validation for the POST requests.'),
100+
'#description' => t('Ignores the KnownUser validation for pages sent via HTTP POST request.'),
101+
'#default_value' => variable_get('queueit_ignore_post', TRUE),
102+
);
103+
$form['validate']['queueit_ignore_cli'] = array(
104+
'#type' => 'checkbox',
105+
'#title' => t('Ignore queue validation for the command-line interface.'),
106+
'#description' => t('Ignores the KnownUser validation when invoked via CLI (such as using drush).'),
107+
'#default_value' => variable_get('queueit_ignore_cli', TRUE),
108+
);
109+
/* Credentials */
64110
$form['creds'] = array(
65111
'#title' => 'Queue-it Credentials',
66112
'#type' => 'fieldset',
@@ -88,3 +134,47 @@ function queueit_settings_form($form, &$form_state) {
88134

89135
return system_settings_form($form);
90136
}
137+
138+
/**
139+
* Form builder.
140+
*/
141+
function queueit_settings_form_validate($form, &$form_state) {
142+
// Manually load the file classes.
143+
module_load_include('php', 'queueit', 'src/classes/QueueitBase');
144+
module_load_include('php', 'queueit', 'src/classes/QueueitKnownUser');
145+
// Validate the configuration.
146+
try {
147+
$known_user = new QueueitKnownUser;
148+
if (!$known_user->validateConfig()) {
149+
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
150+
}
151+
switch (variable_get('queueit_mode', 'code')) {
152+
case 'code':
153+
$result = $known_user->validateRequestByIntegrationConfig();
154+
break;
155+
case 'integration':
156+
$config = $known_user->getIntegrationConfig();
157+
if (!$config) {
158+
drupal_set_message(
159+
t('Cannot fetch the integration configuration from !url.',
160+
['!url' => $known_user->getIntegrationConfigPath()]),
161+
'warning');
162+
}
163+
elseif (!json_decode($config)) {
164+
drupal_set_message(
165+
t('Cannot decode the integration configuration from !url.',
166+
['!url' => $known_user->getIntegrationConfigPath()]),
167+
'warning');
168+
}
169+
$result = $known_user->resolveRequestByLocalEventConfig();
170+
break;
171+
}
172+
}
173+
catch (\Exception $e) {
174+
drupal_set_message(t('Exception error: ') . $e->getMessage(), 'error');
175+
watchdog_exception('queueit', $e);
176+
}
177+
if (!empty($result) && $result) {
178+
drupal_set_message(t('Configuration successfully validated.'), 'status');
179+
}
180+
}

queueit.module

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
* Queue-it main module file.
66
*/
77

8+
define('QUEUEIT_EXCLUDE', "admin\nadmin/*\nsites/*\nsystem/ajax/*");
9+
810
/**
911
* Implements hook_help().
1012
*/
1113
function queueit_help($path, $arg) {
1214
// Load help content from the README file.
1315
$content = file_get_contents(drupal_get_path('module', 'queueit') . '/README.md');
14-
$content .= file_get_contents(libraries_get_path('composer') . '/queueit/KnownUser.V3.PHP' . '/README.md');
16+
$content .= file_get_contents(libraries_get_path('composer') . '/queueit/knownuserv3' . '/README.md');
1517
module_load_include('inc', 'queueit');
1618
switch ($path) {
1719
// Main module help for the queueit module
@@ -24,13 +26,21 @@ function queueit_help($path, $arg) {
2426
* Implements hook_init().
2527
*/
2628
function queueit_init() {
27-
if (user_access('bypass queueit')) {
29+
30+
// Check if user role is excluded from the Queue-it processing.
31+
// If debug mode is enabled, ignore bypassing for debugging purposes.
32+
if (user_access('bypass queueit') && !variable_get('queueit_debug', FALSE)) {
2833
return;
2934
}
30-
// Check for matching path to exclude.
35+
36+
// The KnownUser validation must _only_ be done on page requests.
37+
// So, if you add the KnownUser validation logic to a central place,
38+
// then be sure that the Triggers only fire on page requests
39+
// and not on e.g. image or ajax requests.
3140
$curr_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
32-
$exclude_paths = variable_get('queueit_exclude_pages', "admin/*\nsystem/ajax/*");
41+
$exclude_paths = variable_get('queueit_exclude_pages', QUEUEIT_EXCLUDE);
3342
foreach (explode("\n", $exclude_paths) as $path) {
43+
// Check for matching page path to exclude.
3444
$page_match = drupal_match_path($curr_path, drupal_strtolower($path));
3545
if ($curr_path != $_GET['q']) {
3646
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
@@ -39,6 +49,44 @@ function queueit_init() {
3949
return;
4050
}
4151
}
52+
$known_user = new QueueitKnownUser;
53+
// Ignore processing if the config is invalid.
54+
if (!$known_user->validateConfig()) {
55+
return;
56+
}
57+
// Ignore processing for when in CLI mode.
58+
if (variable_get('queueit_ignore_cli', TRUE) && drupal_is_cli()) {
59+
return;
60+
}
61+
// Ignore processing for pages sent via POST.
62+
if (variable_get('queueit_ignore_post', TRUE) && !empty($_POST)) {
63+
return;
64+
}
65+
try {
66+
switch (variable_get('queueit_mode', 'code')) {
67+
case 'code':
68+
$result = $known_user->validateRequestByIntegrationConfig();
69+
break;
70+
case 'integration':
71+
$result = $known_user->resolveRequestByLocalEventConfig();
72+
break;
73+
}
74+
if (!empty($result)
75+
&& variable_get('queueit_debug', FALSE)
76+
&& user_access('administer queueit')
77+
) {
78+
// Displays response when in debug mode for admin users.
79+
drupal_set_message(var_export($result, TRUE), 'status');
80+
}
81+
if (!user_access('bypass queueit') && $result->doRedirect()) {
82+
// Send the user to the queue.
83+
// Either because hash was missing or because it was invalid.
84+
drupal_goto($result->redirectUrl, ['absolute' => TRUE]);
85+
}
86+
}
87+
catch (\Exception $e) {
88+
watchdog_exception('queueit', $e);
89+
}
4290
}
4391

4492
/**
@@ -63,12 +111,12 @@ function queueit_menu() {
63111
*/
64112
function queueit_library() {
65113
// Register libraries available in the external directory.
66-
$path = libraries_get_path('composer') . '/queueit/KnownUser.V3.PHP';
114+
$path = libraries_get_path('composer') . '/queueit/knownuserv3';
67115
$libraries['queueit/KnownUser'] = array(
68116
'title' => 'KnownUser.V3.PHP',
69117
'version' => '3',
70118
);
71-
return $libraries;
119+
return $libraries;
72120
}
73121

74122
/**

0 commit comments

Comments
 (0)