UserObjectStorageAcl.php 8.1 KB

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