AclUsage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  24. $rootTableName = $acl->getRootTableName();
  25. $idTable = $acl->getID();
  26. $renderProcesLink = function ($idProces) {
  27. return UI::h('a', [
  28. 'href' => "procesy5.php?task=CRM_PROCES&filtr_id={$idProces}",
  29. 'class' => "btn btn-xs btn-link", 'style' => "padding:0",
  30. ], "{$idProces}");
  31. };
  32. $renderPermCell = function ($value) {
  33. return ($value)
  34. ? UI::h('span', [ 'class' => "label label-success" ], $value)
  35. : UI::h('span', [ 'class' => "label label-danger" ], $value);
  36. };
  37. $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ];
  38. $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' ];
  39. $helpPermCols = [
  40. 'PERM_R' => 'Odczyt',
  41. 'PERM_W' => 'Zapis',
  42. 'PERM_X' => 'Wykonanie',
  43. 'PERM_C' => 'Tworzenie',
  44. 'PERM_S' => 'Zapis pomimo braku uprawnień do rekordu',
  45. 'PERM_O' => 'Tylko własne dane',
  46. 'PERM_V' => 'Odczyt pomimo braku uprawnień do rekordu',
  47. 'PERM_E' => 'Export',
  48. ];
  49. echo UI::h('details', ['style'=>"margin-bottom:24px; padding:0 10px; background-color:#eee", 'open' => "open"], [
  50. UI::h('summary', ['style'=>"font-size:1.4em; line-height:2em; cursor:pointer; outline:none"], [
  51. "Uprawnienia obiektu '{$namespace}' ",
  52. // UI::h('small', ['style'=>"font-size:0.8em; font-style:italic; color:#aaa"], " więcej...")
  53. ]),
  54. UI::h('div', ['style'=>"padding:4px 24px; border-top:1px solid #fff"], [
  55. UI::h('p', [], "tabela: '{$rootTableName}'"),
  56. UI::h('p', [], "id zasobu: [{$idTable}]"),
  57. UI::h('p', [], [
  58. "struktura: ",
  59. ($acl instanceof AntAclBase)
  60. ? UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'idStorage' => $acl->getDatabaseID(), 'namespace' => $acl->getNamespace() ]) ], "struktura obiektu (AntAcl)")
  61. : UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('tableStruct', [ 'idStorage' => $acl->getDatabaseID(), 'table' => $acl->getRootTableName() ]) ], "struktura tabeli (TableAcl)")
  62. ]),
  63. UI::h('p', [], [
  64. "dodaj proces: ",
  65. UI::hButtonAjax("Dodaj podstawowy proces - read only (TODO)", 'addObjectBaseProcesAjax', [
  66. 'title' => "Dodaj podstawowy proces dla obiektu '{$namespace}' - read only (TODO)",
  67. 'class' => "btn btn-xs btn-default",
  68. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  69. 'data' => [ 'namespace' => $namespace ]
  70. ]),
  71. " ",
  72. UI::hButtonAjax("TODO edytor procesu", 'todoGotoProcesEditorAjax', [
  73. 'title' => "Otwórz edytor procesu dla obiektu '{$namespace}'",
  74. 'class' => "btn btn-xs btn-warning",
  75. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  76. 'data' => [ 'namespace' => $namespace ]
  77. ]),
  78. ]),
  79. ])
  80. ]);
  81. UI::hButtonAjaxOnResponse('addObjectBaseProcesAjax', /* payload, n */ "
  82. jQuery.notify(payload.msg, payload.type)
  83. ");
  84. UI::hButtonAjaxOnResponse('todoGotoProcesEditorAjax', /* payload, n */ "
  85. jQuery.notify('TODO: edytor procesu', 'error')
  86. ");
  87. if ($idTable > 0) {
  88. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  89. UI::startTag('details');
  90. echo UI::h('summary', ['style'=>"cursor:pointer; margin-bottom:12px"], "Wszystkie powiązania komórek z procesami - szczegóły");
  91. UI::table([
  92. 'cols_label' => $labelPermCols,
  93. 'cols_help' => $helpPermCols,
  94. 'rows' => array_map(function ($row) use ($renderProcesLink, $renderPermCell, $permCols) {
  95. $splitPos = (strlen($row['TABLE_DESCRIPTION']) > 20) ? strpos($row['TABLE_DESCRIPTION'], ' ', 20) : 20;
  96. if ($splitPos > 30) $splitPos = 20;
  97. $row['TABLE_DESCRIPTION'] = UI::h('details', [], [
  98. UI::h('summary', [ 'style' => "white-space:nowrap" ], substr($row['TABLE_DESCRIPTION'], 0, $splitPos)),
  99. UI::h('p', [], substr($row['TABLE_DESCRIPTION'], $splitPos)),
  100. ]);
  101. $row['ID_PROCES'] = $renderProcesLink($row['ID_PROCES']);
  102. foreach ($permCols as $colPerm) $row[ $colPerm ] = $renderPermCell( $row[$colPerm] );
  103. return $row;
  104. }, $aclTableRows)
  105. ]);
  106. UI::endTag('details');
  107. $csvIdProces = array();
  108. foreach ($aclTableRows as $row) {
  109. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  110. }
  111. }
  112. $databaseName = DB::getPDO( $acl->getDatabaseID() )->getDatabaseName();
  113. UI::table([
  114. 'caption' => "Powiązania komórek z procesem",
  115. 'cols_label' => $labelPermCols,
  116. 'cols_help' => $helpPermCols,
  117. 'rows' => array_map(
  118. function ($row) use ($aclTableRows, $idTable, $renderProcesLink, $renderPermCell, $permCols) {
  119. $fieldName = $row['COLUMN_NAME'];
  120. $item = [
  121. 'name' => $fieldName,
  122. 'typ' => UI::h('details', [], [
  123. UI::h('summary', ['style'=>"cursor:pointer"], $row['DATA_TYPE']),
  124. UI::h('p', [], $row['COLUMN_TYPE']),
  125. ]),
  126. 'w procesie' => [],
  127. 'idZasob' => 0,
  128. ];
  129. foreach ($permCols as $colPerm) $item[$colPerm] = '';
  130. foreach ($aclTableRows as $aclInfo) {
  131. if (strtolower($aclInfo['CELL_NAME']) == strtolower($row['COLUMN_NAME'])) {
  132. $item['w procesie'][] = $aclInfo['ID_PROCES'];
  133. $item['idZasob'] = $aclInfo['ID_CELL'];
  134. foreach ($permCols as $colPerm) $item[$colPerm] += $aclInfo[$colPerm];
  135. }
  136. }
  137. $item['w procesie'] = (empty($item['w procesie']))
  138. ? "<i style=\"color:red\">Brak</i>"
  139. : UI::h('span', ['style'=>"white-space:nowrap"], implode(", ", array_map($renderProcesLink, $item['w procesie'])));
  140. if (!$item['idZasob']) $item['idZasob'] = DB::getPDO()->fetchValue("select ID from CRM_LISTA_ZASOBOW where `DESC` = '{$fieldName}' and PARENT_ID = {$idTable} limit 1");
  141. $item['idZasob'] = ($item['idZasob']) ? $item['idZasob'] : UI::h('i', ['style'=>"color:silver"], "Brak");
  142. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  143. return $item;
  144. }, DB::getPDO()->fetchAll("
  145. select t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  146. from `information_schema`.`COLUMNS` t
  147. where t.TABLE_SCHEMA = '{$databaseName}'
  148. and t.TABLE_NAME like '{$rootTableName}'
  149. ")
  150. )
  151. ]);
  152. if (!empty($csvIdProces)) {
  153. $userLogin = User::getLogin();
  154. $csvIdProces = implode(",", $csvIdProces);
  155. UI::tag('h4', ['style'=>"margin-top:40px"], "Procesy dla '{$userLogin}': [{$csvIdProces}] <small><i>(z tabeli CRM_PROCES_idx_USER_to_PROCES_VIEW)</i></small>");
  156. $rows = DB::getPDO()->fetchAll("
  157. select ID_PROCES
  158. from `CRM_PROCES_idx_USER_to_PROCES_VIEW`
  159. where ADM_ACCOUNT = '{$userLogin}'
  160. and ID_PROCES in({$csvIdProces})
  161. group by ID_PROCES
  162. ");
  163. $userIdProces = array_map( V::makePick('ID_PROCES', 0, 'int'), $rows );
  164. $userTablePerms = array_reduce($aclTableRows, function ($ret, $row) use ($userIdProces, $permCols) {
  165. $idProces = (int)$row['ID_PROCES'];
  166. if (!$idProces) return $ret;
  167. if (!in_array($idProces, $userIdProces)) return $ret;
  168. if (!array_key_exists($row['CELL_NAME'], $ret)) {
  169. $ret[ $row['CELL_NAME'] ] = [
  170. 'fieldName' => $row['CELL_NAME'],
  171. 'idZasob' => $row['ID_CELL'],
  172. 'w procesie' => [],
  173. ];
  174. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] = '';
  175. }
  176. $ret[ $row['CELL_NAME'] ]['w procesie'] []= $row['ID_PROCES'];
  177. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] += $row[ $colPerm ];
  178. return $ret;
  179. }, []);
  180. UI::table([
  181. 'caption' => "Uprawniena dla usera '{$userLogin}'",
  182. 'cols_label' => $labelPermCols,
  183. 'cols_help' => $helpPermCols,
  184. 'rows' => array_map(function ($item) use ($renderProcesLink, $renderPermCell, $permCols) {
  185. sort($item['w procesie']);
  186. $item['w procesie'] = array_unique($item['w procesie'], SORT_NUMERIC);
  187. $item['w procesie'] = implode(", ", array_map($renderProcesLink, $item['w procesie']));
  188. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  189. return $item;
  190. }, $userTablePerms),
  191. ]);
  192. } else UI::alert('warning', "Brak przypisanych procesów");
  193. } catch (Exception $e) {
  194. UI::alert('danger', $e->getMessage());
  195. DBG::log($e);
  196. }
  197. UI::endContainer();
  198. UI::dol();
  199. }
  200. }