From 3afe4f59b7092ece775e1eee8953c1cbc2eb4fb4 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 17 Nov 2020 09:30:39 +0100 Subject: [PATCH] Add datasets time restriction (#5768) --- php/classes/AmdaAction.php | 30 ++++++++++++++++++++---------- php/classes/AmdaClient.php | 13 +++++++++++++ php/classes/UserMgr.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- php/config.php | 1 + 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/php/classes/AmdaAction.php b/php/classes/AmdaAction.php index 9644f93..3a91748 100644 --- a/php/classes/AmdaAction.php +++ b/php/classes/AmdaAction.php @@ -282,9 +282,10 @@ class AmdaAction $not_yet = true; } } -// if ($child->parentNode->getAttribute('restriction') > 1 ) { -// $timeRestriction = true; -// } + + if ($child->parentNode->hasAttribute('timeRestriction')) { + $timeRestriction = $child->parentNode->getAttribute('timeRestriction'); + } } $component_info = array(); @@ -302,9 +303,10 @@ class AmdaAction $not_yet = true; } } -// if ($child->parentNode->parentNode->getAttribute('restriction') > 1) { -// $timeRestriction = true; -// } + + if ($child->parentNode->parentNode->hasAttribute('timeRestriction')) { + $timeRestriction = $child->parentNode->parentNode->getAttribute('timeRestriction'); + } if ($child->hasAttribute("index1")) $component_info["index1"] = $child->getAttribute('index1'); @@ -342,23 +344,31 @@ class AmdaAction $rank = $child->getAttribute('rank'); } - $disable = $child->hasAttribute('group'); + if ($child->hasAttribute("timeRestriction")) { + $timeRestriction = $child->getAttribute("timeRestriction"); + } + $disable = TRUE; - if ($disable) { + if ($child->hasAttribute('group')) { if ($child->getAttribute("group") === "TBD") { $info .= "
Under Testing"; - } + } else { $info .= "
Restricted Access : Group ".$child->getAttribute('group').""; + if (!empty($timeRestriction)) { + $disable = FALSE; + $info .= "
Time Restriction : ".$timeRestriction.""; + } } } else { + $disable = FALSE; if ($child->hasAttribute('restriction') && $child->getAttribute('restriction') == 'plotOnly') $info .= "
Plot Only!!!"; } $childrenToReturn[] = array('text' => $name, 'id' => $id,'nodeType' => $nodeType, 'info' => $info, - 'leaf' => false, 'help' => $help, 'disable' => $disable, 'rank' => $rank); + 'leaf' => false, 'help' => $help, 'disable' => $disable, 'rank' => $rank, 'timeRestriction' => $timeRestriction); } break; diff --git a/php/classes/AmdaClient.php b/php/classes/AmdaClient.php index 00887c2..15f9907 100644 --- a/php/classes/AmdaClient.php +++ b/php/classes/AmdaClient.php @@ -107,6 +107,19 @@ class AmdaClient { // return true; // } // + + /* Get list of VI with TimeRestriction in DDBASE */ + public function getDatasetsWithTimeRestriction() { + try { + $restrictions = $this->client->getDatasetsWithTimeRestriction(); + } + catch (SoapFault $exception) { + error_log($exception->faultstring.PHP_EOL, 1, email); + return FALSE; + } + return $restrictions; + } + /* Get Time Restriction from VI in DDBASE */ public function getTimeRestriction($dataSet) { try { diff --git a/php/classes/UserMgr.php b/php/classes/UserMgr.php index 52ef00e..d6ce872 100644 --- a/php/classes/UserMgr.php +++ b/php/classes/UserMgr.php @@ -25,6 +25,8 @@ class UserMgr protected $userGrpsCheckSum; //use to know if there is a modification in user groups protected $amdaClient; //client to dd webservice protected $paramMgr, $baseExtXml; + protected $datasetsTimeRestriction; + protected $datasetsTimeRestrictionCheckSum; public $isFirst = false; public $isOldWS = false; @@ -151,6 +153,36 @@ class UserMgr } } + protected function loadDatasetsTimeRestriction() + { + if (isset($this->datasetsTimeRestriction)) { + return; + } + $restrictionFilePath = datasetsTimeRestrictionJson; + if (file_exists($restrictionFilePath)) { + // Re-use the cache file if it's not older than 3600s. + if (abs(time() - filemtime($restrictionFilePath)) > 3600) { + $fileContent = file_get_contents($restrictionFilePath); + if (!empty($fileContent)) { + $this->datasetsTimeRestriction = json_decode($fileContent, TRUE); + if (isset($this->datasetsTimeRestriction) && ($this->datasetsTimeRestriction !== FALSE)) { + $this->datasetsTimeRestrictionCheckSum = md5($fileContent); + return; + } + } + } + } + + $this->datasetsTimeRestrictionCheckSum = ''; + $this->datasetsTimeRestriction = $this->amdaClient->getDatasetsWithTimeRestriction(); + + if ($this->datasetsTimeRestriction !== FALSE) { + $content = json_encode($this->datasetsTimeRestriction); + $this->datasetsTimeRestrictionCheckSum = md5($content); + file_put_contents($restrictionFilePath, $content); + } + } + /* * Check if special groups with settings exist and user is from these groups * Take the first group from user list @@ -617,12 +649,15 @@ class UserMgr protected function makeLocalTree() { + $this->loadDatasetsTimeRestriction(); + $this->loadUserGrps(); if (file_exists(USERWSDIR.'LocalParams.xml')) { - if (file_exists(USERWSDIR.'userGrpsChecksum')) { + if (file_exists(USERWSDIR.'userGrpsChecksum') && file_exists(USERWSDIR.'datasetsTimeRestrictionCheckSum')) { $lastGrpsChecksum = file_get_contents(USERWSDIR.'userGrpsChecksum'); - if ($lastGrpsChecksum == $this->userGrpsCheckSum) { + $lastDatasetsTimeRestrictionCheckSum = file_get_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum'); + if (($lastGrpsChecksum == $this->userGrpsCheckSum) && ($lastDatasetsTimeRestrictionCheckSum == $this->datasetsTimeRestrictionCheckSum)) { //No modification in groups for this user if (filemtime(USERWSDIR.'LocalParams.xml') == filemtime(LocalData.'/LocalParams.xml')) { //And no modification in LocalParams file => skip makeLocalTree @@ -637,8 +672,10 @@ class UserMgr die("Login for ".$this->user." failed: Can't copy LocalParams.xml"); //Save groups checksum used to generate this user local tree file_put_contents(USERWSDIR.'userGrpsChecksum', $this->userGrpsCheckSum); + //Save datasets time restrictions used to generate this user local tree + file_put_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum', $this->datasetsTimeRestrictionCheckSum); - $result = $this->updateTreeForGrps(USERWSDIR.'LocalParams.xml'); + $result = $this->updateTreeForGrpsAndTimeRestrictions(USERWSDIR.'LocalParams.xml'); //Set modif time to the original one touch(USERWSDIR.'LocalParams.xml', filemtime(LocalData.'/LocalParams.xml')); @@ -646,7 +683,7 @@ class UserMgr return TRUE; } - protected function updateTreeForGrps($file) + protected function updateTreeForGrpsAndTimeRestrictions($file) { if (!isset($this->userGrps)) return TRUE; @@ -671,6 +708,36 @@ class UserMgr } } } + + if (!empty($this->datasetsTimeRestriction)) { + foreach ($this->datasetsTimeRestriction as $datasetTimeRestriction) { + $nodes = $xp->query("//dataset[@xml:id='".str_replace('_','-',$datasetTimeRestriction['vi'])."']"); + if ($nodes->length > 0) { + foreach ($nodes as $node) { + $timeRestriction = $datasetTimeRestriction['restriction']; + //Parse time restriction + $n = sscanf($timeRestriction, "%04d-%02d-%02d", $yy, $mm, $dd); + if ($n != 3) { + $n = sscanf($timeRestriction, "%d", $ndays); + if ($n != 1) { + //Cannot parse the time restriction + $timeRestriction = ''; + } + else { + $timeRestriction = date("Y-m-d", strtotime("-$ndays days"))."T23:59:59.999Z"; + } + } + else { + $timeRestriction .= "T23:59:59.999Z"; + } + + if (!empty($timeRestriction)) { + $node->setAttribute('timeRestriction', $timeRestriction); + } + } + } + } + } return $xml->save($file); } diff --git a/php/config.php b/php/config.php index 8b22e1d..2791769 100644 --- a/php/config.php +++ b/php/config.php @@ -90,6 +90,7 @@ define('orbitsAllXml',LocalData.'/OrbitsAll.xml'); define('FeedbackXml', DATAPATH.'/Feedback/Feedback.xml'); define('specialGrpsXml',SpecialSettingsDir.'/Groups.xml'); define('specialSettingsXml',SpecialSettingsDir.'/Settings.xml'); +define('datasetsTimeRestrictionJson',LocalData.'/datasetsTimeRestriction.json'); //Help info dirs define('HELPPATH', IHM_SRC_DIR."/help/"); -- libgit2 0.21.2