AclReinstall.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  102. // 'namespace' => "{$item['namespace']}/{$fieldName}",
  103. // 'objectNamespace' => $item['namespace'],
  104. // 'idDatabase' => $item['idDatabase'],
  105. // '_rootTableName' => $item['_rootTableName'],
  106. // 'fieldNamespace' => $fieldName,
  107. // 'xsdType' => $x['type'],
  108. // 'xsdRestrictions' => json_encode($x['restrictions']),
  109. // 'appInfo' => json_encode($x['appInfo']),
  110. // 'minOccurs' => $x['minOccurs'],
  111. // 'maxOccurs' => $x['maxOccurs'],
  112. // 'isActive' => 1
  113. // ]);
  114. if (!empty($listEnum)) {
  115. DBG::nicePrint($listEnum, "\$listEnum for field '{$fieldName}'");
  116. foreach ($listEnum as $value => $label) {
  117. // DB::getPDO()->insertOrUpdate("{$this->_rootTableName}_enum", [
  118. // 'namespace' => "{$item['namespace']}/{$fieldName}/@{$value}",
  119. // 'fieldNamespace' => $fieldName,
  120. // 'objectNamespace' => $item['namespace'],
  121. // 'value' => $value,
  122. // 'label' => $label,
  123. // 'isActive' => 1
  124. // ]);
  125. }
  126. }
  127. }
  128. throw new Exception("TODO: printReinstallAntAclPreview...");
  129. }
  130. public function printReinstallTableAclPreview($item) {
  131. throw new Exception("TODO: printReinstallTableAclPreview...");
  132. }
  133. }