fix: handle feed rules

pull/7520/head
Luciano Righetti 2021-06-22 17:46:33 +02:00
parent 1c39af3385
commit c1bb5cbc1d
10 changed files with 212 additions and 171 deletions

View File

@ -327,6 +327,7 @@ class FeedsController extends AppController
'provider',
'enabled',
'caching_enabled',
'pull_rules',
'rules',
'url',
'distribution',
@ -442,7 +443,6 @@ class FeedsController extends AppController
'menuItem' => 'edit',
]);
$this->set('edit', true);
$this->set('feedId', $feedId);
$this->render('add');
}

View File

@ -1,4 +1,5 @@
<?php
$modelForForm = 'Cerebrates';
$edit = $this->request->params['action'] === 'edit' ? true : false;
$fields = [
[
@ -40,6 +41,7 @@ $fields = [
echo $this->element('genericElements/Form/genericForm', [
'data' => [
'description' => false,
'model' => 'Cerebrate',
'title' => $edit ? __('Edit Cerebrate connection') : __('Add Cerebrate connection'),
'fields' => $fields,
'submit' => [
@ -52,4 +54,3 @@ echo $this->element('genericElements/Form/genericForm', [
if (!$ajax) {
echo $this->element('/genericElements/SideMenu/side_menu', $menuData);
}
?>

View File

@ -1,3 +1,5 @@
<?php
$params['div'] = false;
echo $this->Form->input($fieldData['field'], $params);

View File

@ -1,4 +1,5 @@
<?php
$fieldData['type'] = 'select';
echo $this->Form->input($fieldData['field'], $fieldData);

View File

@ -1,3 +1,82 @@
<?php
// TODO: Feed pull rules component
echo 'pull rules form';
<b><?php echo __('Filter rules'); ?>:</b><br />
<span id="pull_tags_OR" style="display:none;"><?php echo __('Events with the following tags allowed'); ?>: <span id="pull_tags_OR_text" style="color:green;"></span><br /></span>
<span id="pull_tags_NOT" style="display:none;"><?php echo __('Events with the following tags blocked'); ?>: <span id="pull_tags_NOT_text" style="color:red;"></span><br /></span>
<span id="pull_orgs_OR" style="display:none;"><?php echo __('Events with the following organisations allowed'); ?>: <span id="pull_orgs_OR_text" style="color:green;"></span><br /></span>
<span id="pull_orgs_NOT" style="display:none;"><?php echo __('Events with the following organisations blocked'); ?>: <span id="pull_orgs_NOT_text" style="color:red;"></span><br /></span>
<span id="pull_modify" class="btn btn-inverse" style="line-height:10px; padding: 4px 4px;"><?php echo __('Modify'); ?></span><br /><br />
<div id="hiddenRuleForms">
<?php
$pullRules = json_decode($fieldData['pull_rules'], true);
$pullRules['url_params'] = json_decode($fieldData['pull_rules'], true);
echo $this->Form->input('pull_rules', array('style' => 'display:none;', 'label' => false, 'div' => false));
$modalData = [
'data' => [
'title' => __('Set PULL rules'),
'content' => [
[
'html' => sprintf('<h5 style="font-weight: normal;"><i>%s</i></h5>', __('Configure the rules to be applied when PULLing data to the server'))
],
[
'html' => $this->element('serverRuleElements/pull', [
'context' => $this->Form->defaultModel,
'allTags' => $fieldData['tags'],
'allOrganisations' => $fieldData['orgs'],
'ruleObject' => $pullRules
])
]
],
],
'type' => 'xl',
'class' => 'pull-rule-modal',
'confirm' => [
'title' => __('Update'),
'onclick' => "serverRulesUpdateState('pull');"
]
];
echo $this->element('genericElements/infoModal', $modalData);
?>
</div>
<script type="text/javascript">
var rules = {
"pull": {
"tags": {
"OR": [],
"NOT": []
},
"orgs": {
"OR": [],
"NOT": []
}
}
};
var validOptions = ['pull'];
var validFields = ['tags', 'orgs'];
var modelContext = '<?= $this->Form->defaultModel ?>';
$(document).ready(function() {
$("#pull_modify").click(function() {
$('#genericModal.pull-rule-modal').modal()
.on('shown', function() {
var $containers = $(this).find('.rules-widget-container')
$containers.each(function() {
var initFun = $(this).data('funname');
if (typeof window[initFun] === 'function') {
window[initFun]()
}
})
})
.on('hidden', function() {
var $containers = $(this).find('.rules-widget-container')
$containers.each(function() {
if ($(this).data('resetrulesfun') !== undefined) {
$(this).data('resetrulesfun')()
}
})
});
});
});
</script>

View File

@ -10,6 +10,7 @@
if (empty($fieldData['label'])) {
$fieldData['label'] = Inflector::humanize($fieldData['field']);
}
if (!empty($fieldDesc[$fieldData['field']])) {
$fieldData['label'] .= $this->element(
'genericElements/Form/formInfo', array(
@ -33,6 +34,17 @@
if (empty($fieldData['type']) || $fieldData['type'] !== 'checkbox' ) {
$params['class'] .= ' form-control';
}
if (!empty($fieldData['description'])) {
if (!isset($params['class'])) {
$params['class'] = '';
}
$params['class'] .= ' input-with-description';
}
if (!empty($fieldData['picker']) && isset($fieldData['picker']['function'])) {
$fieldData['picker']['text'] = isset($fieldData['picker']['text']) ? $fieldData['picker']['text'] : __('Picker');
$params['div'] = 'input text input-append';
$params['after'] = sprintf('<button type="button" class="btn" onclick="%s.call(this);">%s</button>', $fieldData['picker']['function'], __($fieldData['picker']['text']));
}
//$params['class'] = sprintf('form-control %s', $params['class']);
foreach ($fieldData as $k => $fd) {
if (!isset($simpleFieldWhitelist) || in_array($k, $simpleFieldWhitelist) || strpos($k, 'data-') === 0) {

View File

@ -1,5 +1,5 @@
<?php
/*
/*
* Generic form builder
*
* Simply pass a JSON with the following keys set:
@ -13,170 +13,113 @@
- use these to define dynamic form fields, or anything that will feed into the regular fields via JS population
* - submit: The submit button itself. By default it will simply submit to the form as defined via the 'model' field
*/
$modelForForm = empty($data['model']) ?
h(Inflector::singularize(Inflector::classify($this->request->params['controller']))) :
h($data['model']);
$fieldsString = '';
$simpleFieldAllowedlist = array(
'default',
'type',
'options',
'placeholder',
'label',
'empty',
'rows',
'div',
'required',
'checked',
'multiple',
'selected',
'legend',
'disabled',
$modelForForm = empty($data['model']) ?
h(Inflector::singularize(Inflector::classify($this->request->params['controller']))) :
h($data['model']);
$fieldsString = '';
$simpleFieldAllowedlist = array(
'default',
'type',
'options',
'placeholder',
'label',
'empty',
'rows',
'div',
'required',
'checked',
'multiple',
'selected',
'legend',
'disabled',
);
$fieldsArrayForPersistence = array();
$formOptions = isset($formOptions) ? $formOptions : array();
$formOptions = array_merge(['class' => 'genericForm'], $formOptions);
$formCreate = $this->Form->create($modelForForm, $formOptions);
if (!empty($data['fields'])) {
foreach ($data['fields'] as $fieldData) {
if (isset($fieldData['requirements']) && !$fieldData['requirements']) {
continue;
}
$fieldsString .= $this->element(
'genericElements/Form/fieldScaffold',
[
'fieldData' => $fieldData,
'form' => $this->Form,
'simpleFieldWhitelist' => $simpleFieldAllowedlist
]
);
if (empty($fieldData['stayInLine'])) {
$fieldsString .= '<div class="clear"></div>';
}
}
}
$metaFieldString = '';
if (!empty($data['metaFields'])) {
foreach ($data['metaFields'] as $metaField) {
$metaFieldString .= $metaField;
}
}
$submitButtonData = array('model' => $modelForForm);
if (!empty($data['submit'])) {
$submitButtonData = array_merge($submitButtonData, $data['submit']);
}
if (!empty($data['ajaxSubmit'])) {
$submitButtonData['ajaxSubmit'] = $ajaxSubmit;
}
$ajaxFlashMessage = '';
if ($ajax) {
$ajaxFlashMessage = sprintf(
'<div id="flashContainer"><div id="main-view-container">%s</div></div>',
$this->Flash->render()
);
$fieldsArrayForPersistence = array();
$formOptions = isset($formOptions) ? $formOptions : array();
// $formOptions = array_merge(['class' => 'genericForm'], $formOptions);
$formCreate = $this->Form->create($modelForForm, $formOptions);
if (!empty($data['fields'])) {
foreach ($data['fields'] as $fieldData) {
if (isset($fieldData['requirements']) && !$fieldData['requirements']) {
continue;
}
$fieldsString .= $this->element(
'genericElements/Form/fieldScaffold', [
'fieldData' => $fieldData,
'form' => $this->Form,
'simpleFieldWhitelist' => $simpleFieldAllowedlist
]
);
// previous form fields generation logic
// if (is_array($fieldData)) {
// if (empty($fieldData['label'])) {
// $fieldData['label'] = Inflector::humanize($fieldData['field']);
// }
// if (!empty($fieldDesc[$fieldData['field']])) {
// $fieldData['label'] .= $this->element(
// 'genericElements/Form/formInfo', array(
// 'field' => $fieldData,
// 'fieldDesc' => $fieldDesc[$fieldData['field']],
// 'modelForForm' => $modelForForm
// )
// );
// }
// $params = array();
// if (!empty($fieldData['class'])) {
// if (is_array($fieldData['class'])) {
// $class = implode(' ', $fieldData['class']);
// } else {
// $class = $fieldData['class'];
// }
// $params['class'] = $class;
// } else {
// if (!empty($fieldData['type']) && ($fieldData['type'] !== 'checkbox' && $fieldData['type'] !== 'radio')) {
// $params['class'] = 'span6';
// }
// }
// foreach ($simpleFieldAllowedlist as $f) {
// if (!empty($fieldData[$f])) {
// $params[$f] = $fieldData[$f];
// }
// }
// if (!empty($fieldData['picker']) && isset($fieldData['picker']['function'])) {
// $fieldData['picker']['text'] = isset($fieldData['picker']['text']) ? $fieldData['picker']['text'] : __('Picker');
// $params['div'] = 'input text input-append';
// $params['after'] = sprintf('<button type="button" class="btn" onclick="%s.call(this);">%s</button>', $fieldData['picker']['function'], __($fieldData['picker']['text']));
// }
// if (!empty($params['type']) && $params['type'] === 'dropdown') {
// $params['type'] = 'select';
// }
// if (!empty($fieldData['description'])) {
// if (!isset($params['class'])) {
// $params['class'] = '';
// }
// $params['class'] .= ' input-with-description';
// }
// $temp = $this->Form->input($fieldData['field'], $params);
// if (!empty($fieldData['description'])) {
// $temp .= sprintf('<small class="clear form-field-description apply_css_arrow">%s</small>', h($fieldData['description']));
// }
// $fieldsArrayForPersistence[] = $modelForForm . Inflector::camelize($fieldData['field']);
// if (!empty($fieldData['hidden'])) {
// $temp = '<span class="hidden">' . $temp . '</span>';
// }
// $fieldsString .= $temp;
// } else {
// $fieldsString .= $fieldData;
// }
if (empty($fieldData['stayInLine'])) {
$fieldsString .= '<div class="clear"></div>';
}
}
}
$metaFieldString = '';
if (!empty($data['metaFields'])) {
foreach ($data['metaFields'] as $metaField) {
$metaFieldString .= $metaField;
}
}
$submitButtonData = array('model' => $modelForForm);
if (!empty($data['submit'])) {
$submitButtonData = array_merge($submitButtonData, $data['submit']);
}
if (!empty($data['ajaxSubmit'])) {
$submitButtonData['ajaxSubmit'] = $ajaxSubmit;
}
$ajaxFlashMessage = '';
if ($ajax) {
$ajaxFlashMessage = sprintf(
'<div id="flashContainer"><div id="main-view-container">%s</div></div>',
$this->Flash->render()
);
}
$formEnd = $this->Form->end();
if (!empty($ajax)) {
echo sprintf(
'<div id="genericModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="genericModalLabel" aria-hidden="true">%s%s%s</div>',
}
$formEnd = $this->Form->end();
if (!empty($ajax)) {
echo sprintf(
'<div id="genericModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="genericModalLabel" aria-hidden="true">%s%s%s</div>',
sprintf(
'<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="genericModalLabel">%s</h3></div>',
empty($data['title']) ? h(Inflector::humanize($this->request->params['action'])) . ' ' . $modelForForm : h($data['title'])
),
sprintf(
'<div class="modal-body modal-body-long">%s</div>',
sprintf(
'<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="genericModalLabel">%s</h3></div>',
empty($data['title']) ? h(Inflector::humanize($this->request->params['action'])) . ' ' . $modelForForm : h($data['title'])
),
sprintf(
'<div class="modal-body modal-body-long">%s</div>',
sprintf(
'%s%s<fieldset>%s%s</fieldset>%s%s',
empty($data['description']) ? '' : $data['description'],
$formCreate,
$ajaxFlashMessage,
$fieldsString,
$metaFieldString,
$formEnd
)
),
sprintf(
'<div class="modal-footer">%s</div>',
$this->element('genericElements/Form/submitButton', $submitButtonData)
'%s%s<fieldset>%s%s</fieldset>%s%s',
empty($data['description']) ? '' : $data['description'],
$formCreate,
$ajaxFlashMessage,
$fieldsString,
$metaFieldString,
$formEnd
)
);
} else {
echo sprintf(
'<div class="%s">%s<fieldset><legend>%s</legend>%s<div class="clear">%s</div>%s</fieldset>%s%s%s</div>',
empty($data['skip_side_menu']) ? 'form' : 'menuless-form',
$formCreate,
empty($data['title']) ? h(Inflector::humanize($this->request->params['action'])) . ' ' . $modelForForm : h($data['title']),
$ajaxFlashMessage,
empty($data['description']) ? '' : $data['description'],
$fieldsString,
$metaFieldString,
$this->element('genericElements/Form/submitButton', $submitButtonData),
$formEnd
);
}
),
sprintf(
'<div class="modal-footer">%s</div>',
$this->element('genericElements/Form/submitButton', $submitButtonData)
)
);
} else {
echo sprintf(
'<div class="%s">%s<fieldset><legend>%s</legend>%s<div class="clear">%s</div>%s</fieldset>%s%s%s</div>',
empty($data['skip_side_menu']) ? 'form' : 'menuless-form',
$formCreate,
empty($data['title']) ? h(Inflector::humanize($this->request->params['action'])) . ' ' . $modelForForm : h($data['title']),
$ajaxFlashMessage,
empty($data['description']) ? '' : $data['description'],
$fieldsString,
$metaFieldString,
$this->element('genericElements/Form/submitButton', $submitButtonData),
$formEnd
);
}
?>
<script type="text/javascript">
var fieldsArray = <?php echo json_encode($fieldsArrayForPersistence); ?>;
$(function() {
popoverStartup();
});
</script>
</script>

View File

@ -26,4 +26,3 @@
empty($text) ? __('Submit') : h($text)
);
}
?>

View File

@ -70,11 +70,11 @@
<?= __('Additional sync parameters (based on the event index filters)');?>
</div>
<div style="display: flex;">
<textarea style="width:100%;" placeholder='{"timestamp": "30d"}' type="text" value="" id="urlParams" required="required" data-original-title="" title="" rows="3"
<textarea style="width:100%;" placeholder='{"timestamp": "30d"}' type="text" value="" id="urlParams" data-original-title="" title="" rows="3"
><?= !empty($ruleUrlParams) ? json_encode(h($ruleUrlParams), JSON_PRETTY_PRINT) : '' ?></textarea>
</div>
</div>
</div>
</div>
<?php
echo $this->element('genericElements/assetLoader', array(

View File

@ -1,5 +1,6 @@
<?php
$modelForForm = 'Feeds';
$edit = $this->request->params['action'] === 'edit' ? true : false;
echo $this->element('genericElements/Form/genericForm', [
'data' => [
'title' => isset($edit) ? __('Edit MISP feed') : __('Add MISP feed'),
@ -62,21 +63,24 @@ echo $this->element('genericElements/Form/genericForm', [
'field' => 'distribution',
'label' => __('Distribution'),
'options' => $dropdownData['distributionLevels'],
'selected' => isset($feed['Feed']['distribution']) ? $feed['Feed']['distribution'] : 3,
'selected' => isset($entity['Feed']['distribution']) ? $entity['Feed']['distribution'] : 3,
'type' => 'dropdown'
],
[
'field' => 'tag_id',
'label' => __('Default Tag'),
'options' => $dropdownData['tags'],
'selected' => isset($feed['Feed']['tag_id']) ? $feed['Feed']['tag_id'] : '0',
'selected' => isset($entity['Feed']['tag_id']) ? $entity['Feed']['tag_id'] : '0',
'type' => 'dropdown',
'searchable' => 1
],
[
'field' => 'rules',
'label' => __('Filter rules'),
'type' => 'pullRules'
'type' => 'pullRules',
'tags' => $dropdownData['tags'],
'orgs' => $dropdownData['orgs'],
'pull_rules' => $entity['Feed']['rules']
],
],
'submit' => [