| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- Lib::loadClass('DB');
- Lib::loadClass('UsersHelper');
- Lib::loadClass('ProcesHelper');
- Lib::loadClass('TableAcl');
- Lib::loadClass('SchemaReader');
- class UserAcl {
- var $_user_id;
- var $_use_cache = false;
- /**
- * User groups (cached)
- */
- var $_groups = array();
- var $_proces_ids = array();
- var $_proces_tree_flat = array();
- var $_proces_used_ids = array();
- var $_proces_used_map = array();
- var $_proces_init_used_ids = array();
- function __construct($user_id, $use_cache = false) {
- $this->_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 '<div style="border:1px solid blue;background-color:#eee;">' . "<b>find_rec(CRM_PROCESS_TREE, {$proces_id}, 'init') {</b>\n";
- $CRM_PROCES_USERA_UZYTY_FIND = array();
- $this->find_in_tree_parent_rec($CRM_PROCESS_TREE, $proces_id, $CRM_PROCES_USERA_UZYTY_FIND, $dbg);
- if ($dbg) echo '</div>';
- $this->_proces_used_ids[$proces_id] = true;//trzeba dodac sam proces, bo go brakuje w indeksach
- $this->_proces_used_map[$proces_id][] = $proces_id;//trzeba dodac sam proces, bo go brakowalo w indeksach
- if (is_array($CRM_PROCES_USERA_UZYTY_FIND)) {
- foreach ($CRM_PROCES_USERA_UZYTY_FIND as $INDEX => $proces_id_found ) {
- $this->_proces_used_ids[$proces_id_found] = true;
- $this->_proces_used_map[$proces_id][] = $proces_id_found;//tworze mape procesow od glownego w dol
- }
- }
- }
- $this->_proces_used_ids = array_keys($this->_proces_used_ids);
- $this->_cache_save('_proces_used_ids', $this->_proces_used_ids);
- $this->_cache_save('_proces_used_map', $this->_proces_used_map);
- }
- public function getUsedProcesMap() {// TODO: RM, used in ProcesMenu (testy) - use view instead
- $this->_createUsedProcesMap();
- return $this->_proces_used_map;
- }
- public function getUsedProcesIds($procesID = null) {// TODO: RM, used in ProcesMenu
- $this->_createUsedProcesMap();
- $usedProcesListIds = $this->_proces_used_ids;
- if ($procesID > 0) {
- $map = $this->getUsedProcesMap();
- if (!array_key_exists($procesID, $map)) {
- return false;
- }
- $usedProcesListIds = $map[$procesID];
- }
- return $usedProcesListIds;
- }
- public function getUsedProcesInitIds($zasobId = 0) {// TODO: RM, used in ProcesMenu
- if ($zasobId > 0) {
- $pInitListRaw = $this->getProcesInitList($zasobId);
- if (!empty($pInitListRaw)) {
- $pInitListRaw = array_values($pInitListRaw);
- $pInitListRaw = array_unique($pInitListRaw);
- }
- return $pInitListRaw;
- } else {
- $usedProcesInitList = $this->getUsedProcesInitList();
- return array_keys($usedProcesInitList);
- }
- }
- public function getUsedProcesInitList() {// TODO: RM, used in ProcesMenu
- if (!empty($this->_proces_init_used)) {
- return $this->_proces_init_used;
- }
- $map = $this->getUsedProcesMap();
- if (!empty($map)) {
- $db = DB::getDB();
- $sql = "select `ID`, `DESC`
- from `CRM_PROCES`
- where `TYPE`='PROCES_INIT' and `ID` in (" . implode(",", array_keys($map)) . ") ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $this->_proces_init_used[$r->ID] = $r->DESC;
- }
- }
- return $this->_proces_init_used;
- }
- function find_in_tree_parent_rec(&$tree, $parent_id, &$ret_array = array(), $dbg = false, $rec_limit = 1) {// TODO: RM used in _createUsedProcesMap
- if ($dbg) { echo '<p style="margin:0;">' . "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) . ")" .'</p>'; }
- 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 '<p style="margin:0;">' . "-- find_in_tree_parent_rec(tree, $parent_id, ret, $dbg, $rec_limit) crm_proces_usera_uzyty: [" . implode(', ', $ret_array) . "]" .'</p>'; }
- // 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 = <<<SQL
- select tpvg.`ID_TABLE`
- , tpvg.`ID_DATABASE` as `db`
- , tpvg.`TABLE_NAME` as `name`
- , tpvg.`TABLE_LABEL` as `label`
- , tpvg.`TABLE_DESCRIPTION` as `opis`
- from `CRM_PROCES_idx_TABLE_TO_PROCES_GROUPED_VIEW` as tpvg
- where tpvg.`ID_PROCES` in({$sqlIdProcesListSql})
- group by tpvg.`ID_TABLE`
- SQL;
- //echo'<pre>$userAccessForTables - $sql ';print_r($sql);echo'</pre>';
- $db = DB::getDB();
- $res = $db->query($sql);
- while ($h = $db->fetch_assoc($res)) {
- $userAccessForTables[$h['ID_TABLE']] = $h;
- }
- DBG::_('DBG_SCH', '1', "userAccessForTables", $userAccessForTables, __CLASS__, __FUNCTION__, __LINE__ );
- foreach ($userAccessForTables as $idTable => $tableConfig) {
- if (!array_key_exists($idTable, $foundTbls)) {
- $foundTbls[$idTable] = $tableConfig;
- }
- }
- $userAccessForUrls = array();
- $sql = <<<SQL
- select upvg.`ID_URL`
- , upvg.`URL_LINK` as `link`
- , upvg.`URL_LABEL` as `label`
- , upvg.`URL_DESC` as `opis`
- from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
- where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
- group by upvg.`ID_URL`
- SQL;
- //echo'<pre>$userAccessForUrls - $sql ';print_r($sql);echo'</pre>';
- $db = DB::getDB();
- $res = $db->query($sql);
- while ($h = $db->fetch_assoc($res)) {
- $userAccessForUrls[$h['ID_URL']] = $h;
- }
- //echo'<pre>$userAccessForUrls ';print_r($userAccessForUrls);echo'</pre>';
- //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 = <<<SQL
- select z.`ID` as `ID_CELL`
- , z.`DESC` as `CELL_NAME`
- , z.`DESC_PL` as `CELL_LABEL`
- , z.`OPIS` as `CELL_DESC`
- , z.`SORT_PRIO` as `SORT_PRIO`
- , zp.`ID` as `ID_TABLE`
- -- , zp.`DESC` as `TABLE_NAME`
- -- , wsk.`ID_PROCES` as `ID_PROCES`
- , sum(IF(przyp.`FORM_TREAT` & 2, 1, 0)) as PERM_R
- , sum(IF(przyp.`FORM_TREAT` & 4, 1, 0)) as PERM_W
- , sum(IF(przyp.`FORM_TREAT` & 8, 1, 0)) as PERM_X
- , sum(IF(przyp.`FORM_TREAT` & 16, 1, 0)) as PERM_C
- , sum(IF(przyp.`FORM_TREAT` & 32, 1, 0)) as PERM_S
- , sum(IF(przyp.`FORM_TREAT` & 64, 1, 0)) as PERM_O
- , sum(IF(przyp.`FORM_TREAT` & 128, 1, 0)) as PERM_V
- , sum(IF(przyp.`FORM_TREAT` & 256, 1, 0)) as PERM_E
- from `CRM_LISTA_ZASOBOW` z
- join `CRM_LISTA_ZASOBOW` zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE`='TABELA' and zp.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_WSKAZNIK` wsk on(wsk.`ID_ZASOB`=z.`ID` and wsk.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_PROCES` p on(p.`ID`=wsk.`ID_PROCES` and p.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_PRZYPADEK` as przyp on (przyp.`ID`=wsk.`ID_PRZYPADEK`)
- where z.`TYPE`='KOMORKA'
- and z.`A_STATUS` in('WAITING','NORMAL')
- and zp.`ID`='{$idTable}'
- and wsk.`ID_PROCES` in({$sqlIdProcesListSql})
- -- group by z.`ID`, wsk.`ID_PROCES`
- group by z.`ID`
- order by z.`SORT_PRIO`
- SQL;
- //echo'<pre>UserAcl::getPermsForTable('.$idTable.')::$tableCellToProcesSql ';print_r($tableCellToProcesSql);echo'</pre>';
- $userPermsForTable = array();
- $db = DB::getDB();
- $res = $db->query($tableCellToProcesSql);
- while ($h = $db->fetch_assoc($res)) {
- $idCell = $h['ID_CELL'];
- $userPermsForTable[$idCell] = $h;
- }
- return $userPermsForTable;
- }
- public function getUsedUserGroupIds() {
- $idUserGroupList = User::getGroupsIds();
- // TODO: acl filtr by group ids
- return $idUserGroupList;
- }
- public function getUsedUserProcesIdsSql() {
- $filterIdProces = $this->getFilterIdProces();
- if ($filterIdProces > 0) {
- return <<<SQL
- select i.`ID_PROCES`
- from `CRM_PROCES_idx` i
- where i.`idx_PROCES_INIT_ID`='{$filterIdProces}'
- SQL;
- }
- $idUserGroupList = $this->getUsedUserGroupIds();
- $sqlIdUserGroupList = implode(",", $idUserGroupList);
- // TODO: acl filtr by proces id
- return <<<SQL
- select gi.`ID_PROCES`
- from `CRM_PROCES_idx_GROUP_to_PROCES` gi
- where gi.`ID_GROUP` in({$sqlIdUserGroupList})
- SQL;
- }
- }
|