UserAcl.php 19 KB

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