Firewall / Aliases - cleanup AliasField to use new getStaticChildren()

This commit is contained in:
Ad Schellevis 2023-07-03 21:38:31 +02:00
parent 07524f0b73
commit b8cef701e2

View File

@ -38,24 +38,31 @@ use OPNsense\Core\Config;
class AliasField extends ArrayField
{
private static $reservedItems = [];
private static $current_stats = null;
public function __construct($ref = null, $tagname = null)
/**
* {@inheritdoc}
*/
protected static $internalStaticChildren = [];
/**
* {@inheritdoc}
*/
protected static function getStaticChildren()
{
$result = [];
foreach (glob(__DIR__ . "/../static_aliases/*.json") as $filename) {
$payload = json_decode(file_get_contents($filename), true);
if (is_array($payload)) {
foreach ($payload as $aliasname => $content) {
self::$reservedItems[$aliasname] = $content;
$result[$aliasname] = $content;
}
}
}
foreach (Config::getInstance()->object()->interfaces->children() as $k => $n) {
$table_name = sprintf("__%s_network", $k);
$table_desc = !empty((string)$n->descr) ? (string)$n->descr : $k;
self::$reservedItems[$table_name] = [
$result[$table_name] = [
"enabled" => "1",
"name" => $table_name,
"type" => "internal",
@ -63,26 +70,12 @@ class AliasField extends ArrayField
"content" => ""
];
}
parent::__construct($ref, $tagname);
}
private function addStatsFields($node)
{
// generate new unattached fields, which are only usable to read data from (not synched to config.xml)
$current_items = new IntegerField();
$current_items->setInternalIsVirtual();
$last_updated = new TextField();
$last_updated->setInternalIsVirtual();
if (!empty((string)$node->name) && !empty(self::$current_stats[(string)$node->name])) {
$current_items->setValue(self::$current_stats[(string)$node->name]['count']);
$last_updated->setValue(self::$current_stats[(string)$node->name]['updated']);
}
$node->addChildNode('current_items', $current_items);
$node->addChildNode('last_updated', $last_updated);
return $result;
}
protected function actionPostLoadingEvent()
{
parent::actionPostLoadingEvent();
if (self::$current_stats === null) {
self::$current_stats = [];
$stats = json_decode((new Backend())->configdRun('filter diag table_size'), true);
@ -90,73 +83,17 @@ class AliasField extends ArrayField
self::$current_stats = $stats['details'];
}
}
foreach ($this->internalChildnodes as $node) {
if (!$node->getInternalIsVirtual()) {
$this->addStatsFields($node);
foreach ($this->iterateItems() as $node) {
$current_items = new IntegerField();
$current_items->setInternalIsVirtual();
$last_updated = new TextField();
$last_updated->setInternalIsVirtual();
if (!empty((string)$node->name) && !empty(self::$current_stats[(string)$node->name])) {
$current_items->setValue(self::$current_stats[(string)$node->name]['count']);
$last_updated->setValue(self::$current_stats[(string)$node->name]['updated']);
}
}
return parent::actionPostLoadingEvent();
}
/**
* create virtual alias nodes
*/
private function createReservedNodes()
{
$result = [];
foreach (self::$reservedItems as $aliasName => $aliasContent) {
$container_node = $this->newContainerField($this->__reference . "." . $aliasName, $this->internalXMLTagName);
$container_node->setAttributeValue("uuid", $aliasName);
$container_node->setInternalIsVirtual();
foreach ($this->getTemplateNode()->iterateItems() as $key => $value) {
$node = clone $value;
$node->setInternalReference($container_node->__reference . "." . $key);
if (isset($aliasContent[$key])) {
$node->setValue($aliasContent[$key]);
}
$node->markUnchanged();
$container_node->addChildNode($key, $node);
}
$this->addStatsFields($container_node);
$result[$aliasName] = $container_node;
}
return $result;
}
/**
* @inheritdoc
*/
public function hasChild($name)
{
if (isset(self::$reservedItems[$name])) {
return true;
} else {
return parent::hasChild($name);
}
}
/**
* @inheritdoc
*/
public function getChild($name)
{
if (isset(self::$reservedItems[$name])) {
return $this->createReservedNodes()[$name];
} else {
return parent::getChild($name);
}
}
/**
* @inheritdoc
*/
public function iterateItems()
{
foreach (parent::iterateItems() as $key => $value) {
yield $key => $value;
}
foreach ($this->createReservedNodes() as $key => $node) {
yield $key => $node;
$node->addChildNode('current_items', $current_items);
$node->addChildNode('last_updated', $last_updated);
}
}
}