UserUrlActionStorageAcl.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. Lib::loadClass('UsersHelper');
  5. /**
  6. * @require CRM_PROCES_idx_URL_TO_PROCES_VIEW from FixCrmProcesInitIdx
  7. * @require CRM_PROCES_idx_GROUP_to_PROCES from FixCrmProcesInitIdx
  8. * @require idUser (ref from parent object - User)
  9. */
  10. class Schema_UserUrlActionStorageAcl extends Core_AclSimpleSchemaBase {
  11. public $_simpleSchema = [
  12. 'root' => [
  13. '@namespace' => 'default_objects/UserUrlAction',
  14. '@primaryKey' => 'ID_URL',
  15. 'ID_URL' => [ '@type' => 'xsd:integer' ], // `ID_URL` int(11) NOT NULL,
  16. 'ID_USER' => [ '@type' => 'xsd:integer' ], // `ID_USER` int(11) NOT NULL,
  17. 'ID_PROCES' => [ '@type' => 'xsd:integer' ], // `ID_PROCES` int(11) DEFAULT NULL,
  18. 'link' => [ '@type' => 'p5:www_link' ], // `link` varchar(255) DEFAULT '',
  19. 'label' => [ '@type' => 'xsd:string' ], // `label` varchar(255) DEFAULT '',
  20. 'opis' => [ '@type' => 'xsd:string' ], // `opis` text,
  21. 'A_LAST_SYNC' => [ '@type' => 'xsd:dateTime' ], // `A_LAST_SYNC` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  22. // TODO: UNIQUE KEY `uniq` (ID_USER, ID_URL, ID_PROCES)
  23. ]
  24. ];
  25. public $_rootTableName = 'CRM_AUTH_#CACHE_ACL_URL_ACTION';
  26. public $idUser = null;
  27. public $idProcesFilter = null;
  28. public function __construct($simpleSchema = null) {
  29. parent::__construct($simpleSchema);
  30. $this->idUser = User::getID();// default - current user
  31. }
  32. public function setIdUser($idUser) {
  33. $this->idUser = intval($idUser);
  34. if ($this->idUser <= 0) throw new Exception("Missing id user");
  35. }
  36. public function getIdUser() { return $this->idUser; }
  37. public function setIdProcesFilter($idProcesFilter) { $this->idProcesFilter = intval($idProcesFilter); }
  38. public function getIdProcesFilter() { return $this->idProcesFilter; }
  39. public function updateCacheFeatures() {
  40. $sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
  41. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
  42. $sqlNoPrefixWhereAndIdProces = ($this->idProcesFilter > 0) ? "and ID_PROCES = {$this->idProcesFilter}" : "and ID_PROCES is NULL";
  43. DB::getPDO()->execSql("
  44. create table if not exists `{$this->_rootTableName}` (
  45. `ID_USER` int(11) NOT NULL,
  46. `ID_URL` int(11) NOT NULL,
  47. `ID_PROCES` int(11) DEFAULT NULL,
  48. `link` varchar(255) DEFAULT '',
  49. `label` varchar(255) DEFAULT '',
  50. `opis` text,
  51. `A_LAST_SYNC` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  52. UNIQUE KEY `uniq` (ID_USER, ID_URL, ID_PROCES)
  53. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  54. ");
  55. DB::getPDO()->execSql(" delete from `{$this->_rootTableName}` where ID_USER = {$this->idUser} {$sqlNoPrefixWhereAndIdProces} ");
  56. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  57. if ($sqlIdProcesListSql) {
  58. DB::getPDO()->execSql("
  59. insert into `{$this->_rootTableName}` (ID_USER, A_LAST_SYNC, ID_PROCES, ID_URL, link, label, opis)
  60. select {$this->idUser}
  61. , NOW()
  62. , {$sqlIdProces}
  63. , upvg.`ID_URL`
  64. , upvg.`URL_LINK` as `link`
  65. , upvg.`URL_LABEL` as `label`
  66. , upvg.`URL_DESC` as `opis`
  67. from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  68. where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  69. group by upvg.`ID_URL`
  70. ");
  71. }
  72. }
  73. public function getUsedUserProcesIdsSql() {
  74. if ($this->idProcesFilter > 0) {
  75. return "
  76. select i.`ID_PROCES`
  77. from `CRM_PROCES_idx` i
  78. where i.`idx_MAIN_PROCES_INIT_ID`='{$this->idProcesFilter}'
  79. ";
  80. }
  81. $idUserGroupList = $this->getUsedUserGroupIds();
  82. if (empty($idUserGroupList)) return null;
  83. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  84. return "
  85. select gi.`ID_PROCES`
  86. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  87. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  88. ";
  89. }
  90. public function getUsedUserGroupIds() {
  91. // $idUserGroupList = User::getGroupsIds();
  92. // TODO: acl filtr by group ids
  93. return array_keys(UsersHelper::getGroupByUser($this->idUser));
  94. }
  95. public function getTotal($params = []) {
  96. // TODO: $sqlWhereAnd = $this->_parseSqlWhere($params);
  97. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and t.ID_PROCES = {$this->idProcesFilter}" : "and t.ID_PROCES is NULL";
  98. return DB::getPDO()->fetchValue("
  99. select count(1) as total
  100. from `{$this->_rootTableName}` t
  101. where t.ID_USER = {$this->idUser}
  102. {$sqlWhereAndIdProces}
  103. ");
  104. }
  105. public function _parseSqlWhere($params = []) {
  106. $sqlWhereAnd = [];
  107. // TODO: parse where/ogc, etc.
  108. if (!empty($params)) DBG::log($params, 'array', "\$params");
  109. if (!empty($params['f_link'])) {
  110. if (is_string($params['f_link'])) {
  111. if ('=' === substr($params['f_link'], 0, 1)) {
  112. $sqlWhereAnd[] = "t.link = " . DB::getPDO()->quote(substr($params['f_link'], 1));
  113. } else {
  114. $sqlWhereAnd[] = "t.link like " . DB::getPDO()->quote("%{$params['f_link']}%");
  115. }
  116. }
  117. }
  118. if (!empty($params)) DBG::log($sqlWhereAnd, 'array', "\$sqlWhereAnd");
  119. return (!empty($sqlWhereAnd)) ? "where " . implode(" and ", $sqlWhereAnd) : "";
  120. }
  121. public function getItems($params = []) {
  122. $sqlOrderBy = "";
  123. $sqlLimitOffset = "";
  124. $sqlWhereAnd = $this->_parseSqlWhere($params);
  125. $currSortCol = V::get('order_by', 'ID', $params);
  126. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  127. // TODO: validate $currSortCol is in field list
  128. // TODO: validate $currSortFlip ('asc' or 'desc')
  129. $aliasMap = array();
  130. foreach ($this->_simpleSchema['root'] as $key => $field) {
  131. if ('@' === substr($key, 0, 1)) continue;
  132. $aliasMap[ $key ] = (!empty($field['@alias'])) ? $field['@alias'] : $key;
  133. }
  134. // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort");
  135. $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null;
  136. if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) {
  137. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  138. }
  139. $limit = V::get('limit', 0, $params);
  140. $limit = ($limit < 0) ? 0 : $limit;
  141. $offset = V::get('limitstart', 0, $params);
  142. $offset = ($offset < 0) ? 0 : $offset;
  143. if ($limit > 0) $sqlLimitOffset = "limit {$limit} offset {$offset}";
  144. // $sql = "
  145. // select upvg.`ID_URL`
  146. // , upvg.`URL_LINK` as `link`
  147. // , upvg.`URL_LABEL` as `label`
  148. // , upvg.`URL_DESC` as `opis`
  149. // from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  150. // where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  151. // group by upvg.`ID_URL`
  152. // ";
  153. // $userAccessForUrls = DB::getPDO()->fetchAllByKey($sql, 'ID_URL');
  154. // $userAccessForUrls = DB::getPDO()->fetchAllByKey("
  155. // select c.ID_URL
  156. // , c.link
  157. // , c.label
  158. // , c.opis
  159. // from `{$this->_rootTableName}` as c
  160. // where c.ID_USER = {$idUser}
  161. // ", 'ID_URL');
  162. $sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
  163. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
  164. $items = DB::getPDO()->fetchAllByKey("
  165. select t.ID_URL
  166. , t.ID_USER
  167. , t.ID_PROCES
  168. , t.link
  169. , t.label
  170. , t.opis
  171. from (
  172. select c.ID_URL
  173. , c.ID_USER
  174. , c.ID_PROCES
  175. , c.link
  176. , c.label
  177. , c.opis
  178. from `{$this->_rootTableName}` as c
  179. where c.ID_USER = {$this->idUser}
  180. {$sqlWhereAndIdProces}
  181. ) as t
  182. {$sqlWhereAnd}
  183. {$sqlOrderBy}
  184. {$sqlLimitOffset}
  185. ", 'ID_URL');
  186. // array_walk($items, function (&$item, $key) {
  187. // $item['link_uruchom_filtr_procesu'] = Request::getPathUri() . "index.php?FUNCTION_INIT=MENU_SELECT_PROCES&_action=setPermsByProces&id_proces={$item['ID']}";
  188. // });
  189. return $items;
  190. }
  191. }