AclReinstall.php 6.4 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. echo UI::hButtonPost("Reinstall", [
  28. 'data' => [
  29. '_postTask' => 'reinstall'
  30. ],
  31. 'class' => 'btn btn-md btn-danger',
  32. 'title' => "Reinstall structure"
  33. ]);
  34. echo '<hr>';
  35. try {
  36. $this->printReinstallPreview($namespace);
  37. } catch (Exception $e) {
  38. DBG::log($e);
  39. UI::alert('danger', $e->getMessage());
  40. }
  41. return;
  42. }
  43. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  44. $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
  45. $objFieldAcl->updateCache($namespace);
  46. DBG::nicePrint([
  47. 'idInstance' => ACL::getInstanceId($namespace),
  48. 'rootInstance' => ACL::getRootNamespace($namespace),
  49. 'conf' => ACL::fetchInstanceConfig($namespace),
  50. 'table' => ACL::getInstanceTable($namespace),
  51. ], "dbg");
  52. {
  53. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  54. if ('AntAcl' === $item['_type']) {
  55. $dbName = DB::getPDO()->getDatabaseName();
  56. $sqlFunBody = ACL::generateIsInstanceFunctionBody($namespace, $item);
  57. DBG::nicePrint($sqlFunBody, "\$sqlFunBody");
  58. DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$namespace}` ");
  59. // CREATE
  60. // [DEFINER = { user | CURRENT_USER }]
  61. // FUNCTION sp_name ([func_parameter[,...]])
  62. // RETURNS type
  63. // [characteristic ...] routine_body
  64. DB::getPDO()->execSql("
  65. CREATE DEFINER=`root`@`localhost`
  66. FUNCTION `{$dbName}`.`isInstance_{$namespace}` ( pk INT(11) )
  67. RETURNS TINYINT(1)
  68. {$sqlFunBody}
  69. ");
  70. }
  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', [], [
  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', [], [
  126. UI::h('summary', [], "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. }