AclUsage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Router');
  4. Lib::loadClass('Response');
  5. Lib::loadClass('UI');
  6. class Route_Storage_AclUsage extends RouteBase {
  7. public function handleAuth() {
  8. if (!User::logged()) {
  9. User::authByRequest();
  10. }
  11. }
  12. public function defaultAction() {
  13. UI::gora();
  14. UI::menu();
  15. UI::startContainer();
  16. try {
  17. $namespace = V::get('namespace', '', $_GET, 'word');
  18. if (!$namespace) {
  19. $typeName = V::get('typeName', '', $_GET, 'word');
  20. if (!$typeName) throw new Exception("Wrong param typeName");
  21. $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  22. }
  23. $aclNamespace = null;
  24. $aclXsdFieldTypes = null;
  25. $rootTableName = null;
  26. $idTable = null;
  27. $idDatabase = null;
  28. try {
  29. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  30. $rootTableName = $acl->getRootTableName();
  31. $idTable = $acl->getID();
  32. $aclNamespace = $acl->getNamespace();
  33. $idDatabase = $acl->getDatabaseID();
  34. // DBG::nicePrint($acl, 'Acl: $acl class('.get_class($acl).')');
  35. $aclXsdFieldTypes = $acl->getXsdTypes();
  36. // DBG::nicePrint($aclXsdFieldTypes, 'Acl: $aclXsdFieldTypes');
  37. } catch (Exception $e) {
  38. DBG::log($e);
  39. }
  40. if (!$aclNamespace) {
  41. try {
  42. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  43. $rootTableName = $item['_rootTableName'];
  44. $idTable = $item['idZasob'];
  45. $aclNamespace = $item['namespace'];
  46. $aclXsdFieldTypes = array_combine(
  47. array_map( V::makePick('fieldNamespace'), $item['field'] ),
  48. array_map( V::makePick('xsdType'), $item['field'] )
  49. );
  50. // DBG::nicePrint($aclXsdFieldTypes, 'SystemObject: $aclXsdFieldTypes');
  51. } catch (Exception $e) {
  52. DBG::log($e);
  53. }
  54. }
  55. if (!$aclNamespace) throw new Exception("Object not found '{$namespace}'");
  56. if (!$rootTableName) throw new Exception("Missing rootTableName");
  57. if (!$idTable) throw new Exception("Missing idTable");
  58. $renderProcesLink = function ($idProces) {
  59. return UI::h('a', [
  60. 'href' => "procesy5.php?task=CRM_PROCES&filtr_id={$idProces}",
  61. 'class' => "btn btn-xs btn-link", 'style' => "padding:0",
  62. ], "{$idProces}");
  63. };
  64. $renderPermCell = function ($value) {
  65. return ($value)
  66. ? UI::h('span', [ 'class' => "label label-success" ], (string)$value)
  67. : UI::h('span', [ 'class' => "label label-danger" ], (string)$value);
  68. };
  69. $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ];
  70. $labelPermCols = [ 'PERM_R' => 'R', 'PERM_W' => 'W', 'PERM_X' => 'X', 'PERM_C' => 'C', 'PERM_S' => 'S', 'PERM_O' => 'O', 'PERM_V' => 'V', 'PERM_E' => 'E' ];
  71. $helpPermCols = [
  72. 'PERM_R' => 'Odczyt',
  73. 'PERM_W' => 'Zapis',
  74. 'PERM_X' => 'Wykonanie',
  75. 'PERM_C' => 'Tworzenie',
  76. 'PERM_S' => 'Zapis pomimo braku uprawnień do rekordu',
  77. 'PERM_O' => 'Tylko własne dane',
  78. 'PERM_V' => 'Odczyt pomimo braku uprawnień do rekordu',
  79. 'PERM_E' => 'Export',
  80. ];
  81. echo UI::h('details', ['style'=>"margin-bottom:24px; padding:0 10px; background-color:#eee", 'open' => "open"], [
  82. UI::h('summary', ['style'=>"font-size:1.4em; line-height:2em; cursor:pointer; outline:none"], [
  83. "Uprawnienia obiektu '{$namespace}' ",
  84. // UI::h('small', ['style'=>"font-size:0.8em; font-style:italic; color:#aaa"], " więcej...")
  85. ]),
  86. UI::h('div', ['style'=>"padding:4px 24px; border-top:1px solid #fff"], [
  87. UI::h('p', [], "tabela: '{$rootTableName}'"),
  88. UI::h('p', [], "id zasobu: [{$idTable}]"),
  89. UI::h('p', [], [
  90. "struktura: ",
  91. UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'idStorage' => $idDatabase, 'namespace' => $aclNamespace ]) ], "struktura obiektu"),
  92. ]),
  93. UI::h('p', [], [
  94. "dodaj proces: ",
  95. UI::hButtonAjax("Dodaj podstawowy proces - read only (TODO)", 'addObjectBaseProcesAjax', [
  96. 'title' => "Dodaj podstawowy proces dla obiektu '{$namespace}' - read only (TODO)",
  97. 'class' => "btn btn-xs btn-default",
  98. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  99. 'data' => [ 'namespace' => $namespace ]
  100. ]),
  101. " ",
  102. UI::hButtonAjax("TODO edytor procesu", 'todoGotoProcesEditorAjax', [
  103. 'title' => "Otwórz edytor procesu dla obiektu '{$namespace}'",
  104. 'class' => "btn btn-xs btn-warning",
  105. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  106. 'data' => [ 'namespace' => $namespace ]
  107. ]),
  108. ]),
  109. ])
  110. ]);
  111. UI::hButtonAjaxOnResponse('addObjectBaseProcesAjax', /* payload, n */ "
  112. jQuery.notify(payload.msg, payload.type)
  113. ");
  114. UI::hButtonAjaxOnResponse('todoGotoProcesEditorAjax', /* payload, n */ "
  115. jQuery.notify('TODO: edytor procesu', 'error')
  116. ");
  117. if ($idTable > 0) {
  118. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  119. UI::startTag('details');
  120. echo UI::h('summary', ['style'=>"cursor:pointer; margin-bottom:12px"], "Wszystkie powiązania komórek z procesami - szczegóły");
  121. UI::table([
  122. 'cols_label' => $labelPermCols,
  123. 'cols_help' => $helpPermCols,
  124. 'rows' => array_map(function ($row) use ($renderProcesLink, $renderPermCell, $permCols) {
  125. $splitPos = (strlen($row['TABLE_DESCRIPTION']) > 20) ? strpos($row['TABLE_DESCRIPTION'], ' ', 20) : 20;
  126. if ($splitPos > 30) $splitPos = 20;
  127. $row['TABLE_DESCRIPTION'] = UI::h('details', [], [
  128. UI::h('summary', [ 'style' => "white-space:nowrap" ], substr($row['TABLE_DESCRIPTION'], 0, $splitPos)),
  129. UI::h('p', [], substr($row['TABLE_DESCRIPTION'], $splitPos)),
  130. ]);
  131. $row['ID_PROCES'] = $renderProcesLink($row['ID_PROCES']);
  132. foreach ($permCols as $colPerm) $row[ $colPerm ] = $renderPermCell( $row[$colPerm] );
  133. return $row;
  134. }, $aclTableRows)
  135. ]);
  136. UI::endTag('details');
  137. $csvIdProces = array();
  138. foreach ($aclTableRows as $row) {
  139. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  140. }
  141. }
  142. $databaseName = DB::getPDO( $idDatabase )->getDatabaseName();
  143. // DBG::nicePrint($aclXsdFieldTypes, 'DBG: $aclXsdFieldTypes');
  144. $aclXsdFieldTypes = array_map(function ($xsdType) {
  145. return array_merge([
  146. 'type' => $xsdType
  147. ], ('ref:' === substr($xsdType, 0, 4))
  148. ? [
  149. 'type' => 'ref',
  150. 'typeDesc' => $xsdType,
  151. ]
  152. : []
  153. );
  154. }, $aclXsdFieldTypes);
  155. $realDatabaseFields = DB::getPDO()->fetchAll("
  156. select t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  157. from `information_schema`.`COLUMNS` t
  158. where t.TABLE_SCHEMA = '{$databaseName}'
  159. and t.TABLE_NAME like '{$rootTableName}'
  160. ");
  161. // DBG::nicePrint($realDatabaseFields, 'DBG: $realDatabaseFields');
  162. foreach ($realDatabaseFields as $row) {
  163. $fieldName = $row['COLUMN_NAME'];
  164. if (!array_key_exists($fieldName, $aclXsdFieldTypes)) {
  165. $aclXsdFieldTypes[$fieldName] = [
  166. 'type' => "RAW:" . $row['DATA_TYPE'],
  167. ];
  168. }
  169. $aclXsdFieldTypes[$fieldName]['typeDesc'] = $row['COLUMN_TYPE'];
  170. }
  171. UI::table([
  172. 'caption' => "Powiązania komórek z procesem",
  173. 'cols_label' => $labelPermCols,
  174. 'cols_help' => $helpPermCols,
  175. 'rows' => array_map(
  176. function ($aclFieldType, $fieldName) use ($aclTableRows, $idTable, $renderProcesLink, $renderPermCell, $permCols) {
  177. $item = [
  178. 'name' => $fieldName,
  179. 'typ' => UI::h('details', [], [
  180. UI::h('summary', ['style'=>"cursor:pointer;outline:none"], $aclFieldType['type']),
  181. UI::h('p', [], $aclFieldType['typeDesc']),
  182. ]),
  183. 'w procesie' => [],
  184. 'idZasob' => 0,
  185. ];
  186. foreach ($permCols as $colPerm) $item[$colPerm] = '';
  187. foreach ($aclTableRows as $aclInfo) {
  188. if (strtolower($aclInfo['CELL_NAME']) == strtolower($fieldName)) {
  189. $item['w procesie'][] = $aclInfo['ID_PROCES'];
  190. $item['idZasob'] = $aclInfo['ID_CELL'];
  191. foreach ($permCols as $colPerm) $item[$colPerm] += $aclInfo[$colPerm];
  192. }
  193. }
  194. $item['w procesie'] = (empty($item['w procesie']))
  195. ? "<i style=\"color:red\">Brak</i>"
  196. : UI::h('span', ['style'=>"white-space:nowrap"], implode(", ", array_map($renderProcesLink, $item['w procesie'])));
  197. if (!$item['idZasob']) $item['idZasob'] = DB::getPDO()->fetchValue("select ID from CRM_LISTA_ZASOBOW where `DESC` = '{$fieldName}' and PARENT_ID = {$idTable} limit 1");
  198. $item['idZasob'] = ($item['idZasob']) ? $item['idZasob'] : UI::h('i', ['style'=>"color:silver"], "Brak");
  199. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  200. return $item;
  201. }, $aclXsdFieldTypes, array_keys($aclXsdFieldTypes)
  202. )
  203. ]);
  204. if (!empty($csvIdProces)) {
  205. $userLogin = User::getLogin();
  206. $csvIdProces = implode(",", $csvIdProces);
  207. UI::tag('h4', ['style'=>"margin-top:40px"], "Procesy dla '{$userLogin}': [{$csvIdProces}] <small><i>(z tabeli CRM_PROCES_idx_USER_to_PROCES_VIEW)</i></small>");
  208. $rows = DB::getPDO()->fetchAll("
  209. select ID_PROCES
  210. from `CRM_PROCES_idx_USER_to_PROCES_VIEW`
  211. where ADM_ACCOUNT = '{$userLogin}'
  212. and ID_PROCES in({$csvIdProces})
  213. group by ID_PROCES
  214. ");
  215. if (empty($rows)) UI::alert('warning', "Brak danych");
  216. $userIdProces = array_map( V::makePick('ID_PROCES', 0, 'int'), $rows );
  217. $userTablePerms = array_reduce($aclTableRows, function ($ret, $row) use ($userIdProces, $permCols) {
  218. $idProces = (int)$row['ID_PROCES'];
  219. if (!$idProces) return $ret;
  220. if (!in_array($idProces, $userIdProces)) return $ret;
  221. if (!array_key_exists($row['CELL_NAME'], $ret)) {
  222. $ret[ $row['CELL_NAME'] ] = [
  223. 'fieldName' => $row['CELL_NAME'],
  224. 'idZasob' => $row['ID_CELL'],
  225. 'w procesie' => [],
  226. ];
  227. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] = '';
  228. }
  229. $ret[ $row['CELL_NAME'] ]['w procesie'] []= $row['ID_PROCES'];
  230. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] += $row[ $colPerm ];
  231. return $ret;
  232. }, []);
  233. UI::table([
  234. 'caption' => "Uprawniena dla usera '{$userLogin}'",
  235. 'cols_label' => $labelPermCols,
  236. 'cols_help' => $helpPermCols,
  237. 'rows' => array_map(function ($item) use ($renderProcesLink, $renderPermCell, $permCols) {
  238. sort($item['w procesie']);
  239. $item['w procesie'] = array_unique($item['w procesie'], SORT_NUMERIC);
  240. $item['w procesie'] = implode(", ", array_map($renderProcesLink, $item['w procesie']));
  241. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  242. return $item;
  243. }, $userTablePerms),
  244. ]);
  245. } else UI::alert('warning', "Brak przypisanych procesów");
  246. } catch (Exception $e) {
  247. UI::alert('danger', $e->getMessage());
  248. DBG::log($e);
  249. }
  250. UI::endContainer();
  251. UI::dol();
  252. }
  253. }