cmdbobject.class.inc.php 16 KB

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