Commit 3afe4f59b7092ece775e1eee8953c1cbc2eb4fb4

Authored by Benjamin Renard
1 parent f4620bdf

Add datasets time restriction (#5768)

php/classes/AmdaAction.php
... ... @@ -282,9 +282,10 @@ class AmdaAction
282 282 $not_yet = true;
283 283 }
284 284 }
285   -// if ($child->parentNode->getAttribute('restriction') > 1 ) {
286   -// $timeRestriction = true;
287   -// }
  285 +
  286 + if ($child->parentNode->hasAttribute('timeRestriction')) {
  287 + $timeRestriction = $child->parentNode->getAttribute('timeRestriction');
  288 + }
288 289 }
289 290  
290 291 $component_info = array();
... ... @@ -302,9 +303,10 @@ class AmdaAction
302 303 $not_yet = true;
303 304 }
304 305 }
305   -// if ($child->parentNode->parentNode->getAttribute('restriction') > 1) {
306   -// $timeRestriction = true;
307   -// }
  306 +
  307 + if ($child->parentNode->parentNode->hasAttribute('timeRestriction')) {
  308 + $timeRestriction = $child->parentNode->parentNode->getAttribute('timeRestriction');
  309 + }
308 310  
309 311 if ($child->hasAttribute("index1"))
310 312 $component_info["index1"] = $child->getAttribute('index1');
... ... @@ -342,23 +344,31 @@ class AmdaAction
342 344 $rank = $child->getAttribute('rank');
343 345 }
344 346  
345   - $disable = $child->hasAttribute('group');
  347 + if ($child->hasAttribute("timeRestriction")) {
  348 + $timeRestriction = $child->getAttribute("timeRestriction");
  349 + }
  350 + $disable = TRUE;
346 351  
347   - if ($disable) {
  352 + if ($child->hasAttribute('group')) {
348 353 if ($child->getAttribute("group") === "TBD") {
349 354 $info .= "<br/><b>Under Testing</b>";
350   - }
  355 + }
351 356 else {
352 357 $info .= "<br/><b>Restricted Access : Group ".$child->getAttribute('group')."</b>";
  358 + if (!empty($timeRestriction)) {
  359 + $disable = FALSE;
  360 + $info .= "<br/><b>Time Restriction : ".$timeRestriction."</b>";
  361 + }
353 362 }
354 363 }
355 364 else {
  365 + $disable = FALSE;
356 366 if ($child->hasAttribute('restriction') && $child->getAttribute('restriction') == 'plotOnly')
357 367 $info .= "<br/><b>Plot Only!!!</b>";
358 368 }
359 369  
360 370 $childrenToReturn[] = array('text' => $name, 'id' => $id,'nodeType' => $nodeType, 'info' => $info,
361   - 'leaf' => false, 'help' => $help, 'disable' => $disable, 'rank' => $rank);
  371 + 'leaf' => false, 'help' => $help, 'disable' => $disable, 'rank' => $rank, 'timeRestriction' => $timeRestriction);
362 372 }
363 373 break;
364 374  
... ...
php/classes/AmdaClient.php
... ... @@ -107,6 +107,19 @@ class AmdaClient {
107 107 // return true;
108 108 // }
109 109 //
  110 +
  111 + /* Get list of VI with TimeRestriction in DDBASE */
  112 + public function getDatasetsWithTimeRestriction() {
  113 + try {
  114 + $restrictions = $this->client->getDatasetsWithTimeRestriction();
  115 + }
  116 + catch (SoapFault $exception) {
  117 + error_log($exception->faultstring.PHP_EOL, 1, email);
  118 + return FALSE;
  119 + }
  120 + return $restrictions;
  121 + }
  122 +
110 123 /* Get Time Restriction from VI in DDBASE */
111 124 public function getTimeRestriction($dataSet) {
112 125 try {
... ...
php/classes/UserMgr.php
... ... @@ -25,6 +25,8 @@ class UserMgr
25 25 protected $userGrpsCheckSum; //use to know if there is a modification in user groups
26 26 protected $amdaClient; //client to dd webservice
27 27 protected $paramMgr, $baseExtXml;
  28 + protected $datasetsTimeRestriction;
  29 + protected $datasetsTimeRestrictionCheckSum;
28 30  
29 31 public $isFirst = false;
30 32 public $isOldWS = false;
... ... @@ -151,6 +153,36 @@ class UserMgr
151 153 }
152 154 }
153 155  
  156 + protected function loadDatasetsTimeRestriction()
  157 + {
  158 + if (isset($this->datasetsTimeRestriction)) {
  159 + return;
  160 + }
  161 + $restrictionFilePath = datasetsTimeRestrictionJson;
  162 + if (file_exists($restrictionFilePath)) {
  163 + // Re-use the cache file if it's not older than 3600s.
  164 + if (abs(time() - filemtime($restrictionFilePath)) > 3600) {
  165 + $fileContent = file_get_contents($restrictionFilePath);
  166 + if (!empty($fileContent)) {
  167 + $this->datasetsTimeRestriction = json_decode($fileContent, TRUE);
  168 + if (isset($this->datasetsTimeRestriction) && ($this->datasetsTimeRestriction !== FALSE)) {
  169 + $this->datasetsTimeRestrictionCheckSum = md5($fileContent);
  170 + return;
  171 + }
  172 + }
  173 + }
  174 + }
  175 +
  176 + $this->datasetsTimeRestrictionCheckSum = '';
  177 + $this->datasetsTimeRestriction = $this->amdaClient->getDatasetsWithTimeRestriction();
  178 +
  179 + if ($this->datasetsTimeRestriction !== FALSE) {
  180 + $content = json_encode($this->datasetsTimeRestriction);
  181 + $this->datasetsTimeRestrictionCheckSum = md5($content);
  182 + file_put_contents($restrictionFilePath, $content);
  183 + }
  184 + }
  185 +
154 186 /*
155 187 * Check if special groups with settings exist and user is from these groups
156 188 * Take the first group from user list
... ... @@ -617,12 +649,15 @@ class UserMgr
617 649  
618 650 protected function makeLocalTree()
619 651 {
  652 + $this->loadDatasetsTimeRestriction();
  653 +
620 654 $this->loadUserGrps();
621 655  
622 656 if (file_exists(USERWSDIR.'LocalParams.xml')) {
623   - if (file_exists(USERWSDIR.'userGrpsChecksum')) {
  657 + if (file_exists(USERWSDIR.'userGrpsChecksum') && file_exists(USERWSDIR.'datasetsTimeRestrictionCheckSum')) {
624 658 $lastGrpsChecksum = file_get_contents(USERWSDIR.'userGrpsChecksum');
625   - if ($lastGrpsChecksum == $this->userGrpsCheckSum) {
  659 + $lastDatasetsTimeRestrictionCheckSum = file_get_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum');
  660 + if (($lastGrpsChecksum == $this->userGrpsCheckSum) && ($lastDatasetsTimeRestrictionCheckSum == $this->datasetsTimeRestrictionCheckSum)) {
626 661 //No modification in groups for this user
627 662 if (filemtime(USERWSDIR.'LocalParams.xml') == filemtime(LocalData.'/LocalParams.xml')) {
628 663 //And no modification in LocalParams file => skip makeLocalTree
... ... @@ -637,8 +672,10 @@ class UserMgr
637 672 die("Login for ".$this->user." failed: Can't copy LocalParams.xml");
638 673 //Save groups checksum used to generate this user local tree
639 674 file_put_contents(USERWSDIR.'userGrpsChecksum', $this->userGrpsCheckSum);
  675 + //Save datasets time restrictions used to generate this user local tree
  676 + file_put_contents(USERWSDIR.'datasetsTimeRestrictionCheckSum', $this->datasetsTimeRestrictionCheckSum);
640 677  
641   - $result = $this->updateTreeForGrps(USERWSDIR.'LocalParams.xml');
  678 + $result = $this->updateTreeForGrpsAndTimeRestrictions(USERWSDIR.'LocalParams.xml');
642 679  
643 680 //Set modif time to the original one
644 681 touch(USERWSDIR.'LocalParams.xml', filemtime(LocalData.'/LocalParams.xml'));
... ... @@ -646,7 +683,7 @@ class UserMgr
646 683 return TRUE;
647 684 }
648 685  
649   - protected function updateTreeForGrps($file)
  686 + protected function updateTreeForGrpsAndTimeRestrictions($file)
650 687 {
651 688 if (!isset($this->userGrps))
652 689 return TRUE;
... ... @@ -671,6 +708,36 @@ class UserMgr
671 708 }
672 709 }
673 710 }
  711 +
  712 + if (!empty($this->datasetsTimeRestriction)) {
  713 + foreach ($this->datasetsTimeRestriction as $datasetTimeRestriction) {
  714 + $nodes = $xp->query("//dataset[@xml:id='".str_replace('_','-',$datasetTimeRestriction['vi'])."']");
  715 + if ($nodes->length > 0) {
  716 + foreach ($nodes as $node) {
  717 + $timeRestriction = $datasetTimeRestriction['restriction'];
  718 + //Parse time restriction
  719 + $n = sscanf($timeRestriction, "%04d-%02d-%02d", $yy, $mm, $dd);
  720 + if ($n != 3) {
  721 + $n = sscanf($timeRestriction, "%d", $ndays);
  722 + if ($n != 1) {
  723 + //Cannot parse the time restriction
  724 + $timeRestriction = '';
  725 + }
  726 + else {
  727 + $timeRestriction = date("Y-m-d", strtotime("-$ndays days"))."T23:59:59.999Z";
  728 + }
  729 + }
  730 + else {
  731 + $timeRestriction .= "T23:59:59.999Z";
  732 + }
  733 +
  734 + if (!empty($timeRestriction)) {
  735 + $node->setAttribute('timeRestriction', $timeRestriction);
  736 + }
  737 + }
  738 + }
  739 + }
  740 + }
674 741  
675 742 return $xml->save($file);
676 743 }
... ...
php/config.php
... ... @@ -90,6 +90,7 @@ define(&#39;orbitsAllXml&#39;,LocalData.&#39;/OrbitsAll.xml&#39;);
90 90 define('FeedbackXml', DATAPATH.'/Feedback/Feedback.xml');
91 91 define('specialGrpsXml',SpecialSettingsDir.'/Groups.xml');
92 92 define('specialSettingsXml',SpecialSettingsDir.'/Settings.xml');
  93 +define('datasetsTimeRestrictionJson',LocalData.'/datasetsTimeRestriction.json');
93 94  
94 95 //Help info dirs
95 96 define('HELPPATH', IHM_SRC_DIR."/help/");
... ...