From 98a2df02806fef753ad145db629959f72c0da8a3 Mon Sep 17 00:00:00 2001 From: noud Date: Tue, 4 Dec 2012 08:51:27 +0100 Subject: [PATCH] Import Whitelist if Import Whitelist item has regex and no replacement, then do not allow an attribute having value the regex and do not allow events having info conform that regex. --- app/Controller/RegexController.php | 63 ++++++++++++++++++- app/Model/Behavior/RegexBehavior.php | 4 +- app/Model/Regex.php | 2 + .../Model/Behavior/SysLogLogableBehavior.php | 2 + app/View/Regex/admin_add.ctp | 2 +- app/View/Regex/admin_edit.ctp | 2 +- app/View/Regex/admin_index.ctp | 3 +- 7 files changed, 72 insertions(+), 6 deletions(-) diff --git a/app/Controller/RegexController.php b/app/Controller/RegexController.php index a487d5109..778b5a7e9 100644 --- a/app/Controller/RegexController.php +++ b/app/Controller/RegexController.php @@ -109,4 +109,65 @@ class RegexController extends AppController { $this->Session->setFlash(__('Regex was not deleted')); $this->redirect(array('action' => 'index')); } -} + +/** + * + */ + public function admin_clean() { + // Attributes.value + $deletableAttributes = array(); + $this->loadModel('Attribute'); + $attributes = $this->Attribute->find('all', array('recursive' => 0)); + foreach ($attributes as $attribute) { + $result = $this->replaceSpecific(&$attribute['Attribute']['value']); + if (!$result) { + $deletableAttributes[] = $attribute['Attribute']['id']; + } else { + $this->Attribute->save($attribute); + } + } + if (count($deletableAttributes)) { + foreach ($deletableAttributes as $event) { + $this->Attribute->delete($event); + } + } + + // Event.info + $deletableEvents = array(); + $this->loadModel('Event'); + $events = $this->Event->find('all', array('recursive' => 0)); + foreach ($events as $event) { + $result = $this->replaceSpecific(&$event['Event']['info']); + if (!$result) { + $deletableEvents[] = $event['Event']['id']; + } else { + $this->Event->save($event); + } + } + if (count($deletableEvents)) { + foreach ($deletableEvents as $event) { + $this->Event->delete($event); + } + } + + $this->redirect(array('action' => 'index')); + } + + public function replaceSpecific($origString) { + $returnValue = true; + $regex = new Regex(); + $allRegex = $regex->getAll(); + foreach($allRegex as $regex) { + if (strlen($regex['Regex']['replacement'])) { + $origString = preg_replace($regex['Regex']['regex'], $regex['Regex']['replacement'], $origString); + } + if (!strlen($regex['Regex']['replacement']) && preg_match($regex['Regex']['regex'], $origString)) { + App::uses('SessionComponent', 'Controller/Component'); + SessionComponent::setFlash('Blacklisted value!'); + $returnValue = false; + } + } + return $returnValue; + } + +} \ No newline at end of file diff --git a/app/Model/Behavior/RegexBehavior.php b/app/Model/Behavior/RegexBehavior.php index f95d38946..bb1ea34df 100644 --- a/app/Model/Behavior/RegexBehavior.php +++ b/app/Model/Behavior/RegexBehavior.php @@ -47,7 +47,7 @@ class RegexBehavior extends ModelBehavior { $returnValue = true; foreach ($Model->data[$Model->name] as $key => &$field) { if (in_array($key, $this->settings[$Model->alias]['fields']) && is_string($field)) { - $returnValue = $this->replaceWindowsSpecific($field); + $returnValue = $this->replaceWindowsSpecific($Model, &$field); // if (!$returnValue) { // $Model->blacklistErrors[] = array($key, $field); // } @@ -63,7 +63,7 @@ class RegexBehavior extends ModelBehavior { * * @return string */ - public function replaceWindowsSpecific(&$string) { + public function replaceWindowsSpecific(Model $Model, $string) { $returnValue = true; $regex = new Regex(); $allRegex = $regex->getAll(); diff --git a/app/Model/Regex.php b/app/Model/Regex.php index d48113d81..3aaf132d6 100644 --- a/app/Model/Regex.php +++ b/app/Model/Regex.php @@ -13,6 +13,8 @@ class Regex extends AppModel { */ public $useTable = 'regex'; + public $actsAs = array('Regex' => array('fields' => array('info', 'value'))); + public function getAll() { return $this->find('all'); } diff --git a/app/Plugin/SysLogLogable/Model/Behavior/SysLogLogableBehavior.php b/app/Plugin/SysLogLogable/Model/Behavior/SysLogLogableBehavior.php index c79c7cc3d..0eabcfe0a 100644 --- a/app/Plugin/SysLogLogable/Model/Behavior/SysLogLogableBehavior.php +++ b/app/Plugin/SysLogLogable/Model/Behavior/SysLogLogableBehavior.php @@ -183,6 +183,8 @@ class SysLogLogableBehavior extends LogableBehavior { $title = 'User ('. $Model->data[$Model->alias]['id'].') '. $Model->data[$Model->alias]['email']; break; case "Event": + App::uses('EventsController', 'Controller'); + App::build(array('Controller' => array(APP . DS . 'Controller'), 'EventsController')); $this->Events = new EventsController(); $this->Events->constructClasses(); $title = 'Event ('. $Model->data[$Model->alias]['id'].'): '.$this->Events->getName($Model->data[$Model->alias]['id']); diff --git a/app/View/Regex/admin_add.ctp b/app/View/Regex/admin_add.ctp index fce11b7b0..4c8b1a50e 100644 --- a/app/View/Regex/admin_add.ctp +++ b/app/View/Regex/admin_add.ctp @@ -1,7 +1,7 @@
Form->create('Regex');?>
- + Form->input('regex'); echo $this->Form->input('replacement'); diff --git a/app/View/Regex/admin_edit.ctp b/app/View/Regex/admin_edit.ctp index d74695f23..e209214f1 100644 --- a/app/View/Regex/admin_edit.ctp +++ b/app/View/Regex/admin_edit.ctp @@ -1,7 +1,7 @@
Form->create('Regex');?>
- + Form->input('id'); echo $this->Form->input('regex'); diff --git a/app/View/Regex/admin_index.ctp b/app/View/Regex/admin_index.ctp index 2dba46d6a..dd3060005 100644 --- a/app/View/Regex/admin_index.ctp +++ b/app/View/Regex/admin_index.ctp @@ -1,5 +1,5 @@
-

+

@@ -37,6 +37,7 @@
    +
  • Html->link(__('Perform on existing data'), array('admin' => true, 'action' => 'clean')); ?>
  • Html->link(__('New Regex'), array('admin' => true, 'action' => 'add')); ?>
  •  
  • element('actions_menu'); ?>
Paginator->sort('id');?>