AclReinstall.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Router');
  4. Lib::loadClass('Response');
  5. Lib::loadClass('UI');
  6. Lib::loadClass('SchemaFactory');
  7. class Route_Storage_AclReinstall extends RouteBase {
  8. public function handleAuth() {
  9. if (!User::logged()) {
  10. User::authByRequest();
  11. }
  12. }
  13. public function defaultAction() {
  14. UI::gora();
  15. UI::startContainer();
  16. try {
  17. $namespace = V::get('namespace', '', $_GET);
  18. if (empty($namespace)) throw new Exception("Missing param namespace");
  19. echo UI::h('h3', [], $namespace);
  20. echo UI::h('p', [], [
  21. UI::h('a', [
  22. 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'namespace' => $namespace ]),
  23. 'class' => "btn btn-md btn-link",
  24. ], "<i class=\"glyphicon glyphicon-arrow-left\"></i> Wróć do struktury"),
  25. ]);
  26. if ('reinstall' == V::get('_postTask', '', $_POST)) {
  27. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  28. $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
  29. $objFieldAcl->updateCache($namespace);
  30. DBG::nicePrint([
  31. 'idInstance' => ACL::getInstanceId($namespace),
  32. 'rootInstance' => ACL::getRootNamespace($namespace),
  33. 'conf' => ACL::fetchInstanceConfig($namespace),
  34. 'table' => ACL::getInstanceTable($namespace),
  35. ], "dbg");
  36. {
  37. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  38. if ('AntAcl' === $item['_type']) {
  39. $dbName = DB::getPDO()->getDatabaseName();
  40. $sqlFunBody = ACL::generateIsInstanceFunctionBody($namespace, $item);
  41. DBG::nicePrint($sqlFunBody, "\$sqlFunBody");
  42. DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$namespace}` ");
  43. // CREATE
  44. // [DEFINER = { user | CURRENT_USER }]
  45. // FUNCTION sp_name ([func_parameter[,...]])
  46. // RETURNS type
  47. // [characteristic ...] routine_body
  48. DB::getPDO()->execSql("
  49. CREATE DEFINER=`root`@`localhost`
  50. FUNCTION `{$dbName}`.`isInstance_{$namespace}` ( pk INT(11) )
  51. RETURNS TINYINT(1)
  52. {$sqlFunBody}
  53. ");
  54. }
  55. }
  56. return;
  57. }
  58. echo UI::hButtonPost("Reinstall", [
  59. 'data' => [
  60. '_postTask' => 'reinstall'
  61. ],
  62. 'class' => 'btn btn-md btn-danger',
  63. 'title' => "Reinstall structure"
  64. ]);
  65. echo '<hr>';
  66. try {
  67. $this->printReinstallPreview($namespace);
  68. } catch (Exception $e) {
  69. DBG::log($e);
  70. UI::alert('danger', $e->getMessage());
  71. }
  72. } catch (Exception $e) {
  73. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  74. DBG::log($e);
  75. }
  76. UI::endContainer();
  77. UI::dol();
  78. }
  79. public function printReinstallPreview($namespace) {
  80. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => "*,field" ]);
  81. DBG::log($objectItem, 'array', '$objectItem preview');
  82. switch ($objectItem['_type']) {
  83. case 'AntAcl': $this->printReinstallAntAclPreview($objectItem); break;
  84. case 'TableAcl': $this->printReinstallTableAclPreview($objectItem); break;
  85. default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
  86. }
  87. }
  88. public function printReinstallAntAclPreview($item) {
  89. $antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']);
  90. if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
  91. Lib::loadClass('XML');
  92. $xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
  93. DBG::nicePrint($xsdType, '$xsdType');
  94. if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
  95. foreach ($xsdType['struct'] as $fieldName => $x) {
  96. $listEnum = [];
  97. if (!empty($x['restrictions']['enumeration'])) {
  98. $listEnum = $x['restrictions']['enumeration'];
  99. unset($x['restrictions']['enumeration']);
  100. }
  101. if (!empty($listEnum)) {
  102. DBG::log($listEnum, 'array', "\$listEnum for field '{$fieldName}'");
  103. }
  104. }
  105. $old = [
  106. 'fields' => array_map(function ($field) { return $field['fieldNamespace']; }, $item['field']),
  107. ];
  108. $new = [
  109. 'fields' => array_keys($xsdType['struct']),
  110. ];
  111. sort($old['fields']);
  112. sort($new['fields']);
  113. $diffFieldsToCreate = array_diff($new['fields'], $old['fields']);
  114. $diffFieldsToRemove = array_diff($old['fields'], $new['fields']);
  115. $sameFields = array_intersect($new['fields'], $old['fields']);
  116. echo (!empty($diffFieldsToCreate))
  117. ? UI::h('details', [ 'open' => "open" ], [
  118. UI::h('summary', [], "Pola do dodania (".count($diffFieldsToCreate)."):"),
  119. UI::h('ul', [], array_map(function ($fieldName) {
  120. return UI::h('li', [], $fieldName);
  121. }, $diffFieldsToCreate)),
  122. ])
  123. : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do dodania");
  124. echo (!empty($diffFieldsToRemove))
  125. ? UI::h('details', [ 'open' => "open", 'style' => "margin:4px 0; background-color:#ccc; border:1px solid #ddd" ], [
  126. UI::h('summary', [ 'style' => "padding:4px; outline:none; cursor:pointer" ], "Pola do usunięcia (".count($diffFieldsToRemove)."):"),
  127. UI::h('ul', [], array_map(function ($fieldName) {
  128. return UI::h('li', [], $fieldName);
  129. }, $diffFieldsToRemove)),
  130. ])
  131. : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do usunięcia");
  132. foreach ($sameFields as $fieldName) {
  133. // UI::alert('warning', "TODO: is field changed? '{$fieldName}'");
  134. $oldField = array_filter($item['field'], function ($field) use ($fieldName) { return ( $fieldName === $field['fieldNamespace'] ); });
  135. $oldField = ($oldField) ? reset($oldField) : null;
  136. // DBG::nicePrint($oldField, "\$oldField '$fieldName'");
  137. $newField = $xsdType['struct'][$fieldName];
  138. // DBG::nicePrint($newField, "\$newField '$fieldName'");
  139. $fieldDiff = [];
  140. if ($newField['type'] !== $oldField['xsdType']) $fieldDiff[] = 'xsdType';
  141. if ($newField['minOccurs'] != $oldField['minOccurs']) $fieldDiff[] = 'minOccurs';
  142. if ($newField['maxOccurs'] != $oldField['maxOccurs']) $fieldDiff[] = 'maxOccurs';
  143. if (json_encode($newField['restrictions']) !== $oldField['xsdRestrictions']) $fieldDiff[] = 'xsdRestrictions';
  144. if (json_encode($newField['appInfo']) !== $oldField['appInfo']) $fieldDiff[] = 'appInfo';
  145. echo (!empty($fieldDiff))
  146. ? UI::h('p', [ 'style' => "" ], "Pole '{$fieldName}' - zmiany: " . implode(", ", $fieldDiff))
  147. : UI::h('p', [ 'style' => "font-style:italic" ], "Pole '{$fieldName}' - bez zmian");
  148. }
  149. throw new Exception("TODO: Podgląd zmian dla AntAcl {$item['namespace']} ...");
  150. }
  151. public function printReinstallTableAclPreview($item) {
  152. throw new Exception("TODO: Podgląd zmian dla tabeli {$item['namespace']} ...");
  153. }
  154. }