AclReinstall.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. $idInstance = ACL::getInstanceId($namespace);
  43. DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$idInstance}` ");
  44. // CREATE
  45. // [DEFINER = { user | CURRENT_USER }]
  46. // FUNCTION sp_name ([func_parameter[,...]])
  47. // RETURNS type
  48. // [characteristic ...] routine_body
  49. DB::getPDO()->execSql("
  50. CREATE DEFINER=`root`@`localhost`
  51. FUNCTION `{$dbName}`.`isInstance_{$idInstance}` ( pk INT(11) )
  52. RETURNS TINYINT(1)
  53. {$sqlFunBody}
  54. ");
  55. }
  56. }
  57. return;
  58. }
  59. echo UI::hButtonPost("Reinstall", [
  60. 'data' => [
  61. '_postTask' => 'reinstall'
  62. ],
  63. 'class' => 'btn btn-md btn-danger',
  64. 'title' => "Reinstall structure"
  65. ]);
  66. echo '<hr>';
  67. try {
  68. $this->printReinstallPreview($namespace);
  69. } catch (Exception $e) {
  70. DBG::log($e);
  71. UI::alert('danger', $e->getMessage());
  72. }
  73. } catch (Exception $e) {
  74. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  75. DBG::log($e);
  76. }
  77. UI::endContainer();
  78. UI::dol();
  79. }
  80. public function printReinstallPreview($namespace) {
  81. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => "*,field" ]);
  82. DBG::log($objectItem, 'array', '$objectItem preview');
  83. switch ($objectItem['_type']) {
  84. case 'AntAcl': $this->printReinstallAntAclPreview($objectItem); break;
  85. case 'TableAcl': $this->printReinstallTableAclPreview($objectItem); break;
  86. default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
  87. }
  88. }
  89. public function printReinstallAntAclPreview($item) {
  90. $antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']);
  91. if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
  92. Lib::loadClass('XML');
  93. $xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
  94. DBG::nicePrint($xsdType, '$xsdType');
  95. if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
  96. foreach ($xsdType['struct'] as $fieldName => $x) {
  97. $listEnum = [];
  98. if (!empty($x['restrictions']['enumeration'])) {
  99. $listEnum = $x['restrictions']['enumeration'];
  100. unset($x['restrictions']['enumeration']);
  101. }
  102. if (!empty($listEnum)) {
  103. DBG::log($listEnum, 'array', "\$listEnum for field '{$fieldName}'");
  104. }
  105. }
  106. $old = [
  107. 'fields' => array_map(function ($field) { return $field['fieldNamespace']; }, $item['field']),
  108. ];
  109. $new = [
  110. 'fields' => array_keys($xsdType['struct']),
  111. ];
  112. sort($old['fields']);
  113. sort($new['fields']);
  114. $diffFieldsToCreate = array_diff($new['fields'], $old['fields']);
  115. $diffFieldsToRemove = array_diff($old['fields'], $new['fields']);
  116. $sameFields = array_intersect($new['fields'], $old['fields']);
  117. echo (!empty($diffFieldsToCreate))
  118. ? UI::h('details', [ 'open' => "open" ], [
  119. UI::h('summary', [], "Pola do dodania (".count($diffFieldsToCreate)."):"),
  120. UI::h('ul', [], array_map(function ($fieldName) {
  121. return UI::h('li', [], $fieldName);
  122. }, $diffFieldsToCreate)),
  123. ])
  124. : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do dodania");
  125. echo (!empty($diffFieldsToRemove))
  126. ? UI::h('details', [ 'open' => "open", 'style' => "margin:4px 0; background-color:#ccc; border:1px solid #ddd" ], [
  127. UI::h('summary', [ 'style' => "padding:4px; outline:none; cursor:pointer" ], "Pola do usunięcia (".count($diffFieldsToRemove)."):"),
  128. UI::h('ul', [], array_map(function ($fieldName) {
  129. return UI::h('li', [], $fieldName);
  130. }, $diffFieldsToRemove)),
  131. ])
  132. : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do usunięcia");
  133. foreach ($sameFields as $fieldName) {
  134. // UI::alert('warning', "TODO: is field changed? '{$fieldName}'");
  135. $oldField = array_filter($item['field'], function ($field) use ($fieldName) { return ( $fieldName === $field['fieldNamespace'] ); });
  136. $oldField = ($oldField) ? reset($oldField) : null;
  137. // DBG::nicePrint($oldField, "\$oldField '$fieldName'");
  138. $newField = $xsdType['struct'][$fieldName];
  139. // DBG::nicePrint($newField, "\$newField '$fieldName'");
  140. $fieldDiff = [];
  141. if ($newField['type'] !== $oldField['xsdType']) $fieldDiff[] = 'xsdType';
  142. if ($newField['minOccurs'] != $oldField['minOccurs']) $fieldDiff[] = 'minOccurs';
  143. if ($newField['maxOccurs'] != $oldField['maxOccurs']) $fieldDiff[] = 'maxOccurs';
  144. if (json_encode($newField['restrictions']) !== $oldField['xsdRestrictions']) $fieldDiff[] = 'xsdRestrictions';
  145. if (json_encode($newField['appInfo']) !== $oldField['appInfo']) $fieldDiff[] = 'appInfo';
  146. echo (!empty($fieldDiff))
  147. ? UI::h('p', [ 'style' => "" ], "Pole '{$fieldName}' - zmiany: " . implode(", ", $fieldDiff))
  148. : UI::h('p', [ 'style' => "font-style:italic" ], "Pole '{$fieldName}' - bez zmian");
  149. }
  150. throw new Exception("TODO: Podgląd zmian dla AntAcl {$item['namespace']} ...");
  151. }
  152. public function printReinstallTableAclPreview($item) {
  153. throw new Exception("TODO: Podgląd zmian dla tabeli {$item['namespace']} ...");
  154. }
  155. }