UserAcl.php 19 KB

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