AclUsage.php 9.5 KB

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