UserAcl.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. Lib::loadClass('DB');
  3. Lib::loadClass('UsersHelper');
  4. Lib::loadClass('ProcesHelper');
  5. Lib::loadClass('TableAcl');
  6. Lib::loadClass('SchemaReader');
  7. Lib::loadClass('SchemaFactory');
  8. class UserAcl {
  9. var $_user_id;
  10. var $_use_cache = false;
  11. /**
  12. * User groups (cached)
  13. */
  14. var $_groups = array();
  15. var $_proces_ids = array();
  16. var $_proces_tree_flat = array();
  17. var $_proces_used_ids = array();
  18. var $_proces_used_map = array();
  19. var $_proces_init_used_ids = array();
  20. function __construct($user_id, $use_cache = false) {
  21. $this->_user_id = $user_id;
  22. $this->_use_cache = $use_cache;
  23. $this->_cache_init();
  24. }
  25. function fetchGroups() {
  26. if ($this->_user_id < 0) return false;
  27. if (!empty($this->_groups)) {
  28. return $this->_groups;
  29. }
  30. $this->_groups = $this->_cache_read('_groups');
  31. if ($this->_groups != null) {
  32. return $this->_groups;
  33. }
  34. $this->_groups = array();
  35. $this->_groups = UsersHelper::get_group_by_user($this->_user_id);
  36. $this->_cache_save('_groups', $this->_groups);
  37. return $this->_groups;
  38. }
  39. function getProcesIds() {
  40. if (!empty($this->_proces_ids)) {
  41. return $this->_proces_ids;
  42. }
  43. $db = DB::getDB();
  44. $groups = $this->fetchGroups();
  45. if (empty($groups)) {
  46. return false;
  47. }
  48. $sql = "select p.`ID`
  49. from `CRM_PROCES` as p
  50. left join `CRM_WSKAZNIK` as w on(p.`ID`=w.`ID_PROCES`)
  51. where
  52. w.`ID_ZASOB` in(" . implode(",", array_keys($groups)) . ")
  53. and w.`A_STATUS` in('NORMAL', 'WAITING')
  54. and p.`A_STATUS` in('NORMAL', 'WAITING')
  55. ";
  56. $res = $db->query($sql);
  57. while ($r = $db->fetch($res)) {
  58. $this->_proces_ids [$r->ID] = true;
  59. }
  60. $this->_proces_ids = array_keys($this->_proces_ids);
  61. return $this->_proces_ids;
  62. }
  63. function getProcesTree() {
  64. if (!empty($this->_proces_tree_flat)) {
  65. return $this->_proces_tree_flat;
  66. }
  67. $db = DB::getDB();
  68. $sql = "select p.`ID`, p.`PARENT_ID`
  69. from `CRM_PROCES` as p
  70. where p.`A_STATUS` in('WAITING','NORMAL')
  71. union
  72. select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID
  73. from `CRM_PROCES` as p
  74. where p.`A_STATUS` in('WAITING','NORMAL')
  75. and p.IF_TRUE_GOTO>0
  76. and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN'
  77. ";
  78. // 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
  79. //union select '83','122'
  80. //union select '83','2025'
  81. $res = $db->query($sql);
  82. while ($r = $db->fetch($res)) {
  83. $this->_proces_tree_flat[$r->PARENT_ID][] = $r->ID;
  84. }
  85. return $this->_proces_tree_flat;
  86. }
  87. function _cache_init() {
  88. if (!$this->_use_cache) return;
  89. if (!isset($_SESSION['UserAcl_cache'])) {
  90. $_SESSION['UserAcl_cache'] = array();
  91. }
  92. else {
  93. $user_id = V::get('_user_id', 0, $_SESSION['UserAcl_cache'], 'int');
  94. if ($user_id > 0) {
  95. if ($user_id != $this->_user_id) {
  96. // clean cache if another user
  97. $_SESSION['UserAcl_cache'] = array();
  98. }
  99. }
  100. }
  101. }
  102. function _cache_clear() {
  103. if (!$this->_use_cache) return;
  104. $_SESSION['UserAcl_cache'] = array();
  105. }
  106. /**
  107. * Read data from cache.
  108. */
  109. function _cache_read($key) {
  110. if (!$this->_use_cache) return null;
  111. if (array_key_exists($key, $_SESSION['UserAcl_cache'])) {
  112. return $_SESSION['UserAcl_cache'][$key];
  113. }
  114. return null;
  115. }
  116. /**
  117. * Save data in cache.
  118. */
  119. function _cache_save($key, $value) {
  120. if (!$this->_use_cache) return;
  121. if ($key == 'foundTables') {
  122. $tblIds = array();
  123. foreach ($value as $idTable => $tableConfig) {
  124. $tblIds[] = $idTable;
  125. $vTableAcl = TableAcl::buildInstance($idTable, $tableConfig);
  126. }
  127. $value = $tblIds;
  128. }
  129. $_SESSION['UserAcl_cache'][$key] = $value;
  130. }
  131. public function getUrls() {// @require _fetchPerms
  132. static $_userUrls = null;
  133. if (null !== $_userUrls) return $_userUrls;
  134. $_userUrls = [];
  135. $filterIdProces = $this->getFilterIdProces();
  136. $userUrlAction = SchemaFactory::loadDefaultObject('UserUrlAction');
  137. $userUrlAction->setIdUser($this->_user_id);
  138. $userUrlAction->setIdProcesFilter($filterIdProces);
  139. foreach ($userUrlAction->getItems() as $url) {
  140. $_userUrls[ $url['ID_URL'] ] = $url['opis'];
  141. }
  142. return $_userUrls;
  143. }
  144. public function getObjectAcl($sourceName, $objName) {// TODO: rename $sourceName to $prefix (xml namespace - @see Core_AclHelper)
  145. if (empty($objName)) throw new Exception("Missing object name", 400);
  146. if ('default_db' == $sourceName) {
  147. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri("{$sourceName}/{$objName}");
  148. if (!$zasobTblInfo) throw new HttpException("Object not Found '{$objName}'", 404);
  149. if (!$this->hasTableAcl($zasobTblInfo->ID)) throw new HttpException("Access Denied for '{$sourceName}/{$objName}'", 403);
  150. return $this->getTableAcl($zasobTblInfo->ID);
  151. } else if ('objects' == $sourceName) {
  152. return SchemaFactory::loadDefaultObject($objName);
  153. } else if ('default_objects' == $sourceName) {
  154. return SchemaFactory::loadDefaultObject($objName);
  155. } else if ('default_db__x3A__' == substr($sourceName, 0, 17)) {
  156. $rootTableName = strtolower(substr($sourceName, 17));
  157. return SchemaFactory::loadTableObject($rootTableName, $objName);
  158. }
  159. throw new HttpException("Not Implemented", 501);
  160. }
  161. public function getTablesAcl() {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
  162. $tbls = array();
  163. $tblIds = $this->_cache_read('foundTables');
  164. foreach ($tblIds as $vTableID) {
  165. $tbls[$vTableID] = TableAcl::getInstance($vTableID);
  166. }
  167. return $tbls;
  168. }
  169. public function hasTableAcl($tableID) {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
  170. $tbls = $this->_cache_read('foundTables');
  171. return (is_array($tbls) && in_array($tableID, $tbls));
  172. }
  173. public function getTableAcl($tableID) {
  174. $tblAcl = TableAcl::getInstance($tableID);
  175. if (!$tblAcl) throw new Exception("Brak tabeli nr [{$tableID}]!");
  176. $tblAcl->init();
  177. return $tblAcl;
  178. }
  179. /**
  180. * Check if perms are only for one proces.
  181. * @returns int or false
  182. */
  183. public function getPermsFiltrProcesId() {// TODO: RMME mved to getFilterIdProces
  184. return $this->getFilterIdProces();
  185. }
  186. public function getFilterIdProces() {
  187. $procesID = $this->_cache_read('permsByProcesID');
  188. return ($procesID > 0)? $procesID : false;
  189. }
  190. public function fetchAllPerms($force = false) {
  191. $this->_fetchPerms('All', $force);
  192. }
  193. public function fetchProcesPerms($procesID, $force = false) {
  194. $this->_fetchPerms($procesID, $force);
  195. }
  196. /**
  197. * @param $type - 'All', $procesID
  198. */
  199. private function _fetchPerms($type, $force = false) {
  200. $procesID = 0;// if 0 - All, alse perms by procesID
  201. $foundTbls = array();
  202. if ($force) {
  203. $this->_cache_clear();
  204. }
  205. $schemaReader = new SchemaReader();
  206. if ($type == 'All') {
  207. $procesID = 0;// if 0 - All, alse perms by procesID
  208. $schemaReader->getAll();
  209. } else if (is_numeric($type) && $type > 0) {
  210. $procesID = (int)$type;
  211. }
  212. {// fetch from schema files in SE/schema/process/*.ini.php
  213. if ($schemaReader->hasProcessConfigs()) {
  214. foreach ($schemaReader->getProcessConfigs() as $process) {
  215. DBG::_('DBG_SCH', '1', "process", $process, __CLASS__, __FUNCTION__, __LINE__ );
  216. if ($process->hasAccess()) {
  217. $tables = $process->getTables();
  218. DBG::_('DBG_SCH', '1', "tables", $tables, __CLASS__, __FUNCTION__, __LINE__ );
  219. foreach ($tables as $vTable) {
  220. $tblUri = $vTable->getUri();
  221. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri);
  222. DBG::_('DBG_SCH', '1', "table(" . $vTable->getLabel() . ")", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__ );
  223. if ($zasobTblInfo) {
  224. $idTable = $zasobTblInfo->ID;
  225. if (!array_key_exists($idTable, $foundTbls)) {
  226. $tableConfig = array();
  227. $tableConfig['ID_TABLE'] = $idTable;
  228. $tableConfig['db'] = $zasobTblInfo->P__ID;
  229. $tableConfig['name'] = $zasobTblInfo->DESC;
  230. $tableConfig['label'] = $zasobTblInfo->DESC_PL;
  231. $tableConfig['opis'] = $zasobTblInfo->OPIS;
  232. $foundTbls[$idTable] = $tableConfig;
  233. }
  234. $tableAcl = TableAcl::buildInstance($idTable, $foundTbls[$idTable]);
  235. $fieldsConfig = array();
  236. $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable);
  237. foreach ($vTable->getFields() as $field) {
  238. $fldInfo = V::get($field->getName(), null, $fldsInfo);
  239. if ($fldInfo) {
  240. if (!array_key_exists($fldInfo->ID, $fieldsConfig)) {//if (!$tableAcl->hasField($fldInfo->ID)) {
  241. $fieldsConfig[$fldInfo->ID] = array();
  242. $fieldsConfig[$fldInfo->ID]['ID_CELL'] = $fldInfo->ID;
  243. $fieldsConfig[$fldInfo->ID]['CELL_NAME'] = $fldInfo->DESC;
  244. $fieldsConfig[$fldInfo->ID]['CELL_DESC'] = $fldInfo->OPIS;
  245. $fieldsConfig[$fldInfo->ID]['SORT_PRIO'] = $fldInfo->SORT_PRIO;
  246. $fieldsConfig[$fldInfo->ID]['CELL_LABEL'] = $fldInfo->DESC_PL;
  247. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] = '';
  248. //$tableAcl->addField($fldInfo->ID, $fldInfo->DESC, $fldInfo->OPIS, $fldInfo->SORT_PRIO, $fldInfo->DESC_PL);
  249. }
  250. // TODO: $field->getPerms() -> PERM_R, PERM_W, ... etc.?
  251. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] .= $field->getPerms();//$tableAcl->setFieldPerms($fldInfo->ID, $field->getPerms());
  252. }
  253. }
  254. $tableAcl->initFieldsFromConfig($fieldsConfig);
  255. DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  256. $tableAcl->save();
  257. DBG::_('DBG_SCH', '1', "tableAcl({$idTable})", $tableAcl, __CLASS__, __FUNCTION__, __LINE__);
  258. }
  259. }
  260. }
  261. }
  262. } else {
  263. DBG::_('DBG_SCH', '1', "NO \$schemaReader->hasProcessConfigs()", null, __CLASS__, __FUNCTION__, __LINE__);
  264. }
  265. DBG::_('DBG_SCH', '1', "foundTbls", $foundTbls, __CLASS__, __FUNCTION__, __LINE__);
  266. }// fetch from schema files
  267. $this->setFilterIdProces($procesID);//$this->_cache_save('permsByProcesID', $procesID);
  268. $filterIdProces = $this->getFilterIdProces();
  269. $userConfigStorage = SchemaFactory::loadDefaultObject('UserConfig');// TODO:? UserSession
  270. $userConfigStorage->setIdUser($this->_user_id);
  271. $userConfigStorage->setIdProcesFilter($filterIdProces);
  272. $userConf = $userConfigStorage->getItem(1);// TODO:? session_id() or 'api'
  273. DBG::simpleLog('auth', "UserAcl::fetchPerms UserConf: " . str_replace('"', '', json_encode($userConf)));
  274. // 'lastLogin' => [ '@type' => 'xsd:dateTime', '@confKey' => 'auth_user_{ID_USER}' ],// TODO: store last login time
  275. // 'lastAuthCacheUpdate' => [ '@type' => 'xsd:dateTime', '@confKey' => 'acl_user_{ID_USER}_cache_update' ],
  276. $userObject = SchemaFactory::loadDefaultObject('UserObject');
  277. $userObject->setIdUser($this->_user_id);
  278. $userObject->setIdProcesFilter($filterIdProces);
  279. $userUrlAction = SchemaFactory::loadDefaultObject('UserUrlAction');
  280. $userUrlAction->setIdUser($this->_user_id);
  281. $userUrlAction->setIdProcesFilter($filterIdProces);
  282. // DB cache
  283. // TODO: session garbage collector. CRM_CONFIG: last_proces_update, user_last_req_date, last_user_acl_cache_update
  284. $lastProcesIndexer = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end' ");
  285. DBG::simpleLog('auth', "UserAcl::fetchPerms lastProcesIndexer: '{$lastProcesIndexer}'");
  286. if ($userConf['lastAuthCacheUpdate'] > $lastProcesIndexer) {
  287. DBG::simpleLog('auth', "UserAcl::fetchPerms skip update cache");
  288. } else {
  289. DBG::simpleLog('auth', "UserAcl::fetchPerms update cache");
  290. $userObject->updateCacheFeatures();
  291. $userUrlAction->updateCacheFeatures();
  292. $userConfigStorage->updateItem([
  293. 'ID_USER' => $this->_user_id,
  294. 'ID_PROCES' => $filterIdProces,
  295. 'lastAuthCacheUpdate' => 'NOW()'
  296. ]);
  297. }
  298. $userAccessForTables = $userObject->getItems();
  299. DBG::simpleLog('auth', "UserAcl::fetchPerms fetched " . count($userAccessForTables) . " tables");
  300. DBG::_('DBG_SCH', '1', "userAccessForTables", $userAccessForTables, __CLASS__, __FUNCTION__, __LINE__ );
  301. foreach ($userAccessForTables as $tableConfig) {
  302. unset($tableConfig['ID']);
  303. $idTable = $tableConfig['ID_TABLE'];
  304. if (!array_key_exists($idTable, $foundTbls)) {
  305. $foundTbls[$idTable] = $tableConfig;
  306. }
  307. }
  308. $this->_cache_save('foundTables', $foundTbls);
  309. // use cache for UserUrlAction turned OFF @see getUrls()
  310. // $userAccessForUrls = $userUrlAction->getItems();
  311. // DBG::logAuth($userAccessForUrls, "_fetchPerms \$userAccessForUrls");
  312. // DBG::_('DBG_SCH', '2', "userAccessForUrls", $userAccessForUrls, __CLASS__, __FUNCTION__, __LINE__ );
  313. // $foundUrls = array();
  314. // foreach ($userAccessForUrls as $vUrlConfig) {
  315. // $foundUrls[$vUrlConfig['ID_URL']] = $vUrlConfig['opis'];
  316. // }
  317. // $this->_cache_save('foundUrls', $foundUrls);
  318. }
  319. public function setFilterIdProces($procesID) {
  320. $this->_cache_save('permsByProcesID', $procesID);
  321. }
  322. public function getPermsForTable($idTable) {
  323. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  324. $tableCellToProcesSql = <<<SQL
  325. select z.`ID` as `ID_CELL`
  326. , z.`DESC` as `CELL_NAME`
  327. , z.`DESC_PL` as `CELL_LABEL`
  328. , z.`OPIS` as `CELL_DESC`
  329. , z.`SORT_PRIO` as `SORT_PRIO`
  330. , zp.`ID` as `ID_TABLE`
  331. -- , zp.`DESC` as `TABLE_NAME`
  332. -- , wsk.`ID_PROCES` as `ID_PROCES`
  333. , sum(IF(przyp.`FORM_TREAT` & 2, 1, 0)) as PERM_R
  334. , sum(IF(przyp.`FORM_TREAT` & 4, 1, 0)) as PERM_W
  335. , sum(IF(przyp.`FORM_TREAT` & 8, 1, 0)) as PERM_X
  336. , sum(IF(przyp.`FORM_TREAT` & 16, 1, 0)) as PERM_C
  337. , sum(IF(przyp.`FORM_TREAT` & 32, 1, 0)) as PERM_S
  338. , sum(IF(przyp.`FORM_TREAT` & 64, 1, 0)) as PERM_O
  339. , sum(IF(przyp.`FORM_TREAT` & 128, 1, 0)) as PERM_V
  340. , sum(IF(przyp.`FORM_TREAT` & 256, 1, 0)) as PERM_E
  341. from `CRM_LISTA_ZASOBOW` z
  342. join `CRM_LISTA_ZASOBOW` zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE`='TABELA' and zp.`A_STATUS` in('WAITING','NORMAL'))
  343. join `CRM_WSKAZNIK` wsk on(wsk.`ID_ZASOB`=z.`ID` and wsk.`A_STATUS` in('WAITING','NORMAL'))
  344. join `CRM_PROCES` p on(p.`ID`=wsk.`ID_PROCES` and p.`A_STATUS` in('WAITING','NORMAL'))
  345. join `CRM_PRZYPADEK` as przyp on (przyp.`ID`=wsk.`ID_PRZYPADEK`)
  346. where z.`TYPE`='KOMORKA'
  347. and z.`A_STATUS` in('WAITING','NORMAL')
  348. and zp.`ID`='{$idTable}'
  349. and wsk.`ID_PROCES` in({$sqlIdProcesListSql})
  350. -- group by z.`ID`, wsk.`ID_PROCES`
  351. group by z.`ID`
  352. order by z.`SORT_PRIO`
  353. SQL;
  354. //echo'<pre>UserAcl::getPermsForTable('.$idTable.')::$tableCellToProcesSql ';print_r($tableCellToProcesSql);echo'</pre>';
  355. $userPermsForTable = array();
  356. $db = DB::getDB();
  357. $res = $db->query($tableCellToProcesSql);
  358. while ($h = $db->fetch_assoc($res)) {
  359. $idCell = $h['ID_CELL'];
  360. $userPermsForTable[$idCell] = $h;
  361. }
  362. return $userPermsForTable;
  363. }
  364. public function getUsedUserGroupIds() {
  365. $idUserGroupList = User::getGroupsIds();// TODO:? $this->_user_id
  366. // TODO: acl filtr by group ids
  367. return $idUserGroupList;
  368. }
  369. public function getUsedUserProcesIdsSql() {
  370. $filterIdProces = $this->getFilterIdProces();
  371. if ($filterIdProces > 0) {
  372. return "
  373. select i.`ID_PROCES`
  374. from `CRM_PROCES_idx` i
  375. where i.`idx_MAIN_PROCES_INIT_ID`='{$filterIdProces}'
  376. ";
  377. }
  378. $idUserGroupList = $this->getUsedUserGroupIds();
  379. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  380. return "
  381. select gi.`ID_PROCES`
  382. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  383. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  384. ";
  385. }
  386. public function getProcesMaxUpdateDate($idProcesInit) {
  387. return DB::getPDO()->fetchValue("
  388. select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  389. from `CRM_PROCES` as p
  390. where p.`ID` in(
  391. select i.`ID_PROCES`
  392. from `CRM_PROCES_idx` i
  393. where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  394. )
  395. ");
  396. }
  397. /**
  398. * Ids List of Proces Init for user (skip filters)
  399. */
  400. public function getUserProcesInitIds() {
  401. $procesInitList = $this->getUserProcesInitList();
  402. return array_keys($procesInitList);
  403. }
  404. /**
  405. * List of Proces Init for user (skip filters)
  406. */
  407. public function getUserProcesInitList() {// TODO: read from CRM_PROCES_idx_GROUP_to_INIT_VIEW?
  408. $userProcesInitList = array();
  409. $idUserGroupList = $this->fetchGroups();
  410. $sqlIdUserGroupList = implode(",", array_keys($idUserGroupList));
  411. $sqlIdProcesListSql = <<<SQL
  412. select gi.`ID_PROCES`
  413. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  414. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  415. SQL;
  416. $fetchUserProcesInitListSql = <<<SQL
  417. select p.`ID`, p.`DESC`
  418. from `CRM_PROCES_idx` i
  419. join `CRM_PROCES` p on(p.`ID`=i.`idx_PROCES_INIT_ID`)
  420. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  421. group by p.`ID`
  422. order by p.`SORT_PRIO`
  423. SQL;
  424. $db = DB::getDB();
  425. $res = $db->query($fetchUserProcesInitListSql);
  426. while ($r = $db->fetch($res)) {
  427. $userProcesInitList[$r->ID] = $r->DESC;
  428. }
  429. return $userProcesInitList;
  430. }
  431. /**
  432. * Ids List of Proces Init for user (use filters)
  433. */
  434. public function getUsedUserProcesInitIds() {
  435. $usedProcesInitList = $this->getUsedUserProcesInitList();
  436. return array_keys($usedProcesInitList);
  437. }
  438. /**
  439. * List of Proces Init for user (use filters)
  440. */
  441. public function getUsedUserProcesInitList() {
  442. $filterIdProces = $this->getFilterIdProces();
  443. if ($filterIdProces > 0) {
  444. return $filterIdProces;
  445. }
  446. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  447. $fetchUsedProcesInitListSql = <<<SQL
  448. select p.`ID`, p.`DESC`
  449. from `CRM_PROCES` p
  450. where p.`ID` in({$sqlIdProcesListSql})
  451. and p.`TYPE`='PROCES_INIT'
  452. order by p.`SORT_PRIO`
  453. SQL;
  454. $usedProcesInitList = array();
  455. $db = DB::getDB();
  456. $res = $db->query($fetchUsedProcesInitListSql);
  457. while ($r = $db->fetch($res)) {
  458. $usedProcesInitList[$r->ID] = $r->DESC;
  459. }
  460. return $usedProcesInitList;
  461. }
  462. /**
  463. * Ids List of Proces Init for given tabel (skip filters)
  464. */
  465. public function getTableProcesInitIds($idTable) {
  466. $procesInitList = $this->getTableProcesInitList($idTable);
  467. return array_keys($procesInitList);
  468. }
  469. /**
  470. * List of Proces Init for given table (skip filters)
  471. */
  472. public function getTableProcesInitList($idTable) {
  473. $tableProcesInitList = ACL::getTableProcesInitList($idTable);
  474. if (!empty($tableProcesInitList)) {
  475. $filteredTableProcesInitList = array();
  476. DBG::_('DBG_MAP', '1', "tableProcesInitList({$idTable})", $tableProcesInitList, __CLASS__, __FUNCTION__, __LINE__);
  477. $procesIds = array_keys($tableProcesInitList);
  478. $sqlProcesIds = implode(",", $procesIds);
  479. $userLogin = User::getLogin();
  480. $sql = "select uiv.`ID_PROCES_INIT`, uiv.`DESC`
  481. from `CRM_PROCES_idx_USER_to_INIT_VIEW` uiv
  482. where uiv.`ADM_ACCOUNT`='{$userLogin}'
  483. and uiv.`ID_PROCES_INIT` in({$sqlProcesIds})
  484. group by uiv.`ID_PROCES_INIT`
  485. ";
  486. DBG::_('DBG_MAP', '1', "tableProcesInitList({$idTable}):sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
  487. $db = DB::getDB();
  488. $res = $db->query($sql);
  489. while ($r = $db->fetch($res)) {
  490. $filteredTableProcesInitList[$r->ID_PROCES_INIT] = $r->DESC;
  491. }
  492. $tableProcesInitList = $filteredTableProcesInitList;
  493. DBG::_('DBG_MAP', '1', "tableProcesInitList({$idTable}):filteredTableProcesInitList", $filteredTableProcesInitList, __CLASS__, __FUNCTION__, __LINE__);
  494. }
  495. return $tableProcesInitList;
  496. }
  497. public function canExecuteProcesInit($idProcesInit) {
  498. $isAllowed = false;
  499. $idProcesInit = (int)$idProcesInit;
  500. if (!$idProcesInit) return false;
  501. $idUserGroupList = $this->fetchGroups();
  502. $sqlIdUserGroupList = implode(",", array_keys($idUserGroupList));
  503. $checkProcesAccessSql = <<<SQL
  504. select count(*) as cnt
  505. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  506. join `CRM_PROCES` p on(p.`ID`=gi.`ID_PROCES` and p.`TYPE`='PROCES_INIT')
  507. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  508. and gi.`ID_PROCES`='{$idProcesInit}'
  509. SQL;
  510. $db = DB::getDB();
  511. $res = $db->query($checkProcesAccessSql);
  512. if ($r = $db->fetch($res)) {
  513. if ($r->cnt > 0) {
  514. $isAllowed = true;
  515. }
  516. }
  517. return $isAllowed;
  518. }
  519. public function canViewProces($idProcesInit) {
  520. $isAllowed = false;
  521. $idProcesInit = (int)$idProcesInit;
  522. if (!$idProcesInit) return false;
  523. $idUserGroupList = $this->fetchGroups();
  524. $sqlIdUserGroupList = implode(",", array_keys($idUserGroupList));
  525. $checkProcesAccessSql = <<<SQL
  526. select count(*) as cnt
  527. from `CRM_PROCES_idx_GROUP_to_PROCES_PERM` gi
  528. join `CRM_PROCES` p on(p.`ID`=gi.`ID_PROCES`)
  529. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  530. and gi.`ID_PROCES`='{$idProcesInit}'
  531. -- and gi.`HAS_PERM_R`=1 -- TODO: allow only with defined perm 'R'
  532. SQL;
  533. $db = DB::getDB();
  534. $res = $db->query($checkProcesAccessSql);
  535. if ($r = $db->fetch($res)) {
  536. if ($r->cnt > 0) {
  537. $isAllowed = true;
  538. }
  539. }
  540. return $isAllowed;
  541. }
  542. }