UserAcl.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. return TableAcl::getInstance($tableID);
  163. }
  164. public function getProcesInitList($tblId) {
  165. if ($tblId > 0) {
  166. $map = $this->_cache_read('foundMap');
  167. if (is_array($map) && array_key_exists($tblId, $map)) {
  168. return $map[$tblId];
  169. }
  170. }
  171. return null;
  172. }
  173. /**
  174. * Check if perms are only for one proces.
  175. * @returns int or false
  176. */
  177. public function getPermsFiltrProcesId() {// TODO: RMME mved to getFilterIdProces
  178. return $this->getFilterIdProces();
  179. }
  180. public function getFilterIdProces() {
  181. $procesID = $this->_cache_read('permsByProcesID');
  182. return ($procesID > 0)? $procesID : false;
  183. }
  184. public function fetchAllPerms($force = false) {
  185. $this->_fetchPerms('All', $force);
  186. }
  187. public function fetchProcesPerms($procesID, $force = false) {
  188. $this->_fetchPerms($procesID, $force);
  189. }
  190. /**
  191. * @param $type - 'All', $procesID
  192. */
  193. private function _fetchPerms($type, $force = false) {
  194. $db = DB::getDB();
  195. $procesID = 0;// if 0 - All, alse perms by procesID
  196. $foundUrls = array();
  197. $foundTbls = array();
  198. if ($force) {
  199. $this->_cache_clear();
  200. }
  201. $schemaReader = new SchemaReader();
  202. if ($type == 'All') {
  203. $schemaReader->getAll();
  204. } else if (is_numeric($type) && $type > 0) {
  205. $procesID = (int)$type;
  206. }
  207. {// fetch from schema files in SE/schema/process/*.ini.php
  208. if ($schemaReader->hasProcessConfigs()) {
  209. foreach ($schemaReader->getProcessConfigs() as $process) {
  210. DBG::_('DBG_SCH', '1', "process", $process, __CLASS__, __FUNCTION__, __LINE__ );
  211. if ($process->hasAccess()) {
  212. $tables = $process->getTables();
  213. DBG::_('DBG_SCH', '1', "tables", $tables, __CLASS__, __FUNCTION__, __LINE__ );
  214. foreach ($tables as $vTable) {
  215. $tblUri = $vTable->getUri();
  216. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri);
  217. DBG::_('DBG_SCH', '1', "table(" . $vTable->getLabel() . ")", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__ );
  218. if ($zasobTblInfo) {
  219. $idTable = $zasobTblInfo->ID;
  220. if (!array_key_exists($idTable, $foundTbls)) {
  221. $tableConfig = array();
  222. $tableConfig['ID_TABLE'] = $idTable;
  223. $tableConfig['db'] = $zasobTblInfo->P__ID;
  224. $tableConfig['name'] = $zasobTblInfo->DESC;
  225. $tableConfig['label'] = $zasobTblInfo->DESC_PL;
  226. $tableConfig['opis'] = $zasobTblInfo->OPIS;
  227. $foundTbls[$idTable] = $tableConfig;
  228. }
  229. $tableAcl = TableAcl::buildInstance($idTable, $foundTbls[$idTable]);
  230. $fieldsConfig = array();
  231. $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable);
  232. foreach ($vTable->getFields() as $field) {
  233. $fldInfo = V::get($field->getName(), null, $fldsInfo);
  234. if ($fldInfo) {
  235. if (!array_key_exists($fldInfo->ID, $fieldsConfig)) {//if (!$tableAcl->hasField($fldInfo->ID)) {
  236. $fieldsConfig[$fldInfo->ID] = array();
  237. $fieldsConfig[$fldInfo->ID]['ID_CELL'] = $fldInfo->ID;
  238. $fieldsConfig[$fldInfo->ID]['CELL_NAME'] = $fldInfo->DESC;
  239. $fieldsConfig[$fldInfo->ID]['CELL_DESC'] = $fldInfo->OPIS;
  240. $fieldsConfig[$fldInfo->ID]['SORT_PRIO'] = $fldInfo->SORT_PRIO;
  241. $fieldsConfig[$fldInfo->ID]['CELL_LABEL'] = $fldInfo->DESC_PL;
  242. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] = '';
  243. //$tableAcl->addField($fldInfo->ID, $fldInfo->DESC, $fldInfo->OPIS, $fldInfo->SORT_PRIO, $fldInfo->DESC_PL);
  244. }
  245. // TODO: $field->getPerms() -> PERM_R, PERM_W, ... etc.?
  246. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] .= $field->getPerms();//$tableAcl->setFieldPerms($fldInfo->ID, $field->getPerms());
  247. }
  248. }
  249. $tableAcl->initFieldsFromConfig($fieldsConfig);
  250. DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  251. $tableAcl->save();
  252. DBG::_('DBG_SCH', '1', "tableAcl({$idTable})", $tableAcl, __CLASS__, __FUNCTION__, __LINE__);
  253. }
  254. }
  255. }
  256. }
  257. } else {
  258. DBG::_('DBG_SCH', '1', "NO \$schemaReader->hasProcessConfigs()", null, __CLASS__, __FUNCTION__, __LINE__);
  259. }
  260. DBG::_('DBG_SCH', '1', "foundTbls", $foundTbls, __CLASS__, __FUNCTION__, __LINE__);
  261. }// fetch from schema files
  262. $this->setFilterIdProces($procesID);//$this->_cache_save('permsByProcesID', $procesID);
  263. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  264. $userAccessForTables = array();
  265. $sql = <<<SQL
  266. select tpvg.`ID_TABLE`
  267. , tpvg.`ID_DATABASE` as `db`
  268. , tpvg.`TABLE_NAME` as `name`
  269. , tpvg.`TABLE_LABEL` as `label`
  270. , tpvg.`TABLE_DESCRIPTION` as `opis`
  271. from `CRM_PROCES_idx_TABLE_TO_PROCES_GROUPED_VIEW` as tpvg
  272. where tpvg.`ID_PROCES` in({$sqlIdProcesListSql})
  273. group by tpvg.`ID_TABLE`
  274. SQL;
  275. //echo'<pre>$userAccessForTables - $sql ';print_r($sql);echo'</pre>';
  276. $db = DB::getDB();
  277. $res = $db->query($sql);
  278. while ($h = $db->fetch_assoc($res)) {
  279. $userAccessForTables[$h['ID_TABLE']] = $h;
  280. }
  281. DBG::_('DBG_SCH', '1', "userAccessForTables", $userAccessForTables, __CLASS__, __FUNCTION__, __LINE__ );
  282. foreach ($userAccessForTables as $idTable => $tableConfig) {
  283. if (!array_key_exists($idTable, $foundTbls)) {
  284. $foundTbls[$idTable] = $tableConfig;
  285. }
  286. }
  287. $userAccessForUrls = array();
  288. $sql = <<<SQL
  289. select upvg.`ID_URL`
  290. , upvg.`URL_LINK` as `link`
  291. , upvg.`URL_LABEL` as `label`
  292. , upvg.`URL_DESC` as `opis`
  293. from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  294. where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  295. group by upvg.`ID_URL`
  296. SQL;
  297. //echo'<pre>$userAccessForUrls - $sql ';print_r($sql);echo'</pre>';
  298. $db = DB::getDB();
  299. $res = $db->query($sql);
  300. while ($h = $db->fetch_assoc($res)) {
  301. $userAccessForUrls[$h['ID_URL']] = $h;
  302. }
  303. //echo'<pre>$userAccessForUrls ';print_r($userAccessForUrls);echo'</pre>';
  304. DBG::_('DBG_SCH', '2', "userAccessForUrls", $userAccessForUrls, __CLASS__, __FUNCTION__, __LINE__ );
  305. foreach ($userAccessForUrls as $idUrl => $vUrlConfig) {
  306. $foundUrls[$idUrl] = $vUrlConfig['opis'];
  307. }
  308. $this->_cache_save('foundUrls', $foundUrls);
  309. $this->_cache_save('foundTables', $foundTbls);
  310. }
  311. public function setFilterIdProces($procesID) {
  312. $this->_cache_save('permsByProcesID', $procesID);
  313. }
  314. public function getPermsForTable($idTable) {
  315. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  316. $tableCellToProcesSql = <<<SQL
  317. select z.`ID` as `ID_CELL`
  318. , z.`DESC` as `CELL_NAME`
  319. , z.`DESC_PL` as `CELL_LABEL`
  320. , z.`OPIS` as `CELL_DESC`
  321. , z.`SORT_PRIO` as `SORT_PRIO`
  322. , zp.`ID` as `ID_TABLE`
  323. -- , zp.`DESC` as `TABLE_NAME`
  324. -- , wsk.`ID_PROCES` as `ID_PROCES`
  325. , sum(IF(przyp.`FORM_TREAT` & 2, 1, 0)) as PERM_R
  326. , sum(IF(przyp.`FORM_TREAT` & 4, 1, 0)) as PERM_W
  327. , sum(IF(przyp.`FORM_TREAT` & 8, 1, 0)) as PERM_X
  328. , sum(IF(przyp.`FORM_TREAT` & 16, 1, 0)) as PERM_C
  329. , sum(IF(przyp.`FORM_TREAT` & 32, 1, 0)) as PERM_S
  330. , sum(IF(przyp.`FORM_TREAT` & 64, 1, 0)) as PERM_O
  331. , sum(IF(przyp.`FORM_TREAT` & 128, 1, 0)) as PERM_V
  332. , sum(IF(przyp.`FORM_TREAT` & 256, 1, 0)) as PERM_E
  333. from `CRM_LISTA_ZASOBOW` z
  334. join `CRM_LISTA_ZASOBOW` zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE`='TABELA' and zp.`A_STATUS` in('WAITING','NORMAL'))
  335. join `CRM_WSKAZNIK` wsk on(wsk.`ID_ZASOB`=z.`ID` and wsk.`A_STATUS` in('WAITING','NORMAL'))
  336. join `CRM_PROCES` p on(p.`ID`=wsk.`ID_PROCES` and p.`A_STATUS` in('WAITING','NORMAL'))
  337. join `CRM_PRZYPADEK` as przyp on (przyp.`ID`=wsk.`ID_PRZYPADEK`)
  338. where z.`TYPE`='KOMORKA'
  339. and z.`A_STATUS` in('WAITING','NORMAL')
  340. and zp.`ID`='{$idTable}'
  341. and wsk.`ID_PROCES` in({$sqlIdProcesListSql})
  342. -- group by z.`ID`, wsk.`ID_PROCES`
  343. group by z.`ID`
  344. order by z.`SORT_PRIO`
  345. SQL;
  346. //echo'<pre>UserAcl::getPermsForTable('.$idTable.')::$tableCellToProcesSql ';print_r($tableCellToProcesSql);echo'</pre>';
  347. $userPermsForTable = array();
  348. $db = DB::getDB();
  349. $res = $db->query($tableCellToProcesSql);
  350. while ($h = $db->fetch_assoc($res)) {
  351. $idCell = $h['ID_CELL'];
  352. $userPermsForTable[$idCell] = $h;
  353. }
  354. return $userPermsForTable;
  355. }
  356. public function getUsedUserGroupIds() {
  357. $idUserGroupList = User::getGroupsIds();
  358. // TODO: acl filtr by group ids
  359. return $idUserGroupList;
  360. }
  361. public function getUsedUserProcesIdsSql() {
  362. $filterIdProces = $this->getFilterIdProces();
  363. if ($filterIdProces > 0) {
  364. return <<<SQL
  365. select i.`ID_PROCES`
  366. from `CRM_PROCES_idx` i
  367. where i.`idx_PROCES_INIT_ID`='{$filterIdProces}'
  368. SQL;
  369. }
  370. $idUserGroupList = $this->getUsedUserGroupIds();
  371. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  372. return <<<SQL
  373. select gi.`ID_PROCES`
  374. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  375. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  376. SQL;
  377. }
  378. public function getProcesMaxUpdateDate($idProcesInit) {
  379. $maxUpdateDate = null;
  380. $db = DB::getDB();
  381. $sql = <<<SQL
  382. select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  383. from `CRM_PROCES` as p
  384. where p.`ID` in(
  385. select i.`ID_PROCES`
  386. from `CRM_PROCES_idx` i
  387. where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  388. )
  389. SQL;
  390. $res = $db->query($sql);
  391. if ($r = $db->fetch($res)) {
  392. $maxUpdateDate = $r->max_update_date;
  393. }
  394. return $maxUpdateDate;
  395. }
  396. /**
  397. * Ids List of Proces Init for user (skip filters)
  398. */
  399. public function getUserProcesInitIds() {
  400. $procesInitList = $this->getUserProcesInitList();
  401. return array_keys($procesInitList);
  402. }
  403. /**
  404. * List of Proces Init for user (skip filters)
  405. */
  406. public function getUserProcesInitList() {
  407. $userProcesInitList = array();
  408. $idUserGroupList = User::getGroupsIds();
  409. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  410. $sqlIdProcesListSql = <<<SQL
  411. select gi.`ID_PROCES`
  412. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  413. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  414. SQL;
  415. $fetchUserProcesInitListSql = <<<SQL
  416. select p.`ID`, p.`DESC`
  417. from `CRM_PROCES_idx` i
  418. join `CRM_PROCES` p on(p.`ID`=i.`idx_PROCES_INIT_ID`)
  419. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  420. group by p.`ID`
  421. order by p.`SORT_PRIO`
  422. SQL;
  423. $db = DB::getDB();
  424. $res = $db->query($fetchUserProcesInitListSql);
  425. while ($r = $db->fetch($res)) {
  426. $userProcesInitList[$r->ID] = $r->DESC;
  427. }
  428. return $userProcesInitList;
  429. }
  430. /**
  431. * Ids List of Proces Init for user (use filters)
  432. */
  433. public function getUsedUserProcesInitIds() {
  434. $usedProcesInitList = $this->getUsedUserProcesInitList();
  435. return array_keys($usedProcesInitList);
  436. }
  437. /**
  438. * List of Proces Init for user (use filters)
  439. */
  440. public function getUsedUserProcesInitList() {
  441. $filterIdProces = $this->getFilterIdProces();
  442. if ($filterIdProces > 0) {
  443. return $filterIdProces;
  444. }
  445. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  446. $fetchUsedProcesInitListSql = <<<SQL
  447. select p.`ID`, p.`DESC`
  448. from `CRM_PROCES` p
  449. where p.`ID` in({$sqlIdProcesListSql})
  450. and p.`TYPE`='PROCES_INIT'
  451. order by p.`SORT_PRIO`
  452. SQL;
  453. $usedProcesInitList = array();
  454. $db = DB::getDB();
  455. $res = $db->query($fetchUsedProcesInitListSql);
  456. while ($r = $db->fetch($res)) {
  457. $usedProcesInitList[$r->ID] = $r->DESC;
  458. }
  459. return $usedProcesInitList;
  460. }
  461. /**
  462. * Ids List of Proces Init for given tabel (skip filters)
  463. */
  464. public function getTableProcesInitIds($idTable) {// TODO: use in TableAjax
  465. $procesInitList = $this->getTableProcesInitList($idTable);
  466. return array_keys($procesInitList);
  467. }
  468. /**
  469. * List of Proces Init for given table (skip filters)
  470. */
  471. public function getTableProcesInitList($idTable) {// TODO: use in TableAjax
  472. $tableProcesInitList = array();
  473. $sqlIdProcesListSql = <<<SQL
  474. select tpv.`ID_PROCES`
  475. from `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
  476. where tpv.`ID_TABLE`='{$idTable}'
  477. SQL;
  478. $fetchTableProcesInitListSql = <<<SQL
  479. select p.`ID`, p.`DESC`
  480. from `CRM_PROCES_idx` i
  481. join `CRM_PROCES` p on(p.`ID`=i.`idx_PROCES_INIT_ID`)
  482. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  483. group by p.`ID`
  484. order by p.`SORT_PRIO`
  485. SQL;
  486. $tableProcesInitList = array();
  487. $db = DB::getDB();
  488. $res = $db->query($fetchTableProcesInitListSql);
  489. while ($r = $db->fetch($res)) {
  490. $tableProcesInitList[$r->ID] = $r->DESC;
  491. }
  492. return $tableProcesInitList;
  493. }
  494. public function canExecuteProcesInit($idProcesInit) {
  495. $canExecuteProcesInit = false;
  496. $idProcesInit = (int)$idProcesInit;
  497. if (!$idProcesInit) return false;
  498. $idUserGroupList = User::getGroupsIds();
  499. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  500. $checkProcesAccessSql = <<<SQL
  501. select count(*) as cnt
  502. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  503. join `CRM_PROCES` p on(p.`ID`=gi.`ID_PROCES` and p.`TYPE`='PROCES_INIT')
  504. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  505. and gi.`ID_PROCES`='{$idProcesInit}'
  506. SQL;
  507. $db = DB::getDB();
  508. $res = $db->query($checkProcesAccessSql);
  509. if ($r = $db->fetch($res)) {
  510. if ($r->cnt > 0) {
  511. $canExecuteProcesInit = true;
  512. }
  513. }
  514. return $canExecuteProcesInit;
  515. }
  516. }