UserUrlActionStorageAcl.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. DB::getPDO()->execSql("
  58. insert into `{$this->_rootTableName}` (ID_USER, A_LAST_SYNC, ID_PROCES, ID_URL, link, label, opis)
  59. select {$this->idUser}
  60. , NOW()
  61. , {$sqlIdProces}
  62. , upvg.`ID_URL`
  63. , upvg.`URL_LINK` as `link`
  64. , upvg.`URL_LABEL` as `label`
  65. , upvg.`URL_DESC` as `opis`
  66. from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  67. where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  68. group by upvg.`ID_URL`
  69. ");
  70. }
  71. public function getUsedUserProcesIdsSql() {
  72. if ($this->idProcesFilter > 0) {
  73. return "
  74. select i.`ID_PROCES`
  75. from `CRM_PROCES_idx` i
  76. where i.`idx_MAIN_PROCES_INIT_ID`='{$this->idProcesFilter}'
  77. ";
  78. }
  79. $idUserGroupList = $this->getUsedUserGroupIds();
  80. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  81. return "
  82. select gi.`ID_PROCES`
  83. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  84. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  85. ";
  86. }
  87. public function getUsedUserGroupIds() {
  88. // $idUserGroupList = User::getGroupsIds();
  89. // TODO: acl filtr by group ids
  90. return array_keys(UsersHelper::getGroupByUser($this->idUser));
  91. }
  92. public function getTotal($params = []) {
  93. // TODO: $sqlWhereAnd = $this->_parseSqlWhere($params);
  94. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and t.ID_PROCES = {$this->idProcesFilter}" : "and t.ID_PROCES is NULL";
  95. return DB::getPDO()->fetchValue("
  96. select count(1) as total
  97. from `{$this->_rootTableName}` t
  98. where t.ID_USER = {$this->idUser}
  99. {$sqlWhereAndIdProces}
  100. ");
  101. }
  102. public function _parseSqlWhere($params = []) {
  103. $sqlWhereAnd = [];
  104. // TODO: parse where/ogc, etc.
  105. if (!empty($params)) DBG::log($params, 'array', "\$params");
  106. if (!empty($params['f_link'])) {
  107. if (is_string($params['f_link'])) {
  108. if ('=' === substr($params['f_link'], 0, 1)) {
  109. $sqlWhereAnd[] = "t.link = " . DB::getPDO()->quote(substr($params['f_link'], 1));
  110. } else {
  111. $sqlWhereAnd[] = "t.link like " . DB::getPDO()->quote("%{$params['f_link']}%");
  112. }
  113. }
  114. }
  115. if (!empty($params)) DBG::log($sqlWhereAnd, 'array', "\$sqlWhereAnd");
  116. return (!empty($sqlWhereAnd)) ? "where " . implode(" and ", $sqlWhereAnd) : "";
  117. }
  118. public function getItems($params = []) {
  119. $sqlOrderBy = "";
  120. $sqlLimitOffset = "";
  121. $sqlWhereAnd = $this->_parseSqlWhere($params);
  122. $currSortCol = V::get('order_by', 'ID', $params);
  123. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  124. // TODO: validate $currSortCol is in field list
  125. // TODO: validate $currSortFlip ('asc' or 'desc')
  126. $aliasMap = array();
  127. foreach ($this->_simpleSchema['root'] as $key => $field) {
  128. if ('@' === substr($key, 0, 1)) continue;
  129. $aliasMap[ $key ] = (!empty($field['@alias'])) ? $field['@alias'] : $key;
  130. }
  131. // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort");
  132. $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null;
  133. if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) {
  134. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  135. }
  136. $limit = V::get('limit', 0, $params);
  137. $limit = ($limit < 0) ? 0 : $limit;
  138. $offset = V::get('limitstart', 0, $params);
  139. $offset = ($offset < 0) ? 0 : $offset;
  140. if ($limit > 0) $sqlLimitOffset = "limit {$limit} offset {$offset}";
  141. // $sql = "
  142. // select upvg.`ID_URL`
  143. // , upvg.`URL_LINK` as `link`
  144. // , upvg.`URL_LABEL` as `label`
  145. // , upvg.`URL_DESC` as `opis`
  146. // from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  147. // where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  148. // group by upvg.`ID_URL`
  149. // ";
  150. // $userAccessForUrls = DB::getPDO()->fetchAllByKey($sql, 'ID_URL');
  151. // $userAccessForUrls = DB::getPDO()->fetchAllByKey("
  152. // select c.ID_URL
  153. // , c.link
  154. // , c.label
  155. // , c.opis
  156. // from `{$this->_rootTableName}` as c
  157. // where c.ID_USER = {$idUser}
  158. // ", 'ID_URL');
  159. $sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
  160. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
  161. $items = DB::getPDO()->fetchAllByKey("
  162. select t.ID_URL
  163. , t.ID_USER
  164. , t.ID_PROCES
  165. , t.link
  166. , t.label
  167. , t.opis
  168. from (
  169. select c.ID_URL
  170. , c.ID_USER
  171. , c.ID_PROCES
  172. , c.link
  173. , c.label
  174. , c.opis
  175. from `{$this->_rootTableName}` as c
  176. where c.ID_USER = {$this->idUser}
  177. {$sqlWhereAndIdProces}
  178. ) as t
  179. {$sqlWhereAnd}
  180. {$sqlOrderBy}
  181. {$sqlLimitOffset}
  182. ", 'ID_URL');
  183. // array_walk($items, function (&$item, $key) {
  184. // $item['link_uruchom_filtr_procesu'] = Request::getPathUri() . "index.php?FUNCTION_INIT=MENU_SELECT_PROCES&_action=setPermsByProces&id_proces={$item['ID']}";
  185. // });
  186. return $items;
  187. }
  188. }