From 2ec0eef44012151aa88b02c859803694eca09387 Mon Sep 17 00:00:00 2001 From: Monviech <79600909+Monviech@users.noreply.github.com> Date: Fri, 26 Apr 2024 09:41:17 +0200 Subject: [PATCH] www/caddy: Selectpicker that can filter the Domain, Subdomain and Handlers by selected Domain (#3937) * Update reverse_proxy.volt Add a first version of a filter functionality by domain. In this version, only handlers are filtered by domain. A selectpicker with multi selection can choose domains, and the filter function compares these UUIDs to the UUIDs of the "reverse" UUIDs of the model relation fields. Either all domains are shown, or only elements where the UUIDs match. * Update ReverseProxyController.php A new api endpoint for the domain search selectpicker has been created. It returns the ID and a Domain+Port combination. The search function for the Handler now returns all fields if no filter has been set, or only the referenced UUIDs when a filter has been set. * Update ReverseProxyController.php - Add search function to subdomains * Update reverse_proxy.volt - Reference Search Filter in Handlers, Domains and Subdomains * Update ReverseProxyController.php - Add the search function to domains * Update ReverseProxyController.php A little bit of cleanup. $add_empty was unused so it's removed. * Update reverse_proxy.volt - Changed margin to align selectpicker with other options. * Small margin fix to align Selectpicker with other options * Update reverse_proxy.volt - Restrict style to the ID of only the affected selectpicker --- .../Caddy/Api/ReverseProxyController.php | 83 ++++++++++++++--- .../views/OPNsense/Caddy/reverse_proxy.volt | 89 +++++++++++++++++++ 2 files changed, 162 insertions(+), 10 deletions(-) diff --git a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ReverseProxyController.php b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ReverseProxyController.php index 4dcd01ce6..eb94b82e0 100644 --- a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ReverseProxyController.php +++ b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ReverseProxyController.php @@ -41,9 +41,22 @@ class ReverseProxyController extends ApiMutableModelControllerBase /*ReverseProxy Section*/ - public function searchReverseProxyAction($add_empty = '0') + /*Search Function adjusted for the search filter dropdown*/ + public function searchReverseProxyAction() { - return $this->searchBase("reverseproxy.reverse", null, 'description'); + // Get a comma-separated list of UUIDs from the request + $reverseUuids = $this->request->get('reverseUuids'); + $uuidArray = !empty($reverseUuids) ? explode(',', $reverseUuids) : []; + + // Define the filter function to handle multiple UUIDs + $filterFunction = function ($modelItem) use ($uuidArray) { + $itemUuid = (string)$modelItem->getAttributes()['uuid']; + // Include the item if no UUIDs are provided (empty array) or if it's in the array of UUIDs + return empty($uuidArray) || in_array($itemUuid, $uuidArray, true); + }; + + // Return the search results filtered by the provided UUIDs, if any + return $this->searchBase("reverseproxy.reverse", null, 'description', $filterFunction); } public function setReverseProxyAction($uuid) @@ -70,13 +83,48 @@ class ReverseProxyController extends ApiMutableModelControllerBase { return $this->toggleBase("reverseproxy.reverse", $uuid, $enabled); } + + /*Function for the search filter dropdown in the bootgrid*/ + public function getAllReverseDomainsAction() + { + $this->sessionClose(); // Close session early for performance + $result = array("rows" => array()); + + $mdlCaddy = new \OPNsense\Caddy\Caddy(); + $reverseNodes = $mdlCaddy->reverseproxy->reverse->iterateItems(); + + foreach ($reverseNodes as $item) { + if (!empty($item->FromDomain)) { + // Conditionally concatenate port if it exists + $domain = (string)$item->FromDomain; + $port = (string)$item->FromPort; + $combinedDomainPort = $domain . (!empty($port) ? ':' . $port : ''); + + $result['rows'][] = array( + 'id' => (string)$item->getAttributes()['uuid'], + 'domainPort' => $combinedDomainPort // Combined domain and port, conditionally adding port + ); + } + } + + return $result; + } /*Subdomain Section*/ - public function searchSubdomainAction($add_empty = '0') + /*Search Function adjusted for the search filter dropdown*/ + public function searchSubdomainAction() { - return $this->searchBase("reverseproxy.subdomain", null, 'description'); + $reverseUuids = $this->request->get('reverseUuids'); + $uuidArray = !empty($reverseUuids) ? explode(',', $reverseUuids) : []; + + $filterFunction = function ($modelItem) use ($uuidArray) { + // Filtering on domain UUIDs referenced by subdomains + return empty($uuidArray) || in_array((string)$modelItem->reverse, $uuidArray, true); + }; + + return $this->searchBase("reverseproxy.subdomain", null, 'description', $filterFunction); } public function setSubdomainAction($uuid) @@ -106,10 +154,25 @@ class ReverseProxyController extends ApiMutableModelControllerBase /*Handler Section*/ - - public function searchHandleAction($add_empty = '0') + + /*Search Function adjusted for the search filter dropdown*/ + public function searchHandleAction() { - return $this->searchBase("reverseproxy.handle", null, 'description'); + $reverseUuids = $this->request->get('reverseUuids'); + $uuidArray = explode(',', $reverseUuids); + + if (empty($reverseUuids)) { + // If no UUIDs are provided, do not apply any filter, return all records + return $this->searchBase("reverseproxy.handle", null, 'description'); + } else { + // Apply the filter only if UUIDs are provided + $filterFunction = function ($modelItem) use ($uuidArray) { + $modelUUID = (string)$modelItem->reverse; + return in_array($modelUUID, $uuidArray, true); + }; + + return $this->searchBase("reverseproxy.handle", null, 'description', $filterFunction); + } } public function setHandleAction($uuid) @@ -140,7 +203,7 @@ class ReverseProxyController extends ApiMutableModelControllerBase /* AccessList Section */ - public function searchAccessListAction($add_empty = '0') + public function searchAccessListAction() { return $this->searchBase("reverseproxy.accesslist", null, 'description'); } @@ -168,7 +231,7 @@ class ReverseProxyController extends ApiMutableModelControllerBase /* BasicAuth Section */ - public function searchBasicAuthAction($add_empty = '0') + public function searchBasicAuthAction() { return $this->searchBase("reverseproxy.basicauth", null, 'description'); } @@ -216,7 +279,7 @@ class ReverseProxyController extends ApiMutableModelControllerBase /* Header Section */ - public function searchHeaderAction($add_empty = '0') + public function searchHeaderAction() { return $this->searchBase("reverseproxy.header", null, 'description'); } diff --git a/www/caddy/src/opnsense/mvc/app/views/OPNsense/Caddy/reverse_proxy.volt b/www/caddy/src/opnsense/mvc/app/views/OPNsense/Caddy/reverse_proxy.volt index 5d9ebc406..ed93146a1 100644 --- a/www/caddy/src/opnsense/mvc/app/views/OPNsense/Caddy/reverse_proxy.volt +++ b/www/caddy/src/opnsense/mvc/app/views/OPNsense/Caddy/reverse_proxy.volt @@ -26,6 +26,17 @@ + +