2012-08-06 10:42:46 +02:00
< ? php
2013-01-04 16:48:46 +01:00
2012-08-06 10:42:46 +02:00
App :: uses ( 'AppModel' , 'Model' );
2013-01-04 16:48:46 +01:00
2012-08-06 10:42:46 +02:00
/**
* Whitelist Model
*
*/
class Whitelist extends AppModel {
2012-09-18 15:30:32 +02:00
2012-08-06 10:42:46 +02:00
/**
* Use table
*
* @ var mixed False or table name
*/
public $useTable = 'whitelist' ;
2012-09-18 15:30:32 +02:00
2012-08-06 10:42:46 +02:00
/**
* Display field
*
* @ var string
*/
public $displayField = 'name' ;
2012-09-18 15:30:32 +02:00
2013-03-07 11:51:43 +01:00
public $actsAs = array (
'Trim' ,
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
'roleModel' => 'Role' ,
'roleKey' => 'role_id' ,
'change' => 'full'
),
);
2012-11-29 09:35:57 +01:00
2012-08-06 10:42:46 +02:00
/**
* Validation rules
*
* @ var array
*/
public $validate = array (
'name' => array (
2015-10-09 15:59:25 +02:00
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
2012-08-06 10:42:46 +02:00
),
'userdefined' => array (
'rule' => array ( 'validateValue' ),
2016-02-05 08:30:02 +01:00
'message' => 'Name not in the right format. Whitelist entries have to be enclosed by a valid php delimiter (which can be most non-alphanumeric / non-whitespace character). Format: "/8.8.8.8/" Please double check the name.' , //'allowEmpty' => false,
2012-08-06 10:42:46 +02:00
//'allowEmpty' => false,
//'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'unique' => array (
2012-09-18 15:30:32 +02:00
'rule' => 'isUnique' , //array('valueIsUnique'),
'message' => 'A similar name already exists.' ,
//'allowEmpty' => false,
//'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
2012-08-06 10:42:46 +02:00
),
),
);
2013-06-26 14:48:25 +02:00
// regexp validation
2012-09-18 15:30:32 +02:00
public function validateValue ( $fields ) {
2013-12-20 11:17:41 +01:00
if ( preg_match ( $fields [ 'name' ], 'test' ) === false ) return false ;
2012-09-18 15:30:32 +02:00
return true ;
2012-08-06 10:42:46 +02:00
}
2012-09-18 15:30:32 +02:00
public function valueIsUnique ( $fields ) {
$value = $fields [ 'name' ];
$whitelist = $this -> find ( 'all' , array ( 'recursive' => 0 , 'fields' => 'name' ));
foreach ( $whitelist as $whitelistItem ) {
if ( $value == $whitelistItem [ 'Whitelist' ][ 'name' ]) {
return false ;
}
}
2012-08-06 10:42:46 +02:00
2012-09-18 15:30:32 +02:00
return true ;
2012-08-06 10:42:46 +02:00
}
2012-09-18 15:30:32 +02:00
2013-06-26 14:48:25 +02:00
public function getBlockedValues () {
$Whitelists = $this -> find ( 'all' , array ( 'fields' => array ( 'name' )));
$toReturn = array ();
foreach ( $Whitelists as $item ) {
$toReturn [] = $item [ 'Whitelist' ][ 'name' ];
}
return $toReturn ;
}
2013-06-26 15:31:28 +02:00
2013-06-26 16:30:37 +02:00
public function removeWhitelistedFromArray ( $data , $isAttributeArray ) {
2013-06-26 15:31:28 +02:00
// Let's get all of the values that will be blocked by the whitelist
$whitelists = $this -> getBlockedValues ();
// if we don't have any whitelist items in the db, don't loop through each attribute
if ( ! empty ( $whitelists )) {
2013-06-26 16:30:37 +02:00
// if $isAttributeArray, we know that we have just an array of attributes
if ( $isAttributeArray ) {
// loop through each attribute and unset the ones that are whitelisted
foreach ( $data as $k => $attribute ) {
// loop through each whitelist item and run a preg match against the attribute value. If it matches, unset the attribute
foreach ( $whitelists as $wlitem ) {
2013-12-20 11:17:41 +01:00
if ( preg_match ( $wlitem , $attribute [ 'Attribute' ][ 'value' ])) {
2013-06-26 16:30:37 +02:00
unset ( $data [ $k ]);
}
}
}
} else {
// if !$isAttributeArray, we know that we have an array of events that we need to parse through
foreach ( $data as $ke => $event ) {
// loop through each attribute and unset the ones that are whitelisted
foreach ( $event [ 'Attribute' ] as $k => $attribute ) {
// loop through each whitelist item and run a preg match against the attribute value. If it matches, unset the attribute
foreach ( $whitelists as $wlitem ) {
2013-12-20 11:17:41 +01:00
if ( preg_match ( $wlitem , $attribute [ 'value' ])) {
2013-06-26 16:30:37 +02:00
unset ( $data [ $ke ][ 'Attribute' ][ $k ]);
}
}
2013-06-26 15:31:28 +02:00
}
}
}
}
2013-06-26 16:30:37 +02:00
return $data ;
2013-06-26 15:31:28 +02:00
}
2015-05-15 14:58:53 +02:00
// A simplified whitelist removal, for when we just want to throw values against the list instead of attributes / events
public function removeWhitelistedValuesFromArray ( $data ) {
$whitelists = $this -> getBlockedValues ();
// if we don't have any whitelist items in the db, don't loop through each attribute
if ( ! empty ( $whitelists )) {
foreach ( $data as $k => $value ) {
foreach ( $whitelists as $wlitem ) {
if ( preg_match ( $wlitem , $value )) {
unset ( $data [ $k ]);
}
}
}
}
return $data ;
}
2012-08-06 10:42:46 +02:00
}