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
2020-09-01 15:59:38 +02:00
class Allowedlist extends AppModel
2018-07-19 11:48:22 +02:00
{
2020-09-01 15:59:38 +02:00
public $useTable = 'allowedlist' ;
2012-09-18 15:30:32 +02:00
2018-07-19 11:48:22 +02:00
public $displayField = 'name' ;
2012-09-18 15:30:32 +02:00
2018-07-19 11:48:22 +02:00
public $actsAs = array (
2021-01-22 13:01:23 +01:00
'AuditLog' ,
2018-07-19 11:48:22 +02:00
'Trim' ,
2019-02-10 13:08:12 +01:00
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
2018-07-19 11:48:22 +02:00
'roleModel' => 'Role' ,
'roleKey' => 'role_id' ,
'change' => 'full'
),
);
2012-09-18 15:30:32 +02:00
2020-09-01 15:59:38 +02:00
public $allowedlistedItems = false ;
2018-08-31 13:16:44 +02:00
2018-07-19 11:48:22 +02:00
public $validate = array (
'name' => array (
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
),
'userdefined' => array (
'rule' => array ( 'validateValue' ),
2020-09-01 15:59:38 +02:00
'message' => 'Name not in the right format. Allowedlist 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,
2018-07-19 11:48:22 +02:00
//'allowEmpty' => false,
//'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'unique' => array (
'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-11-29 09:35:57 +01:00
2018-07-19 11:48:22 +02:00
// regexp validation
public function validateValue ( $fields )
{
if ( preg_match ( $fields [ 'name' ], 'test' ) === false ) {
return false ;
}
return true ;
}
2012-08-06 10:42:46 +02:00
2018-07-19 11:48:22 +02:00
public function valueIsUnique ( $fields )
{
$value = $fields [ 'name' ];
2012-08-06 10:42:46 +02:00
2020-09-01 15:59:38 +02:00
$allowedlist = $this -> find ( 'all' , array ( 'recursive' => 0 , 'fields' => 'name' ));
foreach ( $allowedlist as $allowedlistItem ) {
if ( $value == $allowedlistItem [ 'Allowedlist' ][ 'name' ]) {
2018-07-19 11:48:22 +02:00
return false ;
}
}
2012-09-18 15:30:32 +02:00
2018-07-19 11:48:22 +02:00
return true ;
}
2012-08-06 10:42:46 +02:00
2018-07-19 11:48:22 +02:00
public function getBlockedValues ()
{
2020-09-01 15:59:38 +02:00
if ( $this -> allowedlistedItems === false ) {
$Allowedlists = $this -> find ( 'all' , array ( 'fields' => array ( 'name' )));
$this -> allowedlistedItems = array ();
foreach ( $Allowedlists as $item ) {
$this -> allowedlistedItems [] = $item [ 'Allowedlist' ][ 'name' ];
2018-11-23 14:11:33 +01:00
}
}
2020-09-01 15:59:38 +02:00
return $this -> allowedlistedItems ;
2018-07-19 11:48:22 +02:00
}
2012-09-18 15:30:32 +02:00
2020-09-01 15:59:38 +02:00
public function removeAllowedlistedFromArray ( $data , $isAttributeArray )
2018-07-19 11:48:22 +02:00
{
2020-09-01 15:59:38 +02:00
// Let's get all of the values that will be blocked by the allowedlist
$allowedlists = $this -> getBlockedValues ();
// if we don't have any allowedlist items in the db, don't loop through each attribute
if ( ! empty ( $allowedlists )) {
2018-07-19 11:48:22 +02:00
// if $isAttributeArray, we know that we have just an array of attributes
if ( $isAttributeArray ) {
2020-09-01 15:59:38 +02:00
// loop through each attribute and unset the ones that are allowedlisted
2018-07-19 11:48:22 +02:00
foreach ( $data as $k => $attribute ) {
2024-01-19 18:11:28 +01:00
if ( empty ( $attribute [ 'Attribute' ])) {
$attribute = [ 'Attribute' => $attribute ];
}
2020-09-01 15:59:38 +02:00
// loop through each allowedlist item and run a preg match against the attribute value. If it matches, unset the attribute
foreach ( $allowedlists as $wlitem ) {
2018-07-19 11:48:22 +02:00
if ( preg_match ( $wlitem , $attribute [ 'Attribute' ][ 'value' ])) {
unset ( $data [ $k ]);
}
}
}
$data = array_values ( $data );
} else {
// if !$isAttributeArray, we know that we have an array of events that we need to parse through
foreach ( $data as $ke => $event ) {
if ( isset ( $event [ 'Attribute' ])) {
2020-09-01 15:59:38 +02:00
// loop through each attribute and unset the ones that are allowedlisted
2018-07-19 11:48:22 +02:00
foreach ( $event [ 'Attribute' ] as $k => $attribute ) {
2020-09-01 15:59:38 +02:00
// loop through each allowedlist item and run a preg match against the attribute value. If it matches, unset the attribute
foreach ( $allowedlists as $wlitem ) {
2018-07-19 11:48:22 +02:00
if ( preg_match ( $wlitem , $attribute [ 'value' ])) {
unset ( $data [ $ke ][ 'Attribute' ][ $k ]);
}
}
}
$data [ $ke ][ 'Attribute' ] = array_values ( $data [ $ke ][ 'Attribute' ]);
}
}
}
}
return $data ;
}
2013-06-26 15:31:28 +02:00
2020-09-01 15:59:38 +02:00
// A simplified allowedlist removal, for when we just want to throw values against the list instead of attributes / events
public function removeAllowedlistedValuesFromArray ( $data )
2018-07-19 11:48:22 +02:00
{
2020-09-01 15:59:38 +02:00
$allowedlists = $this -> getBlockedValues ();
// if we don't have any allowedlist items in the db, don't loop through each attribute
if ( ! empty ( $allowedlists )) {
2018-07-19 11:48:22 +02:00
foreach ( $data as $k => $value ) {
2020-09-01 15:59:38 +02:00
foreach ( $allowedlists as $wlitem ) {
2018-07-19 11:48:22 +02:00
if ( preg_match ( $wlitem , $value )) {
unset ( $data [ $k ]);
}
}
}
$data = array_values ( $data );
}
return $data ;
}
2012-08-06 10:42:46 +02:00
}