UserAcl.php 18 KB

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