AclReinstall.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Router');
  4. Lib::loadClass('Response');
  5. Lib::loadClass('UI');
  6. Lib::loadClass('SchemaFactory');
  7. Lib::loadClass('RefConfig');
  8. Lib::loadClass('Type_Field');
  9. class Route_Storage_AclReinstall extends RouteBase {
  10. public function handleAuth() {
  11. if (!User::logged()) {
  12. User::authByRequest();
  13. }
  14. }
  15. public function defaultAction() {
  16. UI::gora();
  17. UI::startContainer();
  18. try {
  19. $namespace = V::get('namespace', '', $_GET);
  20. if (empty($namespace)) throw new Exception("Missing param namespace");
  21. echo UI::h('h3', [], $namespace);
  22. echo UI::h('p', [], [
  23. UI::h('a', [
  24. 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'namespace' => $namespace ]),
  25. 'class' => "btn btn-md btn-link",
  26. ], "<i class=\"glyphicon glyphicon-arrow-left\"></i> Wróć do struktury"),
  27. " | ",
  28. UI::h('a', [
  29. 'href' => $this->getLink('viewXsdSource', [ 'namespace' => $namespace ]),
  30. 'class' => "btn btn-md btn-link",
  31. 'target' => "_blank",
  32. ], "Otwórz plik xsd (źródłowy)"),
  33. ]);
  34. if ('reinstall' == V::get('_postTask', '', $_POST)) {
  35. $this->reinstallAcl($namespace);
  36. return;
  37. }
  38. echo UI::hButtonPost("Reinstall", [
  39. 'data' => [
  40. '_postTask' => 'reinstall'
  41. ],
  42. 'class' => 'btn btn-md btn-danger',
  43. 'title' => "Reinstall structure"
  44. ]);
  45. echo '<hr>';
  46. try {
  47. $this->printReinstallPreview($namespace);
  48. } catch (Exception $e) {
  49. DBG::log($e);
  50. UI::alert('danger', $e->getMessage());
  51. }
  52. } catch (Exception $e) {
  53. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  54. DBG::log($e);
  55. }
  56. UI::endContainer();
  57. UI::dol();
  58. }
  59. public function printReinstallPreview($namespace) {
  60. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => "*,field" ]);
  61. DBG::log($objectItem, 'array', '$objectItem preview');
  62. switch ($objectItem['_type']) {
  63. case 'AntAcl': $this->printReinstallAntAclPreview($objectItem); break;
  64. case 'TableAcl': $this->printReinstallTableAclPreview($objectItem); break;
  65. case 'StorageAcl': $this->printReinstallStorageAclPreview($objectItem); break;
  66. default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
  67. }
  68. }
  69. public function printReinstallAntAclPreview($item) {
  70. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  71. $antAclPath = Schema_SystemObjectFieldStorageAcl::getAntAclXsdBasePath($item['typeName']);
  72. if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
  73. Lib::loadClass('XML');
  74. $xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
  75. DBG::nicePrint($item, '$item');
  76. $sortPrio = 0;
  77. foreach ($xsdType['struct'] as $fieldName => $fieldItem) {
  78. $xsdType['struct'][$fieldName]['sortPrio'] = $sortPrio++;
  79. }
  80. DBG::nicePrint($xsdType, '$xsdType');
  81. // TODO: fix sortPrio for all fields if not set = 0
  82. echo '<hr>';
  83. echo UI::h('h3', [], "Lista zmian:");
  84. echo ($item['primaryKey'] != $xsdType['primaryKey'])
  85. ? UI::h('p', [ 'style' => "" ], "@primaryKey - zmiana z '{$item['primaryKey']}' na '{$xsdType['primaryKey']}'")
  86. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "@primaryKey - bez zmian");
  87. if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
  88. foreach ($xsdType['struct'] as $fieldName => $x) {
  89. $listEnum = [];
  90. if (!empty($x['restrictions']['enumeration'])) {
  91. $listEnum = $x['restrictions']['enumeration'];
  92. unset($x['restrictions']['enumeration']);
  93. }
  94. if (!empty($listEnum)) {
  95. DBG::log($listEnum, 'array', "\$listEnum for field '{$fieldName}'");
  96. }
  97. }
  98. $old = [
  99. 'fields' => array_map(function ($field) { return $field['fieldNamespace']; }, $item['field']),
  100. ];
  101. $new = [
  102. 'fields' => array_keys($xsdType['struct']),
  103. ];
  104. sort($old['fields']);
  105. sort($new['fields']);
  106. $diffFieldsToCreate = array_diff($new['fields'], $old['fields']);
  107. $diffFieldsToRemove = array_diff($old['fields'], $new['fields']);
  108. $sameFields = array_intersect($new['fields'], $old['fields']);
  109. echo (!empty($diffFieldsToCreate))
  110. ? UI::h('details', [ 'open' => "open" ], [
  111. UI::h('summary', [], "Pola do dodania (".count($diffFieldsToCreate)."):"),
  112. UI::h('ul', [], array_map(function ($fieldName) {
  113. return UI::h('li', [], $fieldName);
  114. }, $diffFieldsToCreate)),
  115. ])
  116. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Brak pól do dodania");
  117. echo (!empty($diffFieldsToRemove))
  118. ? UI::h('details', [ 'open' => "open", 'style' => "margin:4px 0; color:#8a6d3b; background-color:#fcf8e3; border:1px solid #faebcc;" ], [
  119. UI::h('summary', [ 'style' => "padding:4px; outline:none; cursor:pointer" ], "Pola do usunięcia (".count($diffFieldsToRemove)."):"),
  120. UI::h('ul', [], array_map(function ($fieldName) {
  121. return UI::h('li', [], $fieldName);
  122. }, $diffFieldsToRemove)),
  123. ])
  124. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Brak pól do usunięcia");
  125. foreach ($sameFields as $fieldName) {
  126. // UI::alert('warning', "TODO: is field changed? '{$fieldName}'");
  127. $oldField = array_filter($item['field'], function ($field) use ($fieldName) { return ( $fieldName === $field['fieldNamespace'] ); });
  128. $oldField = ($oldField) ? reset($oldField) : null;
  129. // DBG::nicePrint($oldField, "\$oldField '$fieldName'");
  130. $newField = $xsdType['struct'][$fieldName];
  131. // DBG::nicePrint($newField, "\$newField '$fieldName'");
  132. $fieldDiff = [];
  133. if ($newField['type'] !== $oldField['xsdType']) $fieldDiff[] = 'xsdType';
  134. if ($newField['minOccurs'] != $oldField['minOccurs']) $fieldDiff[] = 'minOccurs';
  135. if ($newField['maxOccurs'] != $oldField['maxOccurs']) $fieldDiff[] = 'maxOccurs';
  136. if (json_encode($newField['restrictions']) !== $oldField['xsdRestrictions']) $fieldDiff[] = 'xsdRestrictions';
  137. if (json_encode($newField['appInfo']) !== $oldField['appInfo']) $fieldDiff[] = 'appInfo';
  138. if (RefConfig::isRefField($item['namespace'], $fieldName)) {
  139. $typeNewField = Type_Field::build($newField);
  140. $typeOldField = Type_Field::build($oldField);
  141. if (RefConfig::needUpdate($item['namespace'], $oldField['fieldNamespace'], $typeNewField, $typeOldField)) {
  142. $fieldDiff[] = 'RefConfig';
  143. }
  144. }
  145. echo (!empty($fieldDiff))
  146. ? UI::h('p', [ 'style' => "" ], "Pole '{$fieldName}' - zmiany: " . implode(", ", $fieldDiff))
  147. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Pole '{$fieldName}' - bez zmian");
  148. }
  149. }
  150. public function printReinstallTableAclPreview($item) {
  151. throw new Exception("TODO: Podgląd zmian dla tabeli {$item['namespace']} ...");
  152. }
  153. public function printReinstallStorageAclPreview($item) {
  154. DBG::nicePrint($item, '$item');
  155. $acl = SchemaFactory::loadDefaultObject($item['name']);
  156. DBG::nicePrint($acl, '$acl');
  157. $xsdType = [
  158. 'primaryKey' => $acl->getPrimaryKeyField(),
  159. 'struct' => $acl->getFieldsWithXsdTypes()
  160. ];
  161. DBG::nicePrint($xsdType, '$xsdType');
  162. echo '<hr>';
  163. echo UI::h('h3', [], "Lista zmian:");
  164. echo ($item['primaryKey'] != $xsdType['primaryKey'])
  165. ? UI::h('p', [ 'style' => "" ], "@primaryKey - zmiana z '{$item['primaryKey']}' na '{$xsdType['primaryKey']}'")
  166. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "@primaryKey - bez zmian");
  167. if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
  168. foreach ($xsdType['struct'] as $fieldName => $x) {
  169. $listEnum = [];
  170. if (!empty($x['restrictions']['enumeration'])) {
  171. $listEnum = $x['restrictions']['enumeration'];
  172. unset($x['restrictions']['enumeration']);
  173. }
  174. if (!empty($listEnum)) {
  175. DBG::log($listEnum, 'array', "\$listEnum for field '{$fieldName}'");
  176. }
  177. }
  178. $old = [
  179. 'fields' => array_map(function ($field) { return $field['fieldNamespace']; }, $item['field']),
  180. ];
  181. $new = [
  182. 'fields' => array_keys($xsdType['struct']),
  183. ];
  184. sort($old['fields']);
  185. sort($new['fields']);
  186. $diffFieldsToCreate = array_diff($new['fields'], $old['fields']);
  187. $diffFieldsToRemove = array_diff($old['fields'], $new['fields']);
  188. $sameFields = array_intersect($new['fields'], $old['fields']);
  189. echo (!empty($diffFieldsToCreate))
  190. ? UI::h('details', [ 'open' => "open" ], [
  191. UI::h('summary', [], "Pola do dodania (".count($diffFieldsToCreate)."):"),
  192. UI::h('ul', [], array_map(function ($fieldName) {
  193. return UI::h('li', [], $fieldName);
  194. }, $diffFieldsToCreate)),
  195. ])
  196. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Brak pól do dodania");
  197. echo (!empty($diffFieldsToRemove))
  198. ? UI::h('details', [ 'open' => "open", 'style' => "margin:4px 0; color:#8a6d3b; background-color:#fcf8e3; border:1px solid #faebcc;" ], [
  199. UI::h('summary', [ 'style' => "padding:4px; outline:none; cursor:pointer" ], "Pola do usunięcia (".count($diffFieldsToRemove)."):"),
  200. UI::h('ul', [], array_map(function ($fieldName) {
  201. return UI::h('li', [], $fieldName);
  202. }, $diffFieldsToRemove)),
  203. ])
  204. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Brak pól do usunięcia");
  205. foreach ($sameFields as $fieldName) {
  206. // UI::alert('warning', "TODO: is field changed? '{$fieldName}'");
  207. $oldField = array_filter($item['field'], function ($field) use ($fieldName) { return ( $fieldName === $field['fieldNamespace'] ); });
  208. $oldField = ($oldField) ? reset($oldField) : null;
  209. // DBG::nicePrint($oldField, "\$oldField '$fieldName'");
  210. $newField = $xsdType['struct'][$fieldName];
  211. // DBG::nicePrint($newField, "\$newField '$fieldName'");
  212. $fieldDiff = [];
  213. if ($newField['type'] !== $oldField['xsdType']) $fieldDiff[] = 'xsdType';
  214. if ($newField['minOccurs'] != $oldField['minOccurs']) $fieldDiff[] = 'minOccurs';
  215. if ($newField['maxOccurs'] != $oldField['maxOccurs']) $fieldDiff[] = 'maxOccurs';
  216. if (json_encode($newField['restrictions']) !== $oldField['xsdRestrictions']) $fieldDiff[] = 'xsdRestrictions';
  217. if (json_encode($newField['appInfo']) !== $oldField['appInfo']) $fieldDiff[] = 'appInfo';
  218. echo (!empty($fieldDiff))
  219. ? UI::h('p', [ 'style' => "" ], "Pole '{$fieldName}' - zmiany: " . implode(", ", $fieldDiff))
  220. : UI::h('p', [ 'style' => "font-style:italic; color:silver" ], "Pole '{$fieldName}' - bez zmian");
  221. }
  222. }
  223. public function viewXsdSourceAction() {
  224. try {
  225. $namespace = V::get('namespace', '', $_GET);
  226. if (empty($namespace)) throw new Exception("Missing param namespace");
  227. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => "*,field" ]);
  228. switch ($objectItem['_type']) {
  229. case 'AntAcl': $this->viewXsdSource($objectItem); break;
  230. // case 'TableAcl': $this->viewXsdSource($objectItem); break;
  231. default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
  232. }
  233. } catch (Exception $e) {
  234. DBG::log($e);
  235. echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage();
  236. }
  237. }
  238. function viewXsdSource($objectItem) {
  239. if (empty($objectItem)) throw new Exception("Missing objectItem in viewXsdSource");
  240. DBG::log($objectItem, "viewXsdSource \$objectItem");
  241. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  242. $antAclPath = Schema_SystemObjectFieldStorageAcl::getAntAclXsdBasePath($objectItem['typeName']);
  243. if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
  244. DBG::log(str_replace(APP_PATH_ROOT, '~', $antAclPath), "viewXsdSource \$antAclPath");
  245. $xsdFile = "{$antAclPath}/{$objectItem['name']}.xsd";
  246. if (!file_exists($xsdFile)) throw new Exception("Xsd file not exists");
  247. DBG::log(str_replace(APP_PATH_ROOT, '~', $xsdFile), "viewXsdSource \$xsdFile");
  248. header('Content-Type: application/xml; charset=utf-8');
  249. $fd = fopen($xsdFile, 'r');
  250. fpassthru($fd);
  251. exit;
  252. }
  253. function reinstallAcl($namespace) {
  254. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  255. $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
  256. $objFieldAcl->updateCache($namespace);
  257. try {
  258. $dbgInfo = [
  259. 'idInstance' => ACL::getInstanceId($namespace),
  260. // 'rootInstance' => InstanceConfig::getRootNamespace($namespace),
  261. // 'conf' => InstanceConfig::fetchInstanceConfig($namespace),
  262. ];
  263. DBG::nicePrint($dbgInfo, "dbg");
  264. } catch (Exception $e) {
  265. UI::alert('warning', $e->getMessage());
  266. }
  267. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  268. $childRefList = RefConfig::getChildRefFullList($namespace);
  269. DBG::nicePrint($childRefList, '$childRefList');
  270. DBG::nicePrint($item, '$item');
  271. $activeFields = array_filter($item['field'], function ($field) {
  272. return ($field['isActive'] > 0);
  273. });
  274. $fieldNsList = array_map(function ($field) {
  275. return $field['fieldNamespace'];
  276. }, $activeFields);
  277. DBG::nicePrint($fieldNsList, '$fieldNsList');
  278. if ('AntAcl' === $item['_type']) { // fix ref config status (turn on/off)
  279. foreach ($childRefList as $refConfig) { // [ namespace, A_STATUS ]
  280. if ($refConfig->status !== 'DELETED' && !in_array($refConfig->childName, $fieldNsList)) {
  281. UI::alert('danger', "remove ref config for '{$refConfig->childName}' ...");
  282. if (!$refConfig->id) throw new Exception("Missing ref config ID");
  283. RefConfig::remove($refConfig);
  284. }
  285. else if ($refConfig->status !== 'DELETED' && in_array($refConfig->childName, $fieldNsList)) {
  286. UI::alert('info', "ref config for '{$refConfig->childName}' active - OK");
  287. }
  288. else if ($refConfig->status === 'DELETED' && in_array($refConfig->childName, $fieldNsList)) {
  289. UI::alert('warning', "activate ref config for '{$refConfig->childName}' ...");
  290. if (!$refConfig->id) throw new Exception("Missing ref config ID");
  291. RefConfig::reactivate($refConfig);
  292. }
  293. else if ($refConfig->status === 'DELETED' && !in_array($refConfig->childName, $fieldNsList)) {
  294. UI::alert('info', "ref config for '{$refConfig->childName}' removed - OK");
  295. }
  296. else {
  297. UI::alert('danger', "Not implemented action for '{$refConfig->childName}'");
  298. }
  299. }
  300. // TODO: create missing refConfig - field is not in $childRefList
  301. }
  302. if ('AntAcl' === $item['_type']) { // fix ref tables by appInfo
  303. $activeRefFields = array_filter($item['field'], function ($field) {
  304. return ($field['isActive'] && 'ref:' === substr($field['xsdType'], 0, 4));
  305. });
  306. DBG::log($activeRefFields, 'array', "DBG \$activeRefFields");
  307. foreach ($activeRefFields as $field) {
  308. $typeNewField = Type_Field::build($field);
  309. // if (RefConfig::needUpdate($item['namespace'], $oldField['fieldNamespace'], $typeNewField, $typeOldField))
  310. try {
  311. RefConfig::update($item['namespace'], $field['fieldNamespace'], $typeNewField);
  312. } catch (Exception $e) {
  313. // TODO: deactivate RefConfig if error
  314. DBG::log($e);
  315. UI::alert('danger', $e->getMessage());
  316. }
  317. }
  318. }
  319. { // TODO: RMME? function is too slow for join query
  320. if ('AntAcl' === $item['_type']) {
  321. $idInstance = ACL::getInstanceId($namespace);
  322. $dbName = DB::getPDO()->getDatabaseName();
  323. DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$idInstance}` ");
  324. }
  325. }
  326. { // TODO: mv to updateCache
  327. if ('AntAcl' === $item['_type']) {
  328. $instConf = InstanceConfig::getInstanceConfig($namespace);
  329. InstanceConfig::createInstanceTable($instConf, $item); // force create instance table event if A_STATUS is NORMAL
  330. }
  331. }
  332. }
  333. }