_user_id = $user_id; $this->_use_cache = $use_cache; $this->_cache_init(); } function fetchGroups() { if ($this->_user_id < 0) return false; if (!empty($this->_groups)) { return $this->_groups; } $this->_groups = $this->_cache_read('_groups'); if ($this->_groups != null) { return $this->_groups; } $this->_groups = array(); $this->_groups = UsersHelper::get_group_by_user($this->_user_id); $this->_cache_save('_groups', $this->_groups); return $this->_groups; } function getProcesIds() { if (!empty($this->_proces_ids)) { return $this->_proces_ids; } $db = DB::getDB(); $groups = $this->fetchGroups(); if (empty($groups)) { return false; } $sql = "select p.`ID` from `CRM_PROCES` as p left join `CRM_WSKAZNIK` as w on(p.`ID`=w.`ID_PROCES`) where w.`ID_ZASOB` in(" . implode(",", array_keys($groups)) . ") and w.`A_STATUS` in('NORMAL', 'WAITING') and p.`A_STATUS` in('NORMAL', 'WAITING') "; $res = $db->query($sql); while ($r = $db->fetch($res)) { $this->_proces_ids [$r->ID] = true; } $this->_proces_ids = array_keys($this->_proces_ids); return $this->_proces_ids; } function getProcesTree() { if (!empty($this->_proces_tree_flat)) { return $this->_proces_tree_flat; } $db = DB::getDB(); $sql = "select p.`ID`, p.`PARENT_ID` from `CRM_PROCES` as p where p.`A_STATUS` in('WAITING','NORMAL') union select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID from `CRM_PROCES` as p where p.`A_STATUS` in('WAITING','NORMAL') and p.IF_TRUE_GOTO>0 and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN' "; // union select '83','122' union select p.`ID` as ID, p.`IF_TRUE_GOTO` as PARENT_ID from `CRM_PROCES` as p where p.`A_STATUS` in('WAITING','NORMAL') and p.IF_TRUE_GOTO>0 //union select '83','122' //union select '83','2025' $res = $db->query($sql); while ($r = $db->fetch($res)) { $this->_proces_tree_flat[$r->PARENT_ID][] = $r->ID; } return $this->_proces_tree_flat; } private function _createUsedProcesMap() {// TODO: RM if (!empty($this->_proces_used_ids)) { return; } $this->_proces_used_ids = $this->_cache_read('_proces_used_ids'); $this->_proces_used_map = $this->_cache_read('_proces_used_map'); if ($this->_proces_used_ids != null && $this->_proces_used_map != null) { return; } $this->_proces_used_ids = array();// find path from proces_id connected with user to the end of process $this->_proces_used_map = array(); $dbg = false; $ID_PROCESOW_USERA = $this->getProcesIds(); if (empty($ID_PROCESOW_USERA)) { return; } $CRM_PROCESS_TREE = $this->getProcesTree(); // ! rekursywnie szukam sciezek procesow powiazanych dla usera procesow foreach ($ID_PROCESOW_USERA as $proces_id) { $this->_proces_used_ids[$proces_id] = true; if ($dbg) echo '
' . "find_in_tree_parent_rec(tree, $parent_id, ret, $dbg, $rec_limit) find_in_tree_parent_rec_return: [" . implode(', ', $find_in_tree_parent_rec_return) . "] found(" . in_array($parent_id, $ret_array) . ")" .'
'; } if (isset($tree[$parent_id])) { foreach ($tree[$parent_id] as $IND => $CHILD) { if (in_array($CHILD, $ret_array)) { continue; } $ret_array[] = $CHILD; if ($dbg) { echo '' . "-- find_in_tree_parent_rec(tree, $parent_id, ret, $dbg, $rec_limit) crm_proces_usera_uzyty: [" . implode(', ', $ret_array) . "]" .'
'; } // TODO: $ret_array będize mial tylko wartosci z ostatniego wyszukiwania $this->find_in_tree_parent_rec($tree, $CHILD, $ret_array, $dbg, $rec_limit); } } } function _cache_init() { if (!$this->_use_cache) return; if (!isset($_SESSION['UserAcl_cache'])) { $_SESSION['UserAcl_cache'] = array(); } else { $user_id = V::get('_user_id', 0, $_SESSION['UserAcl_cache'], 'int'); if ($user_id > 0) { if ($user_id != $this->_user_id) { // clean cache if another user $_SESSION['UserAcl_cache'] = array(); } } } } function _cache_clear() { if (!$this->_use_cache) return; $_SESSION['UserAcl_cache'] = array(); } /** * Read data from cache. */ function _cache_read($key) { if (!$this->_use_cache) return null; if (array_key_exists($key, $_SESSION['UserAcl_cache'])) { return $_SESSION['UserAcl_cache'][$key]; } return null; } /** * Save data in cache. */ function _cache_save($key, $value) { if (!$this->_use_cache) return; if ($key == 'foundTables') { $tblIds = array(); foreach ($value as $idTable => $tableConfig) { $tblIds[] = $idTable; $vTableAcl = TableAcl::buildInstance($idTable, $tableConfig); } $value = $tblIds; } $_SESSION['UserAcl_cache'][$key] = $value; } public function getUrls() { $urls = $this->_cache_read('foundUrls');// TODO: old ['MENU_SELECT_PROCES_DATA']['MENU_COLUMN'] if (empty($urls)) $urls = array(); return $urls; } public function getObjectAcl($sourceName, $objName) { if ('default_db' == $sourceName) { $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri("{$sourceName}/{$objName}"); if ($zasobTblInfo) { if ($this->hasTableAcl($zasobTblInfo->ID)) { return $this->getTableAcl($zasobTblInfo->ID); } } } else { throw new HttpException("Not Implemented", 501); } return false; } public function getTablesAcl() {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW` $tbls = array(); $tblIds = $this->_cache_read('foundTables'); foreach ($tblIds as $vTableID) { $tbls[$vTableID] = TableAcl::getInstance($vTableID); } return $tbls; } public function hasTableAcl($tableID) {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW` $tbls = $this->_cache_read('foundTables'); return (is_array($tbls) && in_array($tableID, $tbls)); } public function getTableAcl($tableID) { return TableAcl::getInstance($tableID); } public function getProcesInitList($tblId) { if ($tblId > 0) { $map = $this->_cache_read('foundMap'); if (is_array($map) && array_key_exists($tblId, $map)) { return $map[$tblId]; } } return null; } /** * Check if perms are only for one proces. * @returns int or false */ public function getPermsFiltrProcesId() {// TODO: RMME mved to getFilterIdProces return $this->getFilterIdProces(); } public function getFilterIdProces() { $procesID = $this->_cache_read('permsByProcesID'); return ($procesID > 0)? $procesID : false; } public function fetchAllPerms($force = false) { $this->_fetchPerms('All', $force); } public function fetchProcesPerms($procesID, $force = false) { $this->_fetchPerms($procesID, $force); } /** * @param $type - 'All', $procesID */ private function _fetchPerms($type, $force = false) { $db = DB::getDB(); $procesID = 0;// if 0 - All, alse perms by procesID $foundUrls = array(); $foundTbls = array(); if ($force) { $this->_cache_clear(); } $schemaReader = new SchemaReader(); if ($type == 'All') { $schemaReader->getAll(); } else if (is_numeric($type) && $type > 0) { $procesID = (int)$type; } {// TODO: fetch from schema files if ($schemaReader->hasProcessConfigs()) { foreach ($schemaReader->getProcessConfigs() as $process) { DBG::_('DBG_SCH', '1', "process", $process, __CLASS__, __FUNCTION__, __LINE__ ); if ($process->hasAccess()) { $tables = $process->getTables(); DBG::_('DBG_SCH', '1', "tables", $tables, __CLASS__, __FUNCTION__, __LINE__ ); foreach ($tables as $vTable) { $tblUri = $vTable->getUri(); $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri); DBG::_('DBG_SCH', '1', "table(" . $vTable->getLabel() . ")", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__ ); if ($zasobTblInfo) { $idTable = $zasobTblInfo->ID; // TODO: use $vTableAcl = TableAcl::buildInstance($idTable, $tableConfig); // TODO: then $vTableAcl->init();// to save table fields if (!array_key_exists($idTable, $foundTbls)) { $tableConfig = array(); $tableConfig['ID_TABLE'] = $idTable; $tableConfig['db'] = $zasobTblInfo->P__ID; $tableConfig['name'] = $zasobTblInfo->DESC; $tableConfig['label'] = $zasobTblInfo->DESC_PL; $tableConfig['opis'] = $zasobTblInfo->OPIS; $foundTbls[$idTable] = $tableConfig; } $tableAcl = TableAcl::buildInstance($idTable, $foundTbls[$idTable]); $fieldsConfig = array(); $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable); foreach ($vTable->getFields() as $field) { $fldInfo = V::get($field->getName(), null, $fldsInfo); if ($fldInfo) { if (!array_key_exists($fldInfo->ID, $fieldsConfig)) {//if (!$tableAcl->hasField($fldInfo->ID)) { $fieldsConfig[$fldInfo->ID] = array(); $fieldsConfig[$fldInfo->ID]['ID_CELL'] = $fldInfo->ID; $fieldsConfig[$fldInfo->ID]['CELL_NAME'] = $fldInfo->DESC; $fieldsConfig[$fldInfo->ID]['CELL_DESC'] = $fldInfo->OPIS; $fieldsConfig[$fldInfo->ID]['SORT_PRIO'] = $fldInfo->SORT_PRIO; $fieldsConfig[$fldInfo->ID]['CELL_LABEL'] = $fldInfo->DESC_PL; $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] = ''; //$tableAcl->addField($fldInfo->ID, $fldInfo->DESC, $fldInfo->OPIS, $fldInfo->SORT_PRIO, $fldInfo->DESC_PL); } // TODO: $field->getPerms() -> PERM_R, PERM_W, ... etc.? $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] .= $field->getPerms();//$tableAcl->setFieldPerms($fldInfo->ID, $field->getPerms()); } } $tableAcl->initFieldsFromConfig($fieldsConfig); DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__); $tableAcl->save(); DBG::_('DBG_SCH', '1', "tableAcl({$idTable})", $tableAcl, __CLASS__, __FUNCTION__, __LINE__); } } } } } else { DBG::_('DBG_SCH', '1', "NO \$schemaReader->hasProcessConfigs()", null, __CLASS__, __FUNCTION__, __LINE__); } DBG::_('DBG_SCH', '1', "foundTbls", $foundTbls, __CLASS__, __FUNCTION__, __LINE__); }// fetch from schema files $this->setFilterIdProces($procesID);//$this->_cache_save('permsByProcesID', $procesID); if (1) { $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql(); $userAccessForTables = array(); $sql = <<$userAccessForUrls ';print_r($userAccessForUrls);echo''; //TODO: $foundUrls = $userAccessForUrls; } $this->_cache_save('foundUrls', $foundUrls); $this->_cache_save('foundTables', $foundTbls); } public function setFilterIdProces($procesID) { $this->_cache_save('permsByProcesID', $procesID); } public function getPermsForTable($idTable) { $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql(); $tableCellToProcesSql = <<