AclUsage.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. ($acl instanceof AntAclBase)
  83. ? UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'idStorage' => $idDatabase, 'namespace' => $aclNamespace ]) ], "struktura obiektu (AntAcl)")
  84. : UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('tableStruct', [ 'idStorage' => $idDatabase, 'table' => $rootTableName ]) ], "struktura tabeli (TableAcl)")
  85. ]),
  86. UI::h('p', [], [
  87. "dodaj proces: ",
  88. UI::hButtonAjax("Dodaj podstawowy proces - read only (TODO)", 'addObjectBaseProcesAjax', [
  89. 'title' => "Dodaj podstawowy proces dla obiektu '{$namespace}' - read only (TODO)",
  90. 'class' => "btn btn-xs btn-default",
  91. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  92. 'data' => [ 'namespace' => $namespace ]
  93. ]),
  94. " ",
  95. UI::hButtonAjax("TODO edytor procesu", 'todoGotoProcesEditorAjax', [
  96. 'title' => "Otwórz edytor procesu dla obiektu '{$namespace}'",
  97. 'class' => "btn btn-xs btn-warning",
  98. 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
  99. 'data' => [ 'namespace' => $namespace ]
  100. ]),
  101. ]),
  102. ])
  103. ]);
  104. UI::hButtonAjaxOnResponse('addObjectBaseProcesAjax', /* payload, n */ "
  105. jQuery.notify(payload.msg, payload.type)
  106. ");
  107. UI::hButtonAjaxOnResponse('todoGotoProcesEditorAjax', /* payload, n */ "
  108. jQuery.notify('TODO: edytor procesu', 'error')
  109. ");
  110. if ($idTable > 0) {
  111. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  112. UI::startTag('details');
  113. echo UI::h('summary', ['style'=>"cursor:pointer; margin-bottom:12px"], "Wszystkie powiązania komórek z procesami - szczegóły");
  114. UI::table([
  115. 'cols_label' => $labelPermCols,
  116. 'cols_help' => $helpPermCols,
  117. 'rows' => array_map(function ($row) use ($renderProcesLink, $renderPermCell, $permCols) {
  118. $splitPos = (strlen($row['TABLE_DESCRIPTION']) > 20) ? strpos($row['TABLE_DESCRIPTION'], ' ', 20) : 20;
  119. if ($splitPos > 30) $splitPos = 20;
  120. $row['TABLE_DESCRIPTION'] = UI::h('details', [], [
  121. UI::h('summary', [ 'style' => "white-space:nowrap" ], substr($row['TABLE_DESCRIPTION'], 0, $splitPos)),
  122. UI::h('p', [], substr($row['TABLE_DESCRIPTION'], $splitPos)),
  123. ]);
  124. $row['ID_PROCES'] = $renderProcesLink($row['ID_PROCES']);
  125. foreach ($permCols as $colPerm) $row[ $colPerm ] = $renderPermCell( $row[$colPerm] );
  126. return $row;
  127. }, $aclTableRows)
  128. ]);
  129. UI::endTag('details');
  130. $csvIdProces = array();
  131. foreach ($aclTableRows as $row) {
  132. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  133. }
  134. }
  135. $databaseName = DB::getPDO( $idDatabase )->getDatabaseName();
  136. UI::table([
  137. 'caption' => "Powiązania komórek z procesem",
  138. 'cols_label' => $labelPermCols,
  139. 'cols_help' => $helpPermCols,
  140. 'rows' => array_map(
  141. function ($row) use ($aclTableRows, $idTable, $renderProcesLink, $renderPermCell, $permCols) {
  142. $fieldName = $row['COLUMN_NAME'];
  143. $item = [
  144. 'name' => $fieldName,
  145. 'typ' => UI::h('details', [], [
  146. UI::h('summary', ['style'=>"cursor:pointer"], $row['DATA_TYPE']),
  147. UI::h('p', [], $row['COLUMN_TYPE']),
  148. ]),
  149. 'w procesie' => [],
  150. 'idZasob' => 0,
  151. ];
  152. foreach ($permCols as $colPerm) $item[$colPerm] = '';
  153. foreach ($aclTableRows as $aclInfo) {
  154. if (strtolower($aclInfo['CELL_NAME']) == strtolower($row['COLUMN_NAME'])) {
  155. $item['w procesie'][] = $aclInfo['ID_PROCES'];
  156. $item['idZasob'] = $aclInfo['ID_CELL'];
  157. foreach ($permCols as $colPerm) $item[$colPerm] += $aclInfo[$colPerm];
  158. }
  159. }
  160. $item['w procesie'] = (empty($item['w procesie']))
  161. ? "<i style=\"color:red\">Brak</i>"
  162. : UI::h('span', ['style'=>"white-space:nowrap"], implode(", ", array_map($renderProcesLink, $item['w procesie'])));
  163. if (!$item['idZasob']) $item['idZasob'] = DB::getPDO()->fetchValue("select ID from CRM_LISTA_ZASOBOW where `DESC` = '{$fieldName}' and PARENT_ID = {$idTable} limit 1");
  164. $item['idZasob'] = ($item['idZasob']) ? $item['idZasob'] : UI::h('i', ['style'=>"color:silver"], "Brak");
  165. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  166. return $item;
  167. }, DB::getPDO()->fetchAll("
  168. select t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  169. from `information_schema`.`COLUMNS` t
  170. where t.TABLE_SCHEMA = '{$databaseName}'
  171. and t.TABLE_NAME like '{$rootTableName}'
  172. ")
  173. )
  174. ]);
  175. if (!empty($csvIdProces)) {
  176. $userLogin = User::getLogin();
  177. $csvIdProces = implode(",", $csvIdProces);
  178. UI::tag('h4', ['style'=>"margin-top:40px"], "Procesy dla '{$userLogin}': [{$csvIdProces}] <small><i>(z tabeli CRM_PROCES_idx_USER_to_PROCES_VIEW)</i></small>");
  179. $rows = DB::getPDO()->fetchAll("
  180. select ID_PROCES
  181. from `CRM_PROCES_idx_USER_to_PROCES_VIEW`
  182. where ADM_ACCOUNT = '{$userLogin}'
  183. and ID_PROCES in({$csvIdProces})
  184. group by ID_PROCES
  185. ");
  186. if (empty($rows)) UI::alert('warning', "Brak danych");
  187. $userIdProces = array_map( V::makePick('ID_PROCES', 0, 'int'), $rows );
  188. $userTablePerms = array_reduce($aclTableRows, function ($ret, $row) use ($userIdProces, $permCols) {
  189. $idProces = (int)$row['ID_PROCES'];
  190. if (!$idProces) return $ret;
  191. if (!in_array($idProces, $userIdProces)) return $ret;
  192. if (!array_key_exists($row['CELL_NAME'], $ret)) {
  193. $ret[ $row['CELL_NAME'] ] = [
  194. 'fieldName' => $row['CELL_NAME'],
  195. 'idZasob' => $row['ID_CELL'],
  196. 'w procesie' => [],
  197. ];
  198. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] = '';
  199. }
  200. $ret[ $row['CELL_NAME'] ]['w procesie'] []= $row['ID_PROCES'];
  201. foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] += $row[ $colPerm ];
  202. return $ret;
  203. }, []);
  204. UI::table([
  205. 'caption' => "Uprawniena dla usera '{$userLogin}'",
  206. 'cols_label' => $labelPermCols,
  207. 'cols_help' => $helpPermCols,
  208. 'rows' => array_map(function ($item) use ($renderProcesLink, $renderPermCell, $permCols) {
  209. sort($item['w procesie']);
  210. $item['w procesie'] = array_unique($item['w procesie'], SORT_NUMERIC);
  211. $item['w procesie'] = implode(", ", array_map($renderProcesLink, $item['w procesie']));
  212. foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
  213. return $item;
  214. }, $userTablePerms),
  215. ]);
  216. } else UI::alert('warning', "Brak przypisanych procesów");
  217. } catch (Exception $e) {
  218. UI::alert('danger', $e->getMessage());
  219. DBG::log($e);
  220. }
  221. UI::endContainer();
  222. UI::dol();
  223. }
  224. }