cmdbobject.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class cmdbObject
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * cmdbObjectClass
  26. * the file to include, then the core is yours
  27. *
  28. * @package iTopORM
  29. */
  30. require_once('coreexception.class.inc.php');
  31. require_once('config.class.inc.php');
  32. require_once('log.class.inc.php');
  33. require_once('kpi.class.inc.php');
  34. require_once('dict.class.inc.php');
  35. require_once('attributedef.class.inc.php');
  36. require_once('filterdef.class.inc.php');
  37. require_once('stimulus.class.inc.php');
  38. require_once('valuesetdef.class.inc.php');
  39. require_once('MyHelpers.class.inc.php');
  40. require_once('expression.class.inc.php');
  41. require_once('cmdbsource.class.inc.php');
  42. require_once('sqlquery.class.inc.php');
  43. require_once('oql/oqlquery.class.inc.php');
  44. require_once('oql/oqlexception.class.inc.php');
  45. require_once('oql/oql-parser.php');
  46. require_once('oql/oql-lexer.php');
  47. require_once('oql/oqlinterpreter.class.inc.php');
  48. require_once('dbobject.class.php');
  49. require_once('dbobjectsearch.class.php');
  50. require_once('dbobjectset.class.php');
  51. require_once('backgroundprocess.inc.php');
  52. require_once('asynctask.class.inc.php');
  53. require_once('dbproperty.class.inc.php');
  54. // db change tracking data model
  55. require_once('cmdbchange.class.inc.php');
  56. require_once('cmdbchangeop.class.inc.php');
  57. // customization data model
  58. // Romain: temporary moved into application.inc.php (see explanations there)
  59. //require_once('trigger.class.inc.php');
  60. //require_once('action.class.inc.php');
  61. // application log
  62. // Romain: temporary moved into application.inc.php (see explanations there)
  63. //require_once('event.class.inc.php');
  64. require_once('csvparser.class.inc.php');
  65. require_once('bulkchange.class.inc.php');
  66. /**
  67. * A persistent object, which changes are accurately recorded
  68. *
  69. * @package iTopORM
  70. */
  71. abstract class CMDBObject extends DBObject
  72. {
  73. protected $m_datCreated;
  74. protected $m_datUpdated;
  75. // Note: this value is static, but that could be changed because it is sometimes a real issue (see update of interfaces / connected_to
  76. protected static $m_oCurrChange = null;
  77. private function RecordObjCreation(CMDBChange $oChange)
  78. {
  79. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  80. $oMyChangeOp->Set("change", $oChange->GetKey());
  81. $oMyChangeOp->Set("objclass", get_class($this));
  82. $oMyChangeOp->Set("objkey", $this->GetKey());
  83. $iId = $oMyChangeOp->DBInsertNoReload();
  84. }
  85. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  86. {
  87. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  88. $oMyChangeOp->Set("change", $oChange->GetKey());
  89. $oMyChangeOp->Set("objclass", get_class($this));
  90. $oMyChangeOp->Set("objkey", $objkey);
  91. $iId = $oMyChangeOp->DBInsertNoReload();
  92. }
  93. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  94. {
  95. // $aValues is an array of $sAttCode => $value
  96. //
  97. foreach ($aValues as $sAttCode=> $value)
  98. {
  99. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  100. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  101. if ($oAttDef instanceOf AttributeOneWayPassword)
  102. {
  103. // One Way encrypted passwords' history is stored -one way- encrypted
  104. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword");
  105. $oMyChangeOp->Set("change", $oChange->GetKey());
  106. $oMyChangeOp->Set("objclass", get_class($this));
  107. $oMyChangeOp->Set("objkey", $this->GetKey());
  108. $oMyChangeOp->Set("attcode", $sAttCode);
  109. if (array_key_exists($sAttCode, $aOrigValues))
  110. {
  111. $original = $aOrigValues[$sAttCode];
  112. }
  113. else
  114. {
  115. $original = '';
  116. }
  117. $oMyChangeOp->Set("prev_pwd", $original);
  118. $iId = $oMyChangeOp->DBInsertNoReload();
  119. }
  120. elseif ($oAttDef instanceOf AttributeEncryptedString)
  121. {
  122. // Encrypted string history is stored encrypted
  123. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted");
  124. $oMyChangeOp->Set("change", $oChange->GetKey());
  125. $oMyChangeOp->Set("objclass", get_class($this));
  126. $oMyChangeOp->Set("objkey", $this->GetKey());
  127. $oMyChangeOp->Set("attcode", $sAttCode);
  128. if (array_key_exists($sAttCode, $aOrigValues))
  129. {
  130. $original = $aOrigValues[$sAttCode];
  131. }
  132. else
  133. {
  134. $original = '';
  135. }
  136. $oMyChangeOp->Set("prevstring", $original);
  137. $iId = $oMyChangeOp->DBInsertNoReload();
  138. }
  139. elseif ($oAttDef instanceOf AttributeBlob)
  140. {
  141. // Data blobs
  142. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  143. $oMyChangeOp->Set("change", $oChange->GetKey());
  144. $oMyChangeOp->Set("objclass", get_class($this));
  145. $oMyChangeOp->Set("objkey", $this->GetKey());
  146. $oMyChangeOp->Set("attcode", $sAttCode);
  147. if (array_key_exists($sAttCode, $aOrigValues))
  148. {
  149. $original = $aOrigValues[$sAttCode];
  150. }
  151. else
  152. {
  153. $original = new ormDocument();
  154. }
  155. $oMyChangeOp->Set("prevdata", $original);
  156. $iId = $oMyChangeOp->DBInsertNoReload();
  157. }
  158. elseif ($oAttDef instanceOf AttributeText)
  159. {
  160. // Data blobs
  161. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  162. $oMyChangeOp->Set("change", $oChange->GetKey());
  163. $oMyChangeOp->Set("objclass", get_class($this));
  164. $oMyChangeOp->Set("objkey", $this->GetKey());
  165. $oMyChangeOp->Set("attcode", $sAttCode);
  166. if (array_key_exists($sAttCode, $aOrigValues))
  167. {
  168. $original = $aOrigValues[$sAttCode];
  169. if ($original instanceof ormCaseLog)
  170. {
  171. $original = $original->GetText();
  172. }
  173. }
  174. else
  175. {
  176. $original = null;
  177. }
  178. $oMyChangeOp->Set("prevdata", $original);
  179. $iId = $oMyChangeOp->DBInsertNoReload();
  180. }
  181. else
  182. {
  183. // Scalars
  184. //
  185. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  186. $oMyChangeOp->Set("change", $oChange->GetKey());
  187. $oMyChangeOp->Set("objclass", get_class($this));
  188. $oMyChangeOp->Set("objkey", $this->GetKey());
  189. $oMyChangeOp->Set("attcode", $sAttCode);
  190. if (array_key_exists($sAttCode, $aOrigValues))
  191. {
  192. $sOriginalValue = $aOrigValues[$sAttCode];
  193. }
  194. else
  195. {
  196. $sOriginalValue = 'undefined';
  197. }
  198. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  199. $oMyChangeOp->Set("newvalue", $value);
  200. $iId = $oMyChangeOp->DBInsertNoReload();
  201. }
  202. }
  203. }
  204. /**
  205. * Helper to ultimately check user rights before writing (Insert, Update or Delete)
  206. * The check should never fail, because the UI should prevent from such a usage
  207. * Anyhow, if the user has found a workaround... the security gets enforced here
  208. */
  209. protected function CheckUserRights($bSkipStrongSecurity, $iActionCode)
  210. {
  211. if (is_null($bSkipStrongSecurity))
  212. {
  213. // This is temporary
  214. // We have implemented this safety net right before releasing iTop 1.0
  215. // and we decided that it was too risky to activate it
  216. // Anyhow, users willing to have a very strong security could set
  217. // skip_strong_security = 0, in the config file
  218. $bSkipStrongSecurity = MetaModel::GetConfig()->Get('skip_strong_security');
  219. }
  220. if (!$bSkipStrongSecurity)
  221. {
  222. $sClass = get_class($this);
  223. $oSet = DBObjectSet::FromObject($this);
  224. if (!UserRights::IsActionAllowed($sClass, $iActionCode, $oSet))
  225. {
  226. // Intrusion detected
  227. throw new SecurityException('You are not allowed to modify objects of class: '.$sClass);
  228. }
  229. }
  230. }
  231. public function DBInsert()
  232. {
  233. if(!is_object(self::$m_oCurrChange))
  234. {
  235. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  236. }
  237. return $this->DBInsertTracked_Internal();
  238. }
  239. public function DBInsertTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  240. {
  241. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  242. self::$m_oCurrChange = $oChange;
  243. $ret = $this->DBInsertTracked_Internal();
  244. self::$m_oCurrChange = null;
  245. return $ret;
  246. }
  247. public function DBInsertTrackedNoReload(CMDBChange $oChange, $bSkipStrongSecurity = null)
  248. {
  249. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  250. self::$m_oCurrChange = $oChange;
  251. $ret = $this->DBInsertTracked_Internal(true);
  252. self::$m_oCurrChange = null;
  253. return $ret;
  254. }
  255. protected function DBInsertTracked_Internal($bDoNotReload = false)
  256. {
  257. if ($bDoNotReload)
  258. {
  259. $ret = parent::DBInsertNoReload();
  260. }
  261. else
  262. {
  263. $ret = parent::DBInsert();
  264. }
  265. $this->RecordObjCreation(self::$m_oCurrChange);
  266. return $ret;
  267. }
  268. public function DBClone($newKey = null)
  269. {
  270. if(!self::$m_oCurrChange)
  271. {
  272. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  273. }
  274. return $this->DBCloneTracked_Internal();
  275. }
  276. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  277. {
  278. self::$m_oCurrChange = $oChange;
  279. $this->DBCloneTracked_Internal($newKey);
  280. self::$m_oCurrChange = null;
  281. }
  282. protected function DBCloneTracked_Internal($newKey = null)
  283. {
  284. $newKey = parent::DBClone($newKey);
  285. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  286. $oClone->RecordObjCreation(self::$m_oCurrChange);
  287. return $newKey;
  288. }
  289. public function DBUpdate()
  290. {
  291. if(!self::$m_oCurrChange)
  292. {
  293. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  294. }
  295. return $this->DBUpdateTracked_internal();
  296. }
  297. public function DBUpdateTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  298. {
  299. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  300. self::$m_oCurrChange = $oChange;
  301. $this->DBUpdateTracked_Internal();
  302. self::$m_oCurrChange = null;
  303. }
  304. protected function DBUpdateTracked_Internal()
  305. {
  306. // Copy the changes list before the update (the list should be reset afterwards)
  307. $aChanges = $this->ListChanges();
  308. if (count($aChanges) == 0)
  309. {
  310. //throw new CoreWarning("Attempting to update an unchanged object");
  311. return;
  312. }
  313. // Save the original values (will be reset to the new values when the object get written to the DB)
  314. $aOriginalValues = $this->m_aOrigValues;
  315. $ret = parent::DBUpdate();
  316. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  317. return $ret;
  318. }
  319. public function DBDelete()
  320. {
  321. if(!self::$m_oCurrChange)
  322. {
  323. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  324. }
  325. return $this->DBDeleteTracked_Internal();
  326. }
  327. public function DBDeleteTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  328. {
  329. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_DELETE);
  330. self::$m_oCurrChange = $oChange;
  331. $this->DBDeleteTracked_Internal();
  332. self::$m_oCurrChange = null;
  333. }
  334. protected function DBDeleteTracked_Internal()
  335. {
  336. $prevkey = $this->GetKey();
  337. $ret = parent::DBDelete();
  338. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  339. return $ret;
  340. }
  341. public static function BulkDelete(DBObjectSearch $oFilter)
  342. {
  343. if(!self::$m_oCurrChange)
  344. {
  345. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  346. }
  347. return $this->BulkDeleteTracked_Internal($oFilter);
  348. }
  349. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  350. {
  351. self::$m_oCurrChange = $oChange;
  352. $this->BulkDeleteTracked_Internal($oFilter);
  353. self::$m_oCurrChange = null;
  354. }
  355. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  356. {
  357. throw new CoreWarning("Change tracking not tested for bulk operations");
  358. // Get the list of objects to delete (and record data before deleting the DB records)
  359. $oObjSet = new CMDBObjectSet($oFilter);
  360. $aObjAndKeys = array(); // array of id=>object
  361. while ($oItem = $oObjSet->Fetch())
  362. {
  363. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  364. }
  365. $oObjSet->FreeResult();
  366. // Delete in one single efficient query
  367. $ret = parent::BulkDelete($oFilter);
  368. // Record... in many queries !!!
  369. foreach($aObjAndKeys as $prevkey=>$oItem)
  370. {
  371. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  372. }
  373. return $ret;
  374. }
  375. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  376. {
  377. if(!self::$m_oCurrChange)
  378. {
  379. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  380. }
  381. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  382. }
  383. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  384. {
  385. self::$m_oCurrChange = $oChange;
  386. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  387. self::$m_oCurrChange = null;
  388. }
  389. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  390. {
  391. // $aValues is an array of $sAttCode => $value
  392. // Get the list of objects to update (and load it before doing the change)
  393. $oObjSet = new CMDBObjectSet($oFilter);
  394. $oObjSet->Load();
  395. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  396. $aOriginalValues = array();
  397. $oObjSet->Rewind();
  398. while ($oItem = $oObjSet->Fetch())
  399. {
  400. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  401. }
  402. // Update in one single efficient query
  403. $ret = parent::BulkUpdate($oFilter, $aValues);
  404. // Record... in many queries !!!
  405. $oObjSet->Rewind();
  406. while ($oItem = $oObjSet->Fetch())
  407. {
  408. $aChangedValues = $oItem->ListChangedValues($aValues);
  409. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  410. }
  411. return $ret;
  412. }
  413. }
  414. /**
  415. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  416. *
  417. * @package iTopORM
  418. */
  419. class CMDBObjectSet extends DBObjectSet
  420. {
  421. // this is the public interface (?)
  422. // I have to define those constructors here... :-(
  423. // just to get the right object class in return.
  424. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  425. static public function FromScratch($sClass)
  426. {
  427. $oFilter = new CMDBSearchFilter($sClass);
  428. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  429. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  430. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  431. $oRetSet->m_bLoaded = true; // no DB load
  432. return $oRetSet;
  433. }
  434. static public function FromArray($sClass, $aObjects)
  435. {
  436. $oFilter = new CMDBSearchFilter($sClass);
  437. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  438. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  439. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  440. $oRetSet->m_bLoaded = true; // no DB load
  441. $oRetSet->AddObjectArray($aObjects);
  442. return $oRetSet;
  443. }
  444. static public function FromArrayAssoc($aClasses, $aObjects)
  445. {
  446. // In a perfect world, we should create a complete tree of DBObjectSearch,
  447. // but as we lack most of the information related to the objects,
  448. // let's create one search definition
  449. $sClass = reset($aClasses);
  450. $sAlias = key($aClasses);
  451. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  452. $oRetSet = new CMDBObjectSet($oFilter);
  453. $oRetSet->m_bLoaded = true; // no DB load
  454. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  455. {
  456. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  457. }
  458. return $oRetSet;
  459. }
  460. }
  461. /**
  462. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  463. *
  464. * @package iTopORM
  465. */
  466. class CMDBSearchFilter extends DBObjectSearch
  467. {
  468. // this is the public interface (?)
  469. }
  470. ?>