restservices.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. // Copyright (C) 2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * REST/json services
  20. *
  21. * Definition of common structures + the very minimum service provider (manage objects)
  22. *
  23. * @package REST Services
  24. * @copyright Copyright (C) 2013 Combodo SARL
  25. * @license http://opensource.org/licenses/AGPL-3.0
  26. * @api
  27. */
  28. /**
  29. * Element of the response formed by RestResultWithObjects
  30. *
  31. * @package REST Services
  32. */
  33. class ObjectResult
  34. {
  35. public $code;
  36. public $message;
  37. public $class;
  38. public $key;
  39. public $fields;
  40. /**
  41. * Default constructor
  42. */
  43. public function __construct($sClass = null, $iId = null)
  44. {
  45. $this->code = RestResult::OK;
  46. $this->message = '';
  47. $this->class = $sClass;
  48. $this->key = $iId;
  49. $this->fields = array();
  50. }
  51. /**
  52. * Helper to make an output value for a given attribute
  53. *
  54. * @param DBObject $oObject The object being reported
  55. * @param string $sAttCode The attribute code (must be valid)
  56. * @return string A scalar representation of the value
  57. */
  58. protected function MakeResultValue(DBObject $oObject, $sAttCode)
  59. {
  60. if ($sAttCode == 'id')
  61. {
  62. $value = $oObject->GetKey();
  63. }
  64. else
  65. {
  66. $sClass = get_class($oObject);
  67. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  68. if ($oAttDef instanceof AttributeLinkedSet)
  69. {
  70. $value = array();
  71. // Make the list of required attributes
  72. // - Skip attributes pointing to the current object (redundant data)
  73. // - Skip link sets refering to the current data (infinite recursion!)
  74. $aRelevantAttributes = array();
  75. $sLnkClass = $oAttDef->GetLinkedClass();
  76. foreach (MetaModel::ListAttributeDefs($sLnkClass) as $sLnkAttCode => $oLnkAttDef)
  77. {
  78. // Skip any attribute of the link that points to the current object
  79. //
  80. if ($sLnkAttCode == $oAttDef->GetExtKeyToMe()) continue;
  81. if (method_exists($oLnkAttDef, 'GetKeyAttCode'))
  82. {
  83. if ($oLnkAttDef->GetKeyAttCode() ==$oAttDef->GetExtKeyToMe()) continue;
  84. }
  85. $aRelevantAttributes[] = $sLnkAttCode;
  86. }
  87. // Iterate on the set and build an array of array of attcode=>value
  88. $oSet = $oObject->Get($sAttCode);
  89. while ($oLnk = $oSet->Fetch())
  90. {
  91. $aLnkValues = array();
  92. foreach ($aRelevantAttributes as $sLnkAttCode)
  93. {
  94. $aLnkValues[$sLnkAttCode] = $this->MakeResultValue($oLnk, $sLnkAttCode);
  95. }
  96. $value[] = $aLnkValues;
  97. }
  98. }
  99. else
  100. {
  101. $value = $oAttDef->GetForJSON($oObject->Get($sAttCode));
  102. }
  103. }
  104. return $value;
  105. }
  106. /**
  107. * Report the value for the given object attribute
  108. *
  109. * @param DBObject $oObject The object being reported
  110. * @param string $sAttCode The attribute code (must be valid)
  111. * @return void
  112. */
  113. public function AddField(DBObject $oObject, $sAttCode)
  114. {
  115. $this->fields[$sAttCode] = $this->MakeResultValue($oObject, $sAttCode);
  116. }
  117. }
  118. /**
  119. * REST response for services managing objects. Derive this structure to add information and/or constants
  120. *
  121. * @package Extensibility
  122. * @package REST Services
  123. * @api
  124. */
  125. class RestResultWithObjects extends RestResult
  126. {
  127. public $objects;
  128. /**
  129. * Report the given object
  130. *
  131. * @param int An error code (RestResult::OK is no issue has been found)
  132. * @param string $sMessage Description of the error if any, an empty string otherwise
  133. * @param DBObject $oObject The object being reported
  134. * @param array $aFields An array of attribute codes. List of the attributes to be reported.
  135. * @return void
  136. */
  137. public function AddObject($iCode, $sMessage, $oObject, $aFields)
  138. {
  139. $oObjRes = new ObjectResult(get_class($oObject), $oObject->GetKey());
  140. $oObjRes->code = $iCode;
  141. $oObjRes->message = $sMessage;
  142. foreach ($aFields as $sAttCode)
  143. {
  144. $oObjRes->AddField($oObject, $sAttCode);
  145. }
  146. $sObjKey = get_class($oObject).'::'.$oObject->GetKey();
  147. $this->objects[$sObjKey] = $oObjRes;
  148. }
  149. }
  150. class RestResultWithRelations extends RestResultWithObjects
  151. {
  152. public $relations;
  153. public function __construct()
  154. {
  155. parent::__construct();
  156. $this->relations = array();
  157. }
  158. public function AddRelation($sSrcKey, $sDestKey)
  159. {
  160. if (!array_key_exists($sSrcKey, $this->relations))
  161. {
  162. $this->relations[$sSrcKey] = array();
  163. }
  164. $this->relations[$sSrcKey][] = array('key' => $sDestKey);
  165. }
  166. }
  167. /**
  168. * Deletion result codes for a target object (either deleted or updated)
  169. *
  170. * @package Extensibility
  171. * @api
  172. * @since 2.0.1
  173. */
  174. class RestDelete
  175. {
  176. /**
  177. * Result: Object deleted as per the initial request
  178. */
  179. const OK = 0;
  180. /**
  181. * Result: general issue (user rights or ... ?)
  182. */
  183. const ISSUE = 1;
  184. /**
  185. * Result: Must be deleted to preserve database integrity
  186. */
  187. const AUTO_DELETE = 2;
  188. /**
  189. * Result: Must be deleted to preserve database integrity, but that is NOT possible
  190. */
  191. const AUTO_DELETE_ISSUE = 3;
  192. /**
  193. * Result: Must be deleted to preserve database integrity, but this must be requested explicitely
  194. */
  195. const REQUEST_EXPLICITELY = 4;
  196. /**
  197. * Result: Must be updated to preserve database integrity
  198. */
  199. const AUTO_UPDATE = 5;
  200. /**
  201. * Result: Must be updated to preserve database integrity, but that is NOT possible
  202. */
  203. const AUTO_UPDATE_ISSUE = 6;
  204. }
  205. /**
  206. * Implementation of core REST services (create/get/update... objects)
  207. *
  208. * @package Core
  209. */
  210. class CoreServices implements iRestServiceProvider
  211. {
  212. /**
  213. * Enumerate services delivered by this class
  214. *
  215. * @param string $sVersion The version (e.g. 1.0) supported by the services
  216. * @return array An array of hash 'verb' => verb, 'description' => description
  217. */
  218. public function ListOperations($sVersion)
  219. {
  220. // 1.1 - In the reply, objects have a 'key' entry so that it is no more necessary to split class::key programmaticaly
  221. //
  222. $aOps = array();
  223. if (in_array($sVersion, array('1.0', '1.1')))
  224. {
  225. $aOps[] = array(
  226. 'verb' => 'core/create',
  227. 'description' => 'Create an object'
  228. );
  229. $aOps[] = array(
  230. 'verb' => 'core/update',
  231. 'description' => 'Update an object'
  232. );
  233. $aOps[] = array(
  234. 'verb' => 'core/apply_stimulus',
  235. 'description' => 'Apply a stimulus to change the state of an object'
  236. );
  237. $aOps[] = array(
  238. 'verb' => 'core/get',
  239. 'description' => 'Search for objects'
  240. );
  241. $aOps[] = array(
  242. 'verb' => 'core/delete',
  243. 'description' => 'Delete objects'
  244. );
  245. $aOps[] = array(
  246. 'verb' => 'core/get_related',
  247. 'description' => 'Get related objects through the specified relation'
  248. );
  249. }
  250. return $aOps;
  251. }
  252. /**
  253. * Enumerate services delivered by this class
  254. * @param string $sVersion The version (e.g. 1.0) supported by the services
  255. * @return RestResult The standardized result structure (at least a message)
  256. * @throws Exception in case of internal failure.
  257. */
  258. public function ExecOperation($sVersion, $sVerb, $aParams)
  259. {
  260. $oResult = new RestResultWithObjects();
  261. switch ($sVerb)
  262. {
  263. case 'core/create':
  264. RestUtils::InitTrackingComment($aParams);
  265. $sClass = RestUtils::GetClass($aParams, 'class');
  266. $aFields = RestUtils::GetMandatoryParam($aParams, 'fields');
  267. $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');
  268. $oObject = RestUtils::MakeObjectFromFields($sClass, $aFields);
  269. $oObject->DBInsert();
  270. $oResult->AddObject(0, 'created', $oObject, $aShowFields);
  271. break;
  272. case 'core/update':
  273. RestUtils::InitTrackingComment($aParams);
  274. $sClass = RestUtils::GetClass($aParams, 'class');
  275. $key = RestUtils::GetMandatoryParam($aParams, 'key');
  276. $aFields = RestUtils::GetMandatoryParam($aParams, 'fields');
  277. $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');
  278. $oObject = RestUtils::FindObjectFromKey($sClass, $key);
  279. RestUtils::UpdateObjectFromFields($oObject, $aFields);
  280. $oObject->DBUpdate();
  281. $oResult->AddObject(0, 'updated', $oObject, $aShowFields);
  282. break;
  283. case 'core/apply_stimulus':
  284. RestUtils::InitTrackingComment($aParams);
  285. $sClass = RestUtils::GetClass($aParams, 'class');
  286. $key = RestUtils::GetMandatoryParam($aParams, 'key');
  287. $aFields = RestUtils::GetMandatoryParam($aParams, 'fields');
  288. $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');
  289. $sStimulus = RestUtils::GetMandatoryParam($aParams, 'stimulus');
  290. $oObject = RestUtils::FindObjectFromKey($sClass, $key);
  291. RestUtils::UpdateObjectFromFields($oObject, $aFields);
  292. $aTransitions = $oObject->EnumTransitions();
  293. $aStimuli = MetaModel::EnumStimuli(get_class($oObject));
  294. if (!isset($aTransitions[$sStimulus]))
  295. {
  296. // Invalid stimulus
  297. $oResult->code = RestResult::INTERNAL_ERROR;
  298. $oResult->message = "Invalid stimulus: '$sStimulus' on the object ".$oObject->GetName()." in state '".$oObject->GetState()."'";
  299. }
  300. else
  301. {
  302. $aTransition = $aTransitions[$sStimulus];
  303. $sTargetState = $aTransition['target_state'];
  304. $aStates = MetaModel::EnumStates($sClass);
  305. $aTargetStateDef = $aStates[$sTargetState];
  306. $aExpectedAttributes = $aTargetStateDef['attribute_list'];
  307. $aMissingMandatory = array();
  308. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  309. {
  310. if ( ($iExpectCode & OPT_ATT_MANDATORY) && ($oObject->Get($sAttCode) == ''))
  311. {
  312. $aMissingMandatory[] = $sAttCode;
  313. }
  314. }
  315. if (count($aMissingMandatory) == 0)
  316. {
  317. // If all the mandatory fields are already present, just apply the transition silently...
  318. if ($oObject->ApplyStimulus($sStimulus))
  319. {
  320. $oObject->DBUpdate();
  321. $oResult->AddObject(0, 'updated', $oObject, $aShowFields);
  322. }
  323. }
  324. else
  325. {
  326. // Missing mandatory attributes for the transition
  327. $oResult->code = RestResult::INTERNAL_ERROR;
  328. $oResult->message = 'Missing mandatory attribute(s) for applying the stimulus: '.implode(', ', $aMissingMandatory).'.';
  329. }
  330. }
  331. break;
  332. case 'core/get':
  333. $sClass = RestUtils::GetClass($aParams, 'class');
  334. $key = RestUtils::GetMandatoryParam($aParams, 'key');
  335. $aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');
  336. $oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key);
  337. while ($oObject = $oObjectSet->Fetch())
  338. {
  339. $oResult->AddObject(0, '', $oObject, $aShowFields);
  340. }
  341. $oResult->message = "Found: ".$oObjectSet->Count();
  342. break;
  343. case 'core/delete':
  344. $sClass = RestUtils::GetClass($aParams, 'class');
  345. $key = RestUtils::GetMandatoryParam($aParams, 'key');
  346. $bSimulate = RestUtils::GetOptionalParam($aParams, 'simulate', false);
  347. $oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key);
  348. $aObjects = $oObjectSet->ToArray();
  349. $this->DeleteObjects($oResult, $aObjects, $bSimulate);
  350. break;
  351. case 'core/get_related':
  352. $oResult = new RestResultWithRelations();
  353. $sClass = RestUtils::GetClass($aParams, 'class');
  354. $key = RestUtils::GetMandatoryParam($aParams, 'key');
  355. $sRelation = RestUtils::GetMandatoryParam($aParams, 'relation');
  356. $iMaxRecursionDepth = RestUtils::GetOptionalParam($aParams, 'depth', 20 /* = MAX_RECURSION_DEPTH */);
  357. $aShowFields = array('id', 'friendlyname');
  358. $oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key);
  359. $aIndexByClass = array();
  360. while ($oObject = $oObjectSet->Fetch())
  361. {
  362. $aRelated = array();
  363. $aGraph = array();
  364. $aIndexByClass[get_class($oObject)][$oObject->GetKey()] = null;
  365. $oResult->AddObject(0, '', $oObject, $aShowFields);
  366. $this->GetRelatedObjects($oObject, $sRelation, $iMaxRecursionDepth, $aRelated, $aGraph);
  367. foreach($aRelated as $sClass => $aObjects)
  368. {
  369. foreach($aObjects as $oRelatedObj)
  370. {
  371. $aIndexByClass[get_class($oRelatedObj)][$oRelatedObj->GetKey()] = null;
  372. $oResult->AddObject(0, '', $oRelatedObj, $aShowFields);
  373. }
  374. }
  375. foreach($aGraph as $sSrcKey => $aDestinations)
  376. {
  377. foreach ($aDestinations as $sDestKey)
  378. {
  379. $oResult->AddRelation($sSrcKey, $sDestKey);
  380. }
  381. }
  382. }
  383. if (count($aIndexByClass) > 0)
  384. {
  385. $aStats = array();
  386. foreach ($aIndexByClass as $sClass => $aIds)
  387. {
  388. $aStats[] = $sClass.'= '.count($aIds);
  389. }
  390. $oResult->message = "Scope: ".$oObjectSet->Count()."; Related objects: ".implode(', ', $aStats);
  391. }
  392. else
  393. {
  394. $oResult->message = "Nothing found";
  395. }
  396. break;
  397. default:
  398. // unknown operation: handled at a higher level
  399. }
  400. return $oResult;
  401. }
  402. /**
  403. * Helper for object deletion
  404. */
  405. public function DeleteObjects($oResult, $aObjects, $bSimulate)
  406. {
  407. $oDeletionPlan = new DeletionPlan();
  408. foreach($aObjects as $oObj)
  409. {
  410. if ($bSimulate)
  411. {
  412. $oObj->CheckToDelete($oDeletionPlan);
  413. }
  414. else
  415. {
  416. $oObj->DBDelete($oDeletionPlan);
  417. }
  418. }
  419. foreach ($oDeletionPlan->ListDeletes() as $sTargetClass => $aDeletes)
  420. {
  421. foreach ($aDeletes as $iId => $aData)
  422. {
  423. $oToDelete = $aData['to_delete'];
  424. $bAutoDel = (($aData['mode'] == DEL_SILENT) || ($aData['mode'] == DEL_AUTO));
  425. if (array_key_exists('issue', $aData))
  426. {
  427. if ($bAutoDel)
  428. {
  429. if (isset($aData['requested_explicitely'])) // i.e. in the initial list of objects to delete
  430. {
  431. $iCode = RestDelete::ISSUE;
  432. $sPlanned = 'Cannot be deleted: '.$aData['issue'];
  433. }
  434. else
  435. {
  436. $iCode = RestDelete::AUTO_DELETE_ISSUE;
  437. $sPlanned = 'Should be deleted automatically... but: '.$aData['issue'];
  438. }
  439. }
  440. else
  441. {
  442. $iCode = RestDelete::REQUEST_EXPLICITELY;
  443. $sPlanned = 'Must be deleted explicitely... but: '.$aData['issue'];
  444. }
  445. }
  446. else
  447. {
  448. if ($bAutoDel)
  449. {
  450. if (isset($aData['requested_explicitely']))
  451. {
  452. $iCode = RestDelete::OK;
  453. $sPlanned = '';
  454. }
  455. else
  456. {
  457. $iCode = RestDelete::AUTO_DELETE;
  458. $sPlanned = 'Deleted automatically';
  459. }
  460. }
  461. else
  462. {
  463. $iCode = RestDelete::REQUEST_EXPLICITELY;
  464. $sPlanned = 'Must be deleted explicitely';
  465. }
  466. }
  467. $oResult->AddObject($iCode, $sPlanned, $oToDelete, array('id', 'friendlyname'));
  468. }
  469. }
  470. foreach ($oDeletionPlan->ListUpdates() as $sRemoteClass => $aToUpdate)
  471. {
  472. foreach ($aToUpdate as $iId => $aData)
  473. {
  474. $oToUpdate = $aData['to_reset'];
  475. if (array_key_exists('issue', $aData))
  476. {
  477. $iCode = RestDelete::AUTO_UPDATE_ISSUE;
  478. $sPlanned = 'Should be updated automatically... but: '.$aData['issue'];
  479. }
  480. else
  481. {
  482. $iCode = RestDelete::AUTO_UPDATE;
  483. $sPlanned = 'Reset external keys: '.$aData['attributes_list'];
  484. }
  485. $oResult->AddObject($iCode, $sPlanned, $oToUpdate, array('id', 'friendlyname'));
  486. }
  487. }
  488. if ($oDeletionPlan->FoundStopper())
  489. {
  490. if ($oDeletionPlan->FoundSecurityIssue())
  491. {
  492. $iRes = RestResult::UNAUTHORIZED;
  493. $sRes = 'Deletion not allowed on some objects';
  494. }
  495. elseif ($oDeletionPlan->FoundManualOperation())
  496. {
  497. $iRes = RestResult::UNSAFE;
  498. $sRes = 'The deletion requires that other objects be deleted/updated, and those operations must be requested explicitely';
  499. }
  500. else
  501. {
  502. $iRes = RestResult::INTERNAL_ERROR;
  503. $sRes = 'Some issues have been encountered. See the list of planned changes for more information about the issue(s).';
  504. }
  505. }
  506. else
  507. {
  508. $iRes = RestResult::OK;
  509. $sRes = 'Deleted: '.count($aObjects);
  510. $iIndirect = $oDeletionPlan->GetTargetCount() - count($aObjects);
  511. if ($iIndirect > 0)
  512. {
  513. $sRes .= ' plus (for DB integrity) '.$iIndirect;
  514. }
  515. }
  516. $oResult->code = $iRes;
  517. if ($bSimulate)
  518. {
  519. $oResult->message = 'SIMULATING: '.$sRes;
  520. }
  521. else
  522. {
  523. $oResult->message = $sRes;
  524. }
  525. }
  526. /**
  527. * Helper function to get the related objects up to the given depth along with the "graph" of the relation
  528. * @param DBObject $oObject Starting point of the computation
  529. * @param string $sRelation Code of the relation (i.e; 'impact', 'depends on'...)
  530. * @param integer $iMaxRecursionDepth Maximum level of recursion
  531. * @param Hash $aRelated Two dimensions hash of the already related objects: array( 'class' => array(key => ))
  532. * @param Hash $aGraph Hash array for the topology of the relation: source => related: array('class:key' => array( DBObjects ))
  533. * @param integer $iRecursionDepth Current level of recursion
  534. */
  535. protected function GetRelatedObjects(DBObject $oObject, $sRelation, $iMaxRecursionDepth, &$aRelated, &$aGraph, $iRecursionDepth = 1)
  536. {
  537. // Avoid loops
  538. if ((array_key_exists(get_class($oObject), $aRelated)) && (array_key_exists($oObject->GetKey(), $aRelated[get_class($oObject)]))) return;
  539. // Stop at maximum recursion level
  540. if ($iRecursionDepth > $iMaxRecursionDepth) return;
  541. $sSrcKey = get_class($oObject).'::'.$oObject->GetKey();
  542. $aNewRelated = array();
  543. $oObject->GetRelatedObjects($sRelation, 1, $aNewRelated);
  544. foreach($aNewRelated as $sClass => $aObjects)
  545. {
  546. if (!array_key_exists($sSrcKey, $aGraph))
  547. {
  548. $aGraph[$sSrcKey] = array();
  549. }
  550. foreach($aObjects as $oRelatedObject)
  551. {
  552. $aRelated[$sClass][$oRelatedObject->GetKey()] = $oRelatedObject;
  553. $aGraph[$sSrcKey][] = get_class($oRelatedObject).'::'.$oRelatedObject->GetKey();
  554. $this->GetRelatedObjects($oRelatedObject, $sRelation, $iMaxRecursionDepth, $aRelated, $aGraph, $iRecursionDepth+1);
  555. }
  556. }
  557. }
  558. }