From 301565b449296c7fa809efff44004900b49f5480 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 13 May 2018 20:15:48 +0200 Subject: [PATCH] Firewall/Alias, API work in progress https://github.com/opnsense/core/issues/1858 add grid and empty model --- .../OPNsense/Firewall/AliasController.php | 45 +++++++ .../OPNsense/Firewall/Api/AliasController.php | 117 ++++++++++++++++++ .../Firewall/forms/dialogEditAlias.xml | 20 +++ .../app/models/OPNsense/Firewall/Alias.php | 40 ++++++ .../app/models/OPNsense/Firewall/Alias.xml | 31 +++++ .../Firewall/FieldTypes/AliasNameField.php | 78 ++++++++++++ .../app/views/OPNsense/Firewall/alias.volt | 49 ++++++++ 7 files changed, 380 insertions(+) create mode 100644 src/opnsense/mvc/app/controllers/OPNsense/Firewall/AliasController.php create mode 100644 src/opnsense/mvc/app/controllers/OPNsense/Firewall/Api/AliasController.php create mode 100644 src/opnsense/mvc/app/controllers/OPNsense/Firewall/forms/dialogEditAlias.xml create mode 100644 src/opnsense/mvc/app/models/OPNsense/Firewall/Alias.php create mode 100644 src/opnsense/mvc/app/models/OPNsense/Firewall/Alias.xml create mode 100644 src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/AliasNameField.php create mode 100644 src/opnsense/mvc/app/views/OPNsense/Firewall/alias.volt diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Firewall/AliasController.php b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/AliasController.php new file mode 100644 index 000000000..c4ff5df52 --- /dev/null +++ b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/AliasController.php @@ -0,0 +1,45 @@ +view->formDialogAlias = $this->getForm("dialogEditAlias"); + $this->view->pick('OPNsense/Firewall/alias'); + } +} diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Firewall/Api/AliasController.php b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/Api/AliasController.php new file mode 100644 index 000000000..051c85275 --- /dev/null +++ b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/Api/AliasController.php @@ -0,0 +1,117 @@ +searchBase( + "aliases.alias", + array('enabled', 'name', 'description'), + "description" + ); + } + + /** + * Update alias with given properties + * @param string $uuid internal id + * @return array save result + validation output + * @throws \Phalcon\Validation\Exception when field validations fail + * @throws \ReflectionException when not bound to model + */ + public function setItemAction($uuid) + { + return $this->setBase("alias", "aliases.alias", $uuid); + } + + /** + * Add new alias and set with attributes from post + * @return array save result + validation output + * @throws \OPNsense\Base\ModelException when not bound to model + * @throws \Phalcon\Validation\Exception when field validations fail + * @throws \ReflectionException + */ + public function addItemAction() + { + return $this->addBase("alias", "aliases.alias"); + } + + /** + * Retrieve alias settings or return defaults for new one + * @param $uuid item unique id + * @return array alias content + * @throws \ReflectionException when not bound to model + */ + public function getItemAction($uuid = null) + { + return $this->getBase("alias", "aliases.alias", $uuid); + } + + /** + * Delete alias by uuid, save contents to tmp for removal on apply + * @param string $uuid internal id + * @return array save status + * @throws \Phalcon\Validation\Exception when field validations fail + * @throws \ReflectionException when not bound to model + */ + public function delItemAction($uuid) + { + return $this->delBase("aliases.alias", $uuid); + } + + /** + * toggle status + * @param string $uuid id to toggled + * @param string|null $disabled set disabled by default + * @return array status + * @throws \Phalcon\Validation\Exception when field validations fail + * @throws \ReflectionException when not bound to model + */ + public function toggleItemAction($uuid, $disabled = null) + { + return $this->toggleBase("aliases.aliases", $uuid); + } +} diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Firewall/forms/dialogEditAlias.xml b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/forms/dialogEditAlias.xml new file mode 100644 index 000000000..fbb26d033 --- /dev/null +++ b/src/opnsense/mvc/app/controllers/OPNsense/Firewall/forms/dialogEditAlias.xml @@ -0,0 +1,20 @@ +
+ + alias.enabled + + checkbox + enable this alias + + + alias.name + + text + The name of the alias may only consist of the characters "a-z, A-Z, 0-9 and _". Aliases can be nested using this name. + + + alias.description + + You may enter a description here for your reference (not parsed). + text + +
\ No newline at end of file diff --git a/src/opnsense/mvc/app/models/OPNsense/Firewall/Alias.php b/src/opnsense/mvc/app/models/OPNsense/Firewall/Alias.php new file mode 100644 index 000000000..004b2dcc2 --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Firewall/Alias.php @@ -0,0 +1,40 @@ + + //OPNSense/Firewall/Alias + 0.0.1 + + OPNsense central management + + + + + + 1 + Y + + + Y + + + An alias with this name already exists. + UniqueConstraint + + + + + Y + /^([\t\n\v\f\r 0-9a-zA-Z.\-,_\x{00A0}-\x{FFFF}]){1,255}$/u + Description should be a string between 1 and 255 characters + + + + + diff --git a/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/AliasNameField.php b/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/AliasNameField.php new file mode 100644 index 000000000..e052474db --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/AliasNameField.php @@ -0,0 +1,78 @@ +internalValue != null) { + $validators[] = new ExclusionIn(array( + 'message' => sprintf(gettext('The name cannot be the internally reserved keyword "%s".'), + (string)$this), + 'domain' => $reservedwords) + ); + +// $validators[] = new Regex(array( +// 'message' => sprintf(gettext( +// 'The name must be less than 32 characters long and may only consist of the following characters: %s' +// ), 'a-z, A-Z, 0-9, _'), +// 'pattern'=>'/(^_*$|^\d*$|[^a-z0-9_]){1,32}/') +// ); + + } + return $validators; + } +} diff --git a/src/opnsense/mvc/app/views/OPNsense/Firewall/alias.volt b/src/opnsense/mvc/app/views/OPNsense/Firewall/alias.volt new file mode 100644 index 000000000..1aab441da --- /dev/null +++ b/src/opnsense/mvc/app/views/OPNsense/Firewall/alias.volt @@ -0,0 +1,49 @@ + + +
+
+
+
+
+ + + + + + + + + + + + + + + + + + +
{{ lang._('ID') }}{{ lang._('Enabled') }}{{ lang._('Name') }}{{ lang._('Description') }}{{ lang._('Commands') }}
+ + +
+
+
+
+
+
+ +{# include dialogs #} +{{ partial("layout_partials/base_dialog",['fields':formDialogAlias,'id':'DialogAlias','label':lang._('Edit Alias')])}}