Skip to content
Snippets Groups Projects
Commit 8ea8c820 authored by kksandr's avatar kksandr
Browse files

Added path filter

parent 34fc2c94
Branches
Tags
No related merge requests found
Pipeline #78660 failed
......@@ -7,3 +7,4 @@ skip_xml_http_request: false
methods:
GET: true
content_type: '#text/html#'
paths: { }
......@@ -29,3 +29,9 @@ path_watcher.settings:
content_type:
type: string
label: 'Content type (regex)'
paths:
type: sequence
label: 'Path filters (regex)'
sequence:
type: string
label: 'Path filter (regex)'
......@@ -22,3 +22,14 @@ function path_watcher_update_9000(): void {
$data['content_type'] = '#text/html#';
$config->setData($data)->save();
}
/**
* Populate the new config with default values.
*/
function path_watcher_update_9001(): void {
\Drupal::configFactory()
->getEditable(PathWatcherSettingsForm::CONFIG_NAME)
->set('paths', [])
->save();
}
......@@ -114,7 +114,7 @@ class PathWatcherSettingsForm extends ConfigFormBase {
];
$form['advanced']['methods'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Methods'),
'#title' => $this->t('Processed methods'),
'#options' => [
Request::METHOD_HEAD => Request::METHOD_HEAD,
Request::METHOD_GET => Request::METHOD_GET,
......@@ -132,22 +132,67 @@ class PathWatcherSettingsForm extends ConfigFormBase {
];
$form['advanced']['content_type'] = [
'#type' => 'textfield',
'#title' => $this->t('Content type (regex)'),
'#title' => $this->t('Content type filter (regex)'),
'#default_value' => $config['content_type'],
'#placeholder' => '#text/html#',
'#description' => $this->t('Leave empty to skip content type check.'),
];
$paths = $config['paths'];
$form['advanced']['paths'] = [
'#type' => 'textarea',
'#title' => $this->t('Path filters (regex)'),
'#default_value' => $paths ? implode(PHP_EOL, $paths) : '',
'#placeholder' => "#^/oauth/#\n#^/some-api/#",
'#description' => $this->t('One regular expression per line. Leave empty to skip path check.'),
'#rows' => max(min(5, count($paths) + 1), 2),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getErrors()) {
return;
}
// Validate content type filter.
$content_type = trim($form_state->getValue('content_type'));
@preg_match($content_type, '');
if (preg_last_error() !== PREG_NO_ERROR) {
$form_state->setErrorByName('content_type', $this->t('Invalid regular expression: @error', [
'@error' => preg_last_error_msg(),
]));
}
// Validate path filters.
$paths = array_unique(array_filter(array_map('trim', explode(PHP_EOL, $form_state->getValue('paths')))));
foreach ($paths as $path) {
@preg_match($path, '');
if (preg_last_error() !== PREG_NO_ERROR) {
$form_state->setErrorByName('paths', $this->t('Regular expression "@path" is invalid: @error', [
'@path' => $path,
'@error' => preg_last_error_msg(),
]));
break;
}
}
// Set values if no errors.
if (!$form_state->getErrors()) {
$form_state
->setValue('content_type', $content_type)
->setValue('paths', $paths);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->cleanValues()->getValues();
$values['skip_roles'] = Checkboxes::getCheckedCheckboxes($values['skip_roles']);
$values['methods'] = array_fill_keys(Checkboxes::getCheckedCheckboxes($values['methods']), TRUE);
$values['skip_usernames'] = array_unique(array_filter(array_map('trim', explode(PHP_EOL, $values['skip_usernames']))));
$values['methods'] = array_fill_keys(Checkboxes::getCheckedCheckboxes($values['methods']), TRUE);
$this->config(static::CONFIG_NAME)
->setData($values)
......
......@@ -71,6 +71,14 @@ class PathWatcherManager implements PathWatcherManagerInterface {
if ($config['skip_usernames'] && in_array($this->currentUser->getAccountName(), $config['skip_usernames'])) {
return FALSE;
}
if ($config['paths']) {
$path_info = $request->getPathInfo();
foreach ($config['paths'] as $path) {
if (preg_match($path, $path_info)) {
return FALSE;
}
}
}
return TRUE;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment