From 7d2f569663398b1ff0e59cccc512691632b1c49f Mon Sep 17 00:00:00 2001 From: Sae126V Date: Thu, 14 Sep 2023 11:09:58 +0000 Subject: [PATCH] [GT-141] Add support for pre selecting endpoints --- .../controllers/downtime/add_downtime.php | 68 ++++++++++++++++++- .../downtime/view_endpoint_tree.php | 5 ++ .../views/downtime/add_downtime.php | 59 +++++++++++++--- .../downtime/view_nested_endpoints_list.php | 55 +++++++++++---- 4 files changed, 164 insertions(+), 23 deletions(-) diff --git a/htdocs/web_portal/controllers/downtime/add_downtime.php b/htdocs/web_portal/controllers/downtime/add_downtime.php index becb0c73f..c05760798 100644 --- a/htdocs/web_portal/controllers/downtime/add_downtime.php +++ b/htdocs/web_portal/controllers/downtime/add_downtime.php @@ -25,6 +25,8 @@ require_once __DIR__.'/../utils.php'; require_once __DIR__.'/../../../web_portal/components/Get_User_Principle.php'; +use Exception; + /** * Controller for a new_downtime request. * @@ -158,6 +160,18 @@ function draw(\User $user = null) { die(json_encode(array('UTC', 0))); } + /** + * URL Mapping for `site` and `se` (Service Endpoint). + * + * If a user wants to add downtime to a specific + * `site` and `se` (Service Endpoint), the portal will + * pre-select the service endpoint corresponding to the `se` parameter + * and the endpoints of that service. + */ + elseif (isset($_GET['site']) && isset($_GET['se'])) { + displaySiteAndSeEndpoints($user); + } + // URL Mapping // If the user wants to add a downtime to a specific site, show only that site's SEs else if(isset($_GET['site'])) { @@ -210,10 +224,60 @@ function draw(\User $user = null) { throw new Exception("You don't hold a role over a NGI " . "or site with child services."); } - $params = array('ses' => $ses, 'nowUtc' => $nowUtcDateTime->format('H:i T')); + + $params = array( + 'ses' => $ses, + 'userCannotPreSelect' => true + ); + show_view("downtime/add_downtime.php", $params); die(); } } -?> \ No newline at end of file +/** + * Helper method to fetch service endpoint detail(s) associated + * with `site` provided. + * This will help portal to pre-select endpoints corresponding + * to the `se` (Service Endpoint) provided. + */ +function displaySiteAndSeEndpoints($user) +{ + $site = \Factory::getSiteService()->getSite($_GET['site']); + $ses = $site->getServices(); + + if (!hasEditPermission($site, $user)) { + throwPermissionException($site, true); + } + + $params = [ + 'ses' => $ses + ]; + + show_view("downtime/add_downtime.php", $params); + die(); +} + +// Validates if the user has edit permission for the given site. +function hasEditPermission($site, $user) +{ + return \Factory::getRoleActionAuthorisationService() + ->authoriseAction(\Action::EDIT_OBJECT, $site, $user) + ->getGrantAction(); +} + +/** + * Handles exceptions for permission-related issues. + * + * @throws Exception + */ +function throwPermissionException($resource, $isGeneric) +{ + if ($isGeneric) { + $errorMsg = "You do not have permission over $resource"; + } else { + $errorMsg = "$resource"; + } + + throw new Exception($errorMsg); +} diff --git a/htdocs/web_portal/controllers/downtime/view_endpoint_tree.php b/htdocs/web_portal/controllers/downtime/view_endpoint_tree.php index eb9f81946..9052e148f 100644 --- a/htdocs/web_portal/controllers/downtime/view_endpoint_tree.php +++ b/htdocs/web_portal/controllers/downtime/view_endpoint_tree.php @@ -23,6 +23,7 @@ function getServiceandEndpointList() { require_once __DIR__ . '/../utils.php'; require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php'; + $params = []; $dn = Get_User_Principle(); $user = \Factory::getUserService()->getUserByPrinciple($dn); $params['portalIsReadOnly'] = portalIsReadOnlyAndUserIsNotAdmin($user); @@ -30,6 +31,10 @@ function getServiceandEndpointList() { if (!isset($_REQUEST['site_id']) || !is_numeric($_REQUEST['site_id']) ){ throw new Exception("An id must be specified"); } + if (isset($_REQUEST['se'])) { + $params['se'] = $_REQUEST['se']; + } + $site = \Factory::getSiteService()->getSite($_REQUEST['site_id']); $services = $site->getServices(); $params['services'] = $services; diff --git a/htdocs/web_portal/views/downtime/add_downtime.php b/htdocs/web_portal/views/downtime/add_downtime.php index 50b255cb0..71676c823 100644 --- a/htdocs/web_portal/views/downtime/add_downtime.php +++ b/htdocs/web_portal/views/downtime/add_downtime.php @@ -156,7 +156,14 @@ class="form-control" id="Select_Sites" name="select_sites" size="10" $siteName = $site->getName(); $ngiName = $site->getNgi()->getName(); $label = xssafe($site." (".$ngiName.")"); - echo ""; + echo ""; } ?>

@@ -231,7 +238,11 @@ class="form-control" id="Select_Sites" name="select_sites" size="10" validate(); }); - + /** + * Helps us to decide whether to fetch + * and `select` all services, endpoints or NOT. + */ + getSitesServices(); }); @@ -494,14 +505,44 @@ function validateUtcDates(){ } function getSitesServices(){ - var siteId=$('#Select_Sites').val(); - if(siteId != null){ //If the user clicks on the box but not a specific row there will be no input, so catch that here - $('#chooseEndpoints').empty(); //Remove any previous content from the endpoints select list - $('#chooseServices').load('index.php?Page_Type=Downtime_view_endpoint_tree&site_id='+siteId,function( response, status, xhr ) { - if ( status == "success" ) { - validate(); + let selectedSiteID = $('#Select_Sites').val(); + + /** + * If the user clicks on the box but not a specific row + * there will be no input, so catch that here. + */ + if (selectedSiteID != null) { + // Remove any previous content from the endpoints select list. + $('#chooseEndpoints').empty(); + + let urlpathAndSearchName; + const SE_FROM_QUERY_PARAMS = new URL( + this.location.href + ).searchParams.get('se'); + + if (SE_FROM_QUERY_PARAMS) { + /** + * NOTE: If you use template literal for string interpolation, + * i.e., `${}`. It has to be in one line. + */ + urlpathAndSearchName = "index.php?" + + "Page_Type=Downtime_view_endpoint_tree" + + `&se=${SE_FROM_QUERY_PARAMS}` + + `&site_id=${selectedSiteID}`; + } else { + urlpathAndSearchName = "index.php?" + + "Page_Type=Downtime_view_endpoint_tree" + + `&site_id=${selectedSiteID}`; + } + + $('#chooseServices').load( + urlpathAndSearchName, + function(response, status, xhr) { + if ( status == "success" ) { + validate(); + } } - }); + ); } } diff --git a/htdocs/web_portal/views/downtime/view_nested_endpoints_list.php b/htdocs/web_portal/views/downtime/view_nested_endpoints_list.php index bef7dcfe5..e9dba3cd2 100644 --- a/htdocs/web_portal/views/downtime/view_nested_endpoints_list.php +++ b/htdocs/web_portal/views/downtime/view_nested_endpoints_list.php @@ -1,26 +1,57 @@