| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- Lib::loadClass('RouteBase');
- Lib::loadClass('Router');
- Lib::loadClass('Response');
- Lib::loadClass('UI');
- class Route_Storage_AclUsage extends RouteBase {
- public function handleAuth() {
- if (!User::logged()) {
- User::authByRequest();
- }
- }
- public function defaultAction() {
- UI::gora();
- UI::menu();
- UI::startContainer();
- try {
- $namespace = V::get('namespace', '', $_GET, 'word');
- if (!$namespace) {
- $typeName = V::get('typeName', '', $_GET, 'word');
- if (!$typeName) throw new Exception("Wrong param typeName");
- $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
- }
- $aclNamespace = null;
- $aclXsdFieldTypes = null;
- $rootTableName = null;
- $idTable = null;
- $idDatabase = null;
- try {
- $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
- $rootTableName = $acl->getRootTableName();
- $idTable = $acl->getID();
- $aclNamespace = $acl->getNamespace();
- $idDatabase = $acl->getDatabaseID();
- // DBG::nicePrint($acl, 'Acl: $acl class('.get_class($acl).')');
- $aclXsdFieldTypes = $acl->getXsdTypes();
- // DBG::nicePrint($aclXsdFieldTypes, 'Acl: $aclXsdFieldTypes');
- } catch (Exception $e) {
- DBG::log($e);
- }
- if (!$aclNamespace) {
- try {
- $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
- $rootTableName = $item['_rootTableName'];
- $idTable = $item['idZasob'];
- $aclNamespace = $item['namespace'];
- $aclXsdFieldTypes = array_combine(
- array_map( V::makePick('fieldNamespace'), $item['field'] ),
- array_map( V::makePick('xsdType'), $item['field'] )
- );
- // DBG::nicePrint($aclXsdFieldTypes, 'SystemObject: $aclXsdFieldTypes');
- } catch (Exception $e) {
- DBG::log($e);
- }
- }
- if (!$aclNamespace) throw new Exception("Object not found '{$namespace}'");
- if (!$rootTableName) throw new Exception("Missing rootTableName");
- if (!$idTable) throw new Exception("Missing idTable");
- $renderProcesLink = function ($idProces) {
- return UI::h('a', [
- 'href' => "procesy5.php?task=CRM_PROCES&filtr_id={$idProces}",
- 'class' => "btn btn-xs btn-link", 'style' => "padding:0",
- ], "{$idProces}");
- };
- $renderPermCell = function ($value) {
- return ($value)
- ? UI::h('span', [ 'class' => "label label-success" ], (string)$value)
- : UI::h('span', [ 'class' => "label label-danger" ], (string)$value);
- };
- $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ];
- $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' ];
- $helpPermCols = [
- 'PERM_R' => 'Odczyt',
- 'PERM_W' => 'Zapis',
- 'PERM_X' => 'Wykonanie',
- 'PERM_C' => 'Tworzenie',
- 'PERM_S' => 'Zapis pomimo braku uprawnień do rekordu',
- 'PERM_O' => 'Tylko własne dane',
- 'PERM_V' => 'Odczyt pomimo braku uprawnień do rekordu',
- 'PERM_E' => 'Export',
- ];
- echo UI::h('details', ['style'=>"margin-bottom:24px; padding:0 10px; background-color:#eee", 'open' => "open"], [
- UI::h('summary', ['style'=>"font-size:1.4em; line-height:2em; cursor:pointer; outline:none"], [
- "Uprawnienia obiektu '{$namespace}' ",
- // UI::h('small', ['style'=>"font-size:0.8em; font-style:italic; color:#aaa"], " więcej...")
- ]),
- UI::h('div', ['style'=>"padding:4px 24px; border-top:1px solid #fff"], [
- UI::h('p', [], "tabela: '{$rootTableName}'"),
- UI::h('p', [], "id zasobu: [{$idTable}]"),
- UI::h('p', [], [
- "struktura: ",
- UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'idStorage' => $idDatabase, 'namespace' => $aclNamespace ]) ], "struktura obiektu"),
- ]),
- UI::h('p', [], [
- "dodaj proces: ",
- UI::hButtonAjax("Dodaj podstawowy proces - read only (TODO)", 'addObjectBaseProcesAjax', [
- 'title' => "Dodaj podstawowy proces dla obiektu '{$namespace}' - read only (TODO)",
- 'class' => "btn btn-xs btn-default",
- 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
- 'data' => [ 'namespace' => $namespace ]
- ]),
- " ",
- UI::hButtonAjax("TODO edytor procesu", 'todoGotoProcesEditorAjax', [
- 'title' => "Otwórz edytor procesu dla obiektu '{$namespace}'",
- 'class' => "btn btn-xs btn-warning",
- 'href' => Router::getRoute('Storage')->getLink('addObjectBaseProcesAjax'),
- 'data' => [ 'namespace' => $namespace ]
- ]),
- ]),
- ])
- ]);
- UI::hButtonAjaxOnResponse('addObjectBaseProcesAjax', /* payload, n */ "
- jQuery.notify(payload.msg, payload.type)
- ");
- UI::hButtonAjaxOnResponse('todoGotoProcesEditorAjax', /* payload, n */ "
- jQuery.notify('TODO: edytor procesu', 'error')
- ");
- if ($idTable > 0) {
- $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
- UI::startTag('details');
- echo UI::h('summary', ['style'=>"cursor:pointer; margin-bottom:12px"], "Wszystkie powiązania komórek z procesami - szczegóły");
- UI::table([
- 'cols_label' => $labelPermCols,
- 'cols_help' => $helpPermCols,
- 'rows' => array_map(function ($row) use ($renderProcesLink, $renderPermCell, $permCols) {
- $splitPos = (strlen($row['TABLE_DESCRIPTION']) > 20) ? strpos($row['TABLE_DESCRIPTION'], ' ', 20) : 20;
- if ($splitPos > 30) $splitPos = 20;
- $row['TABLE_DESCRIPTION'] = UI::h('details', [], [
- UI::h('summary', [ 'style' => "white-space:nowrap" ], substr($row['TABLE_DESCRIPTION'], 0, $splitPos)),
- UI::h('p', [], substr($row['TABLE_DESCRIPTION'], $splitPos)),
- ]);
- $row['ID_PROCES'] = $renderProcesLink($row['ID_PROCES']);
- foreach ($permCols as $colPerm) $row[ $colPerm ] = $renderPermCell( $row[$colPerm] );
- return $row;
- }, $aclTableRows)
- ]);
- UI::endTag('details');
- $csvIdProces = array();
- foreach ($aclTableRows as $row) {
- if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
- }
- }
- $databaseName = DB::getPDO( $idDatabase )->getDatabaseName();
- // DBG::nicePrint($aclXsdFieldTypes, 'DBG: $aclXsdFieldTypes');
- $aclXsdFieldTypes = array_map(function ($xsdType) {
- return array_merge([
- 'type' => $xsdType
- ], ('ref:' === substr($xsdType, 0, 4))
- ? [
- 'type' => 'ref',
- 'typeDesc' => $xsdType,
- ]
- : []
- );
- }, $aclXsdFieldTypes);
- $realDatabaseFields = DB::getPDO()->fetchAll("
- select t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
- from `information_schema`.`COLUMNS` t
- where t.TABLE_SCHEMA = '{$databaseName}'
- and t.TABLE_NAME like '{$rootTableName}'
- ");
- // DBG::nicePrint($realDatabaseFields, 'DBG: $realDatabaseFields');
- foreach ($realDatabaseFields as $row) {
- $fieldName = $row['COLUMN_NAME'];
- if (!array_key_exists($fieldName, $aclXsdFieldTypes)) {
- $aclXsdFieldTypes[$fieldName] = [
- 'type' => "RAW:" . $row['DATA_TYPE'],
- ];
- }
- $aclXsdFieldTypes[$fieldName]['typeDesc'] = $row['COLUMN_TYPE'];
- }
- UI::table([
- 'caption' => "Powiązania komórek z procesem",
- 'cols_label' => $labelPermCols,
- 'cols_help' => $helpPermCols,
- 'rows' => array_map(
- function ($aclFieldType, $fieldName) use ($aclTableRows, $idTable, $renderProcesLink, $renderPermCell, $permCols) {
- $item = [
- 'name' => $fieldName,
- 'typ' => UI::h('details', [], [
- UI::h('summary', ['style'=>"cursor:pointer;outline:none"], $aclFieldType['type']),
- UI::h('p', [], $aclFieldType['typeDesc']),
- ]),
- 'w procesie' => [],
- 'idZasob' => 0,
- ];
- foreach ($permCols as $colPerm) $item[$colPerm] = '';
- foreach ($aclTableRows as $aclInfo) {
- if (strtolower($aclInfo['CELL_NAME']) == strtolower($fieldName)) {
- $item['w procesie'][] = $aclInfo['ID_PROCES'];
- $item['idZasob'] = $aclInfo['ID_CELL'];
- foreach ($permCols as $colPerm) $item[$colPerm] += $aclInfo[$colPerm];
- }
- }
- $item['w procesie'] = (empty($item['w procesie']))
- ? "<i style=\"color:red\">Brak</i>"
- : UI::h('span', ['style'=>"white-space:nowrap"], implode(", ", array_map($renderProcesLink, $item['w procesie'])));
- if (!$item['idZasob']) $item['idZasob'] = DB::getPDO()->fetchValue("select ID from CRM_LISTA_ZASOBOW where `DESC` = '{$fieldName}' and PARENT_ID = {$idTable} limit 1");
- $item['idZasob'] = ($item['idZasob']) ? $item['idZasob'] : UI::h('i', ['style'=>"color:silver"], "Brak");
- foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
- return $item;
- }, $aclXsdFieldTypes, array_keys($aclXsdFieldTypes)
- )
- ]);
- if (!empty($csvIdProces)) {
- $userLogin = User::getLogin();
- $csvIdProces = implode(",", $csvIdProces);
- UI::tag('h4', ['style'=>"margin-top:40px"], "Procesy dla '{$userLogin}': [{$csvIdProces}] <small><i>(z tabeli CRM_PROCES_idx_USER_to_PROCES_VIEW)</i></small>");
- $rows = DB::getPDO()->fetchAll("
- select ID_PROCES
- from `CRM_PROCES_idx_USER_to_PROCES_VIEW`
- where ADM_ACCOUNT = '{$userLogin}'
- and ID_PROCES in({$csvIdProces})
- group by ID_PROCES
- ");
- if (empty($rows)) UI::alert('warning', "Brak danych");
- $userIdProces = array_map( V::makePick('ID_PROCES', 0, 'int'), $rows );
- $userTablePerms = array_reduce($aclTableRows, function ($ret, $row) use ($userIdProces, $permCols) {
- $idProces = (int)$row['ID_PROCES'];
- if (!$idProces) return $ret;
- if (!in_array($idProces, $userIdProces)) return $ret;
- if (!array_key_exists($row['CELL_NAME'], $ret)) {
- $ret[ $row['CELL_NAME'] ] = [
- 'fieldName' => $row['CELL_NAME'],
- 'idZasob' => $row['ID_CELL'],
- 'w procesie' => [],
- ];
- foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] = '';
- }
- $ret[ $row['CELL_NAME'] ]['w procesie'] []= $row['ID_PROCES'];
- foreach ($permCols as $colPerm) $ret[ $row['CELL_NAME'] ][ $colPerm ] += $row[ $colPerm ];
- return $ret;
- }, []);
- UI::table([
- 'caption' => "Uprawniena dla usera '{$userLogin}'",
- 'cols_label' => $labelPermCols,
- 'cols_help' => $helpPermCols,
- 'rows' => array_map(function ($item) use ($renderProcesLink, $renderPermCell, $permCols) {
- sort($item['w procesie']);
- $item['w procesie'] = array_unique($item['w procesie'], SORT_NUMERIC);
- $item['w procesie'] = implode(", ", array_map($renderProcesLink, $item['w procesie']));
- foreach ($permCols as $colPerm) $item[$colPerm] = $renderPermCell($item[$colPerm]);
- return $item;
- }, $userTablePerms),
- ]);
- } else UI::alert('warning', "Brak przypisanych procesów");
- } catch (Exception $e) {
- UI::alert('danger', $e->getMessage());
- DBG::log($e);
- }
- UI::endContainer();
- UI::dol();
- }
- }
|