UserAcl.php 21 KB

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