cmdbchangeop.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. * Persistent classes (internal) : cmdbChangeOp and derived
  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. * Various atomic change operations, to be tracked
  26. *
  27. * @package iTopORM
  28. */
  29. class CMDBChangeOp extends DBObject
  30. {
  31. public static function Init()
  32. {
  33. $aParams = array
  34. (
  35. "category" => "core/cmdb",
  36. "key_type" => "autoincrement",
  37. "name_attcode" => "change",
  38. "state_attcode" => "",
  39. "reconc_keys" => array(),
  40. "db_table" => "priv_changeop",
  41. "db_key_field" => "id",
  42. "db_finalclass_field" => "optype",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. //MetaModel::Init_InheritAttributes();
  46. MetaModel::Init_AddAttribute(new AttributeExternalKey("change", array("allowed_values"=>null, "sql"=>"changeid", "targetclass"=>"CMDBChange", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date")));
  48. MetaModel::Init_AddAttribute(new AttributeExternalField("userinfo", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"userinfo")));
  49. MetaModel::Init_AddAttribute(new AttributeString("objclass", array("allowed_values"=>null, "sql"=>"objclass", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeString("objkey", array("allowed_values"=>null, "sql"=>"objkey", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  51. MetaModel::Init_SetZListItems('details', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  52. MetaModel::Init_SetZListItems('list', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  53. }
  54. /**
  55. * Describe (as a text string) the modifications corresponding to this change
  56. */
  57. public function GetDescription()
  58. {
  59. return '';
  60. }
  61. }
  62. /**
  63. * Record the creation of an object
  64. *
  65. * @package iTopORM
  66. */
  67. class CMDBChangeOpCreate extends CMDBChangeOp
  68. {
  69. public static function Init()
  70. {
  71. $aParams = array
  72. (
  73. "category" => "core/cmdb",
  74. "key_type" => "",
  75. "name_attcode" => "change",
  76. "state_attcode" => "",
  77. "reconc_keys" => array(),
  78. "db_table" => "priv_changeop_create",
  79. "db_key_field" => "id",
  80. "db_finalclass_field" => "",
  81. );
  82. MetaModel::Init_Params($aParams);
  83. MetaModel::Init_InheritAttributes();
  84. }
  85. /**
  86. * Describe (as a text string) the modifications corresponding to this change
  87. */
  88. public function GetDescription()
  89. {
  90. return Dict::S('Change:ObjectCreated');
  91. }
  92. }
  93. /**
  94. * Record the deletion of an object
  95. *
  96. * @package iTopORM
  97. */
  98. class CMDBChangeOpDelete extends CMDBChangeOp
  99. {
  100. public static function Init()
  101. {
  102. $aParams = array
  103. (
  104. "category" => "core/cmdb",
  105. "key_type" => "",
  106. "name_attcode" => "change",
  107. "state_attcode" => "",
  108. "reconc_keys" => array(),
  109. "db_table" => "priv_changeop_delete",
  110. "db_key_field" => "id",
  111. "db_finalclass_field" => "",
  112. );
  113. MetaModel::Init_Params($aParams);
  114. MetaModel::Init_InheritAttributes();
  115. }
  116. /**
  117. * Describe (as a text string) the modifications corresponding to this change
  118. */
  119. public function GetDescription()
  120. {
  121. return Dict::S('Change:ObjectDeleted');
  122. }
  123. }
  124. /**
  125. * Record the modification of an attribute (abstract)
  126. *
  127. * @package iTopORM
  128. */
  129. class CMDBChangeOpSetAttribute extends CMDBChangeOp
  130. {
  131. public static function Init()
  132. {
  133. $aParams = array
  134. (
  135. "category" => "core/cmdb",
  136. "key_type" => "",
  137. "name_attcode" => "change",
  138. "state_attcode" => "",
  139. "reconc_keys" => array(),
  140. "db_table" => "priv_changeop_setatt",
  141. "db_key_field" => "id",
  142. "db_finalclass_field" => "",
  143. );
  144. MetaModel::Init_Params($aParams);
  145. MetaModel::Init_InheritAttributes();
  146. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  147. // Display lists
  148. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  149. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  150. }
  151. }
  152. /**
  153. * Record the modification of a scalar attribute
  154. *
  155. * @package iTopORM
  156. */
  157. class CMDBChangeOpSetAttributeScalar extends CMDBChangeOpSetAttribute
  158. {
  159. public static function Init()
  160. {
  161. $aParams = array
  162. (
  163. "category" => "core/cmdb",
  164. "key_type" => "",
  165. "name_attcode" => "change",
  166. "state_attcode" => "",
  167. "reconc_keys" => array(),
  168. "db_table" => "priv_changeop_setatt_scalar",
  169. "db_key_field" => "id",
  170. "db_finalclass_field" => "",
  171. );
  172. MetaModel::Init_Params($aParams);
  173. MetaModel::Init_InheritAttributes();
  174. MetaModel::Init_AddAttribute(new AttributeString("oldvalue", array("allowed_values"=>null, "sql"=>"oldvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  175. MetaModel::Init_AddAttribute(new AttributeString("newvalue", array("allowed_values"=>null, "sql"=>"newvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  176. // Display lists
  177. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for the complete details
  178. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for a list
  179. }
  180. /**
  181. * Describe (as a text string) the modifications corresponding to this change
  182. */
  183. public function GetDescription()
  184. {
  185. // Temporary, until we change the options of GetDescription() -needs a more global revision
  186. $bIsHtml = true;
  187. $sResult = '';
  188. $oTargetObjectClass = $this->Get('objclass');
  189. $oTargetObjectKey = $this->Get('objkey');
  190. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  191. $oTargetSearch->AddCondition('id', $oTargetObjectKey, '=');
  192. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  193. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  194. {
  195. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  196. $sAttName = $oAttDef->GetLabel();
  197. $sNewValue = $this->Get('newvalue');
  198. $sOldValue = $this->Get('oldvalue');
  199. if ( (($oAttDef->GetType() == 'String') || ($oAttDef->GetType() == 'Text')) &&
  200. (strlen($sNewValue) > strlen($sOldValue)) )
  201. {
  202. // Check if some text was not appended to the field
  203. if (substr($sNewValue,0, strlen($sOldValue)) == $sOldValue) // Text added at the end
  204. {
  205. $sDelta = substr($sNewValue, strlen($sOldValue));
  206. $sResult = Dict::Format('Change:Text_AppendedTo_AttName', $sDelta, $sAttName);
  207. }
  208. else if (substr($sNewValue, -strlen($sOldValue)) == $sOldValue) // Text added at the beginning
  209. {
  210. $sDelta = substr($sNewValue, 0, strlen($sNewValue) - strlen($sOldValue));
  211. $sResult = Dict::Format('Change:Text_AppendedTo_AttName', $sDelta, $sAttName);
  212. }
  213. else
  214. {
  215. $sResult = Dict::Format('Change:AttName_SetTo_NewValue_PreviousValue_OldValue', $sAttName, $sNewValue, $sOldValue);
  216. }
  217. }
  218. elseif($bIsHtml && $oAttDef->IsExternalKey())
  219. {
  220. $sTargetClass = $oAttDef->GetTargetClass();
  221. $sFrom = MetaModel::GetHyperLink($sTargetClass, $sOldValue);
  222. $sTo = MetaModel::GetHyperLink($sTargetClass, $sNewValue);
  223. $sResult = "$sAttName set to $sTo (previous: $sFrom)";
  224. }
  225. elseif ($oAttDef instanceOf AttributeBlob)
  226. {
  227. $sResult = "#@# Issue... found an attribute for which other type of tracking should be made";
  228. }
  229. else
  230. {
  231. $sResult = Dict::Format('Change:AttName_SetTo_NewValue_PreviousValue_OldValue', $sAttName, $sNewValue, $sOldValue);
  232. }
  233. }
  234. return $sResult;
  235. }
  236. }
  237. /**
  238. * Record the modification of a blob
  239. *
  240. * @package iTopORM
  241. */
  242. class CMDBChangeOpSetAttributeBlob extends CMDBChangeOpSetAttribute
  243. {
  244. public static function Init()
  245. {
  246. $aParams = array
  247. (
  248. "category" => "core/cmdb",
  249. "key_type" => "",
  250. "name_attcode" => "change",
  251. "state_attcode" => "",
  252. "reconc_keys" => array(),
  253. "db_table" => "priv_changeop_setatt_data",
  254. "db_key_field" => "id",
  255. "db_finalclass_field" => "",
  256. );
  257. MetaModel::Init_Params($aParams);
  258. MetaModel::Init_InheritAttributes();
  259. MetaModel::Init_AddAttribute(new AttributeBlob("prevdata", array("depends_on"=>array())));
  260. // Display lists
  261. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  262. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  263. }
  264. /**
  265. * Describe (as a text string) the modifications corresponding to this change
  266. */
  267. public function GetDescription()
  268. {
  269. // Temporary, until we change the options of GetDescription() -needs a more global revision
  270. $bIsHtml = true;
  271. $sResult = '';
  272. $oTargetObjectClass = $this->Get('objclass');
  273. $oTargetObjectKey = $this->Get('objkey');
  274. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  275. $oTargetSearch->AddCondition('id', $oTargetObjectKey, '=');
  276. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  277. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  278. {
  279. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  280. $sAttName = $oAttDef->GetLabel();
  281. $oPrevDoc = $this->Get('prevdata');
  282. $sDocView = $oPrevDoc->GetAsHtml();
  283. $sDocView .= "<br/>".Dict::Format('UI:OpenDocumentInNewWindow_',$oPrevDoc->GetDisplayLink(get_class($this), $this->GetKey(), 'prevdata')).", \n";
  284. $sDocView .= Dict::Format('UI:DownloadDocument_', $oPrevDoc->GetDownloadLink(get_class($this), $this->GetKey(), 'prevdata'))."\n";
  285. //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata');
  286. $sResult = Dict::Format('Change:AttName_Changed_PreviousValue_OldValue', $sAttName, $sDocView);
  287. }
  288. return $sResult;
  289. }
  290. }
  291. /**
  292. * Safely record the modification of one way encrypted password
  293. */
  294. class CMDBChangeOpSetAttributeOneWayPassword extends CMDBChangeOpSetAttribute
  295. {
  296. public static function Init()
  297. {
  298. $aParams = array
  299. (
  300. "category" => "core/cmdb",
  301. "key_type" => "",
  302. "name_attcode" => "change",
  303. "state_attcode" => "",
  304. "reconc_keys" => array(),
  305. "db_table" => "priv_changeop_setatt_pwd",
  306. "db_key_field" => "id",
  307. "db_finalclass_field" => "",
  308. );
  309. MetaModel::Init_Params($aParams);
  310. MetaModel::Init_InheritAttributes();
  311. MetaModel::Init_AddAttribute(new AttributeOneWayPassword("prev_pwd", array("sql" => 'data', "default_value" => '', "is_null_allowed"=> true, "allowed_values" => null, "depends_on"=>array())));
  312. // Display lists
  313. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  314. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  315. }
  316. /**
  317. * Describe (as a text string) the modifications corresponding to this change
  318. */
  319. public function GetDescription()
  320. {
  321. // Temporary, until we change the options of GetDescription() -needs a more global revision
  322. $bIsHtml = true;
  323. $sResult = '';
  324. $oTargetObjectClass = $this->Get('objclass');
  325. $oTargetObjectKey = $this->Get('objkey');
  326. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  327. $oTargetSearch->AddCondition('id', $oTargetObjectKey, '=');
  328. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  329. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  330. {
  331. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  332. $sAttName = $oAttDef->GetLabel();
  333. $sResult = Dict::Format('Change:AttName_Changed', $sAttName);
  334. }
  335. return $sResult;
  336. }
  337. }
  338. /**
  339. * Safely record the modification of an encrypted field
  340. */
  341. class CMDBChangeOpSetAttributeEncrypted extends CMDBChangeOpSetAttribute
  342. {
  343. public static function Init()
  344. {
  345. $aParams = array
  346. (
  347. "category" => "core/cmdb",
  348. "key_type" => "",
  349. "name_attcode" => "change",
  350. "state_attcode" => "",
  351. "reconc_keys" => array(),
  352. "db_table" => "priv_changeop_setatt_encrypted",
  353. "db_key_field" => "id",
  354. "db_finalclass_field" => "",
  355. );
  356. MetaModel::Init_Params($aParams);
  357. MetaModel::Init_InheritAttributes();
  358. MetaModel::Init_AddAttribute(new AttributeEncryptedString("prevstring", array("sql" => 'data', "default_value" => '', "is_null_allowed"=> true, "allowed_values" => null, "depends_on"=>array())));
  359. // Display lists
  360. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  361. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  362. }
  363. /**
  364. * Describe (as a text string) the modifications corresponding to this change
  365. */
  366. public function GetDescription()
  367. {
  368. // Temporary, until we change the options of GetDescription() -needs a more global revision
  369. $bIsHtml = true;
  370. $sResult = '';
  371. $oTargetObjectClass = $this->Get('objclass');
  372. $oTargetObjectKey = $this->Get('objkey');
  373. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  374. $oTargetSearch->AddCondition('id', $oTargetObjectKey, '=');
  375. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  376. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  377. {
  378. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  379. $sAttName = $oAttDef->GetLabel();
  380. $sPrevString = $this->Get('prevstring');
  381. $sResult = Dict::Format('Change:AttName_Changed_PreviousValue_OldValue', $sAttName, $sPrevString);
  382. }
  383. return $sResult;
  384. }
  385. }
  386. /**
  387. * Record the modification of a multiline string (text)
  388. *
  389. * @package iTopORM
  390. */
  391. class CMDBChangeOpSetAttributeText extends CMDBChangeOpSetAttribute
  392. {
  393. public static function Init()
  394. {
  395. $aParams = array
  396. (
  397. "category" => "core/cmdb",
  398. "key_type" => "",
  399. "name_attcode" => "change",
  400. "state_attcode" => "",
  401. "reconc_keys" => array(),
  402. "db_table" => "priv_changeop_setatt_text",
  403. "db_key_field" => "id",
  404. "db_finalclass_field" => "",
  405. );
  406. MetaModel::Init_Params($aParams);
  407. MetaModel::Init_InheritAttributes();
  408. MetaModel::Init_AddAttribute(new AttributeText("prevdata", array("allowed_values"=>null, "sql"=>"prevdata", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  409. // Display lists
  410. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  411. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  412. }
  413. /**
  414. * Describe (as a text string) the modifications corresponding to this change
  415. */
  416. public function GetDescription()
  417. {
  418. // Temporary, until we change the options of GetDescription() -needs a more global revision
  419. $bIsHtml = true;
  420. $sResult = '';
  421. $oTargetObjectClass = $this->Get('objclass');
  422. $oTargetObjectKey = $this->Get('objkey');
  423. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  424. $oTargetSearch->AddCondition('id', $oTargetObjectKey, '=');
  425. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  426. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  427. {
  428. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  429. $sAttName = $oAttDef->GetLabel();
  430. $sTextView = '<div>'.$this->GetAsHtml('prevdata').'</div>';
  431. //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata');
  432. $sResult = Dict::Format('Change:AttName_Changed_PreviousValue_OldValue', $sAttName, $sTextView);
  433. }
  434. return $sResult;
  435. }
  436. }
  437. ?>