Skip to content

Commit

Permalink
[GT-183] Add service types page for non-admins
Browse files Browse the repository at this point in the history
  • Loading branch information
Sae126V committed Aug 31, 2023
1 parent a8d8285 commit c1e5ab8
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 101 deletions.
6 changes: 6 additions & 0 deletions config/web_portal/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<link>index.php?Page_Type=View_Role_Action_Mappings</link>
</RoleActionMappings>

<ViewServiceTypes>
<show_on_instance>write_enabled</show_on_instance>
<name>Service Types</name>
<link>index.php?Page_Type=Service_Types</link>
</ViewServiceTypes>

<spacer>
<spacer>Add</spacer>
<show_on_instance>write_enabled</show_on_instance>
Expand Down
1 change: 1 addition & 0 deletions config/web_portal/menu.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<xs:element name="Services" type="showType" minOccurs="0"/>
<xs:element name="Scopes" type="showType" minOccurs="0"/>
<xs:element name="RoleActionMappings" type="showType" minOccurs="0"/>
<xs:element name="ViewServiceTypes" type="showType" minOccurs="0"/>
<xs:element name="AddSite" type="showType" minOccurs="0"/>
<xs:element name="AddServiceGroup" type="showType" minOccurs="0"/>
<xs:element name="AddService" type="showType" minOccurs="0"/>
Expand Down
162 changes: 99 additions & 63 deletions htdocs/web_portal/components/Draw_Components/menu.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

/*______________________________________________________
*======================================================
* File: menu.php
* Owner: STFC - GOCDB developers
* Author: John Casson, George Ryall, David Meredith
* Description: Draws the left hand menu bar.
*
Expand All @@ -21,92 +23,126 @@
/*====================================================== */


if(isset($_REQUEST['menu']))
if (isset($_REQUEST['menu'])) {
draw_menu($_REQUEST['menu']);
}


function draw_menu($menu_name)
function draw_menu($mainMenuNameFromXML)
{
// Open the XML file of possible menus
// as a SimpleXML object
$menus_xml = simplexml_load_file(__DIR__.'/../../../../config/web_portal/menu.xml');
$menu_html = xml_to_menu($menu_name, $menus_xml);
return $menu_html;
}
// Get user in order to correctly display GOCDB admin menu Items
include_once __DIR__ . '/../Get_User_Principle.php';

$identifier = Get_User_Principle();
$userService = \Factory::getUserService();
$user = $userService->getUserByPrinciple($identifier);

// Open the XML file of possible menus as a SimpleXML object
$menusInXML = simplexml_load_file(
__DIR__ . '/../../../../config/web_portal/menu.xml'
);
$menusInHTML = xml_to_html($mainMenuNameFromXML, $menusInXML, $user);

return $menusInHTML;
}

// Reads a menu with the name $menu_name from the $menus_xml simplexml object
// and draws that menu as HTML
function xml_to_menu($menu_name, $menus_xml)
/**
* Reads a menu with the name `$mainMenuNameFromXML` from the `$menusInXML`
* simplexml object and draws that menu as HTML
*/
function xml_to_html($mainMenuNameFromXML, $menusInXML, $user)
{
$html = "";
$html .= "<hr style=\"clear: both;\"/>";
$html .= "<ul class=\"Smaller_Left_Padding Smaller_Top_Margin\">";
foreach($menus_xml->$menu_name->children() as $key => $value)
{
// Check if display of menu is overridden in the local configuration
$isUserAdmin = isset($user) ? $user->isAdmin() : false;

foreach (
$menusInXML->$mainMenuNameFromXML->children() as $key => $value
) {
/**
* Check if the display of menu is overridden
* in the local configuration
*/
if (\Factory::getConfigService()->showMenu($key)) {
$html .= add_menu_item($value) . "\n";
if ($key == "ViewServiceTypes") {
if ($isUserAdmin) {
continue;
}
}
$html .= add_menu_item($value, $isUserAdmin) . "\n";
}
}
$html .= "</ul>";

return $html;
}



function add_menu_item($menu_item)
function add_menu_item($menuItem, $isUserAdmin)
{
//Get user in order to correctly display GOCDB admin menu Items
include_once __DIR__ . '/../Get_User_Principle.php';
$dn = Get_User_Principle();
$userserv = \Factory::getUserService();
$user = $userserv->getUserByPrinciple($dn);
if ($user == null){
$userisadmin = false;
}
else {
$userisadmin = $user->isAdmin();
}

//Find out if the portal is currently read only from local_info.xml
// Find out if the portal is currently read only from local_info.xml
$portalIsReadOnly = \Factory::getConfigService()->IsPortalReadOnly();
$html = "";

foreach($menu_item->children() as $key => $value)
{
$html = "";
switch($key)
{
case "show_on_instance":
$show= strtolower($value);
break;
case "name":
$name = $value;
break;
case "link":
$link = $value;
break;
case "spacer":
// John C: modified this so that we could use show_on_instance for spacers
foreach($menu_item as $child_name => $child_value) {
if($child_name=="show_on_instance") {
// If the spacer has a show_on_instance type that we want to show, then show it
if($child_value == "all" or ($child_value=="write_enabled" and (!$portalIsReadOnly or $userisadmin)) or (($child_value == "admin") and ($userisadmin))){
return "</ul><h4 class='menu_title'>$value</h4><ul class=\"Smaller_Left_Padding Smaller_Top_Margin\">";
}
}
}
return;
}
foreach ($menuItem->children() as $key => $value) {
if ($key == "spacer") {
$showOnInstanceValue = $menuItem->show_on_instance;

/**
* If the spacer has a show_on_instance type that we want to show,
* then show it.
*/
if (
!empty($showOnInstanceValue) &&
canMenuItemBeDisplayed(
$showOnInstanceValue,
$portalIsReadOnly,
$isUserAdmin
)
) {
return "</ul>
<h4 class='menu_title'>$value</h4>
<ul class=\"Smaller_Left_Padding Smaller_Top_Margin\">
";
}

return;
}
}
if ($show == "all" or ($show=="write_enabled" and (!$portalIsReadOnly or $userisadmin)) or (($show == "admin") and ($userisadmin))){
$html .= "<li class=\"Menu_Item\">".
"<a href=\"".htmlspecialchars($link)."\"><span class=\"menu_link\">".
htmlspecialchars($name)."</span></a></li>";

$showOnInstanceValue = strtolower($menuItem->show_on_instance);
$name = $menuItem->name;
$link = $menuItem->link;

if (
canMenuItemBeDisplayed(
$showOnInstanceValue,
$portalIsReadOnly,
$isUserAdmin
)
) {
$html .= "<li class=\"Menu_Item\">" .
"<a href=\"" . htmlspecialchars($link) . "\">
<span class=\"menu_link\">" .
htmlspecialchars($name) .
"</span>
</a>
</li>";
}

return $html;
}

?>
function canMenuItemBeDisplayed(
$showOnInstanceValue,
$portalIsReadOnly,
$isUserAdmin
) {
return (
($showOnInstanceValue == "all") or
(
$showOnInstanceValue == "write_enabled" and
(!$portalIsReadOnly or $isUserAdmin)
) or
(($showOnInstanceValue == "admin") and ($isUserAdmin))
);
}
24 changes: 12 additions & 12 deletions htdocs/web_portal/controllers/admin/view_service_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@
require_once __DIR__ . '/../utils.php';
require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php';

function view_service_type()
function view_service_type($slicedURLSearchName)
{
//Check the user has permission to see the page, will throw exception
//if correct permissions are lacking
checkUserIsAdmin();
$params = [];

$identifier = Get_User_Principle();
$user = \Factory::getUserService()->getUserByPrinciple($identifier);
$params['isUserAdmin'] = checkUserForAdminCredentials($user);

if (!$params['isUserAdmin'] && $slicedURLSearchName == "Admin") {
// Since the user is NOT an Admin, it will throw an ERROR.
checkUserIsAdmin();
}

if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
throw new Exception("An id must be specified");
}
$dn = Get_User_Principle();
$user = \Factory::getUserService()->getUserByPrinciple($dn);

$serv = \Factory::getServiceTypeService();
/**
* @var \ServiceType $serviceType
*/
$serviceType = $serv ->getServiceType($_REQUEST['id']);

$params = [];
$params['Name'] = $serviceType->getName();
$params['Description'] = $serviceType->getDescription();
$params['ID'] = $serviceType->getId();
$params['AllowMonitoringException'] = $serviceType->getAllowMonitoringException();
$params['Services'] = $serv->getServices($params['ID']);
/**
* @var \User $user
*/
$params['portalIsReadOnly'] = portalIsReadOnlyAndUserIsNotAdmin($user);

show_view("admin/view_service_type.php", $params, $params['Name']);
}
21 changes: 13 additions & 8 deletions htdocs/web_portal/controllers/admin/view_service_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@
require_once __DIR__ . '/../utils.php';
require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php';

function show_all(){
//Check the user has permission to see the page, will throw exception
//if correct permissions are lacking
checkUserIsAdmin();
function show_all($slicedURLSearchName)
{
$params = [];

$dn = Get_User_Principle();
$user = \Factory::getUserService()->getUserByPrinciple($dn);
$identifier = Get_User_Principle();
$user = \Factory::getUserService()->getUserByPrinciple($identifier);
$params['isUserAdmin'] = checkUserForAdminCredentials($user);

if (!$params['isUserAdmin'] && $slicedURLSearchName == 'Admin') {
// Since the user is NOT an Admin, it will throw an ERROR.
checkUserIsAdmin();
}

$serviceTypes = \Factory::getServiceTypeService()->getServiceTypes();
$params['ServiceTypes']= $serviceTypes;
$params['portalIsReadOnly'] = portalIsReadOnlyAndUserIsNotAdmin($user);
$params['ServiceTypes'] = $serviceTypes;

show_view('admin/view_service_types.php', $params, 'Service Types');
}
17 changes: 14 additions & 3 deletions htdocs/web_portal/controllers/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ function portalIsReadOnlyAndUserIsNotAdmin(\user $user = null)
// this block is required to deal with unregistered users (where $user is null)
$userIsAdmin = false;
if (! is_null($user)) {
if ($user->isAdmin()) { // sub query required becauser ->isAdmin can't be called on null
// Sub query required becauser ->isAdmin can't be called on null
if (checkUserForAdminCredentials($user)) {
$userIsAdmin = true;
}
}
Expand Down Expand Up @@ -237,7 +238,7 @@ function checkUserIsAdmin()
if ($user == null) {
throw new Exception("Unregistered users may not carry out this operation");
}
if (! $user->isAdmin()) {
if (!checkUserForAdminCredentials($user)) {
throw new Exception("Only GOCDB administrators can perform this action.");
}
}
Expand Down Expand Up @@ -808,7 +809,7 @@ function getReadPDParams($user)
get_class($user) . "'.");
}

if ($user->isAdmin()) {
if (checkUserForAdminCredentials($user)) {
$userIsAdmin = true;
$authenticated = true;
} elseif (\Factory::getUserService()->isAllowReadPD($user)) {
Expand All @@ -817,3 +818,13 @@ function getReadPDParams($user)
}
return array($userIsAdmin, $authenticated);
}

/**
* Checks whether the user is an Admin or NOT.
*
* Returns `true` if the user is an Admin, `false` otherwise.
*/
function checkUserForAdminCredentials($user)
{
return $user->isAdmin();
}
35 changes: 29 additions & 6 deletions htdocs/web_portal/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ function testForHtmlSpecialChars($value, $key){
}
}

/**
* Helper method to get a relative PATH for the required file when `Page_type`
* is equals to either `Service_Types`, `Service_Type`, `Admin_Service_Types`
* or `Admin_Service_Type`.
*/
function helperForServiceTypes($iServiceType)
{
rejectIfNotAuthenticated();
$defaultPath = '/controllers/admin/view_service_types.php';

if ($iServiceType) {
$defaultPath = '/controllers/admin/view_service_type.php';
}

require_once __DIR__ . $defaultPath;
}

/* Decides which type of page to draw based on the passed $Page_Type */
function Draw_Page($Page_Type) {

Expand Down Expand Up @@ -450,15 +467,21 @@ function Draw_Page($Page_Type) {
require_once __DIR__.'/controllers/admin/move_service_end_point.php';
move_service_end_point();
break;
case "Service_Types":
helperForServiceTypes(false);
show_all("user");
break;
case "Admin_Service_Types":
rejectIfNotAuthenticated();
require_once __DIR__.'/controllers/admin/view_service_types.php';
show_all();
helperForServiceTypes(false);
show_all("Admin");
break;
case "Service_Type":
helperForServiceTypes(true);
view_service_type("user");
break;
case "Admin_Service_Type":
rejectIfNotAuthenticated();
require_once __DIR__.'/controllers/admin/view_service_type.php';
view_service_type();
helperForServiceTypes(true);
view_service_type("Admin");
break;
case "Admin_Edit_Service_Type":
rejectIfNotAuthenticated();
Expand Down
Loading

0 comments on commit c1e5ab8

Please sign in to comment.