AccessOwnerStorageAcl.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. class Schema_AccessOwnerStorageAcl extends Core_AclBase {
  5. public function getNamespace() { return 'default_objects/' . $this->getName(); }
  6. public function getSourceName() { return 'default_objects'; }
  7. public function init($force = false) {}
  8. public function isInitialized() { return true; }
  9. public function getName() { return 'AccessOwner'; }
  10. public function getRootTableName() { return 'ADMIN_USERS'; }// TODO: turn off - use getName for generating ref's
  11. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  12. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  13. public function getVirtualFieldListByIdZasob() { return array(); }
  14. public function getXsdTypes() { // @returns [ fieldName => xsdType, ... ]
  15. return array_map(function ($field) {
  16. return $field['xsdType'];
  17. }, $this->getFieldsWithXsdTypes());
  18. }
  19. public function getFieldsWithXsdTypes() {
  20. $xsdTypes = array();
  21. foreach ($this->getFields() as $idZasob => $field) {
  22. $xsdTypes[ $field['name'] ] = $field;
  23. $xsdTypes[ $field['name'] ][ 'xsdType' ] = $this->getXsdFieldType($field['name']);
  24. }
  25. return $xsdTypes;
  26. }
  27. public function getRealFieldListByIdZasob($force = false) {
  28. $cols = array();
  29. $cols[100000] = 'id';// ADMIN_USERS.ID
  30. $cols[100001] = 'login';// ADMIN_USERS.ADM_ACCOUNT
  31. $cols[100002] = 'name';// ADMIN_USERS.ADM_NAME
  32. return $cols;
  33. }
  34. public function getFields() {
  35. $fields = array();
  36. $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100];
  37. $fields[100001] = ['name'=>'login', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101];
  38. $fields[100002] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102];
  39. return $fields;
  40. }
  41. public function getSqlFieldName($fieldName) {
  42. switch ($fieldName) {
  43. case 'id': return 'ID';
  44. case 'login': return 'ADM_ACCOUNT';
  45. case 'name': return 'ADM_NAME';
  46. }
  47. throw new Exception("Unknown field '{$fieldName}' in AccessOwner");
  48. }
  49. public function getFieldType($fieldName) { return null; }
  50. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  51. public function canCreateField($fieldName) { return false; }
  52. public function canReadField($fieldName) { return true; }
  53. public function canReadObjectField($fieldName, $record) {return true; }
  54. public function canWriteField($fieldName) { return false; }
  55. public function canWriteObjectField($fieldName, $record) { return false; }
  56. public function getTotal($params = array()) {
  57. return count($this->getItems($params));
  58. }
  59. public function getItem($primaryKey, $params = []) {
  60. $items = $this->getItems(['primaryKey'=>$primaryKey]);
  61. return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
  62. }
  63. public function getItems($params = array()) {
  64. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  65. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  66. DBG::log($params, 'array', "AccessOwner::getItems \$params");
  67. $sqlWhereAndPk = "";
  68. if ($pk = V::get('@primaryKey', null, $params)) {// [@primaryKey] => TODO: int or array of int
  69. if (!is_array($pk)) $sqlPk = array($pk);
  70. else $sqlPk = $pk;
  71. $sqlWhereAndPk = "and t.ID in (" . implode(", ", $sqlPk) . ")";
  72. }
  73. $sqlWhereAndBackRef = "";
  74. if (!empty($params['__backRef'])) {
  75. $backRef = $params['__backRef'];
  76. if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
  77. if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
  78. if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
  79. if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
  80. // TODO: $this->_query->where([ '__backRef' ]); or $this->_query->join([ '__backRef' ]);
  81. $refAcl = ACL::getAclByNamespace($backRef['namespace']);
  82. if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source");
  83. $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
  84. $sqlPk = $this->getAclSqlPrimaryKeyField();
  85. $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
  86. $sqlWhereAndBackRef = "
  87. and t.{$sqlPk} in (
  88. select refTable.REMOTE_PRIMARY_KEY
  89. from `{$refTable}` refTable
  90. where refTable.PRIMARY_KEY = {$sqlBackRefPk}
  91. )
  92. ";
  93. }
  94. $items = DB::getPDO()->fetchAllByKey("
  95. select t.ID as id, t.ADM_ACCOUNT as login, t.ADM_NAME as name
  96. from ADMIN_USERS t
  97. where t.A_STATUS = 'NORMAL'
  98. and t.ADM_TECH_WORKER != 'NO'
  99. {$sqlWhereAndPk}
  100. {$sqlWhereAndBackRef}
  101. ", $key = 'id');
  102. if ($pk = V::get('primaryKey', 0, $params, 'int')) {// [primaryKey] => 2948
  103. if (!array_key_exists($pk, $items)) return array();
  104. $items = array($pk => $items[$pk]);
  105. }
  106. if (!empty($params['ogc:Filter'])) {
  107. $parser = new ParseOgcFilter();
  108. $parser->loadOgcFilter($params['ogc:Filter']);
  109. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  110. DBG::_('DBG_DS', '>2', "ogc:Filter \$queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  111. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  112. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  113. }
  114. $filterId = trim(V::get('f_id', '', $params));
  115. if (strlen($filterId)) {// allow '0'
  116. $queryWhereBuilder = new SqlQueryWhereBuilder();
  117. if (is_numeric($filterId)) {
  118. $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId);
  119. } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) {
  120. $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId);
  121. } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  122. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2));
  123. } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  124. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2));
  125. } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  126. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1));
  127. } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  128. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1));
  129. } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  130. $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1));
  131. } else {
  132. $filterId = null;// TODO: BUG uniimplemented comparison sign
  133. }
  134. if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  135. }
  136. foreach (['login', 'name'] as $fieldName) {
  137. $filterValue = trim(V::get("f_{$fieldName}", '', $params));
  138. if (strlen($filterValue)) {// allow '0'
  139. $queryWhereBuilder = new SqlQueryWhereBuilder();
  140. if (!is_scalar($filterValue)) {
  141. } else if ('=' == substr($filterValue, 0, 1)) {
  142. $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1));
  143. } else {
  144. if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}";
  145. if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%";
  146. $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue);
  147. }
  148. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  149. }
  150. }
  151. $orderBy = strtolower(V::get('order_by', 'id', $params));
  152. $orderDir = strtolower(V::get('order_dir', 'desc', $params));
  153. if (!in_array($orderBy, ['id', 'login', 'name'])) throw new HttpException("Bad Request - wrong or missing order by", 400);
  154. if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400);
  155. uasort($items, function ($a, $b) use ($orderBy, $orderDir) {
  156. if ('desc' == $orderDir) {
  157. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1;
  158. } else if ('asc' == $orderDir) {
  159. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1;
  160. }
  161. return 0;
  162. });
  163. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  164. return $items;
  165. }
  166. public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
  167. public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
  168. public function getGeomFieldType($fieldName) { return null; }
  169. public function getPrimaryKeyField() { return 'id'; }
  170. public function getAclSqlPrimaryKeyField() { return 'ID'; }
  171. public function getAttributesFromZasoby() { return array(); }
  172. public function isEnumerationField($fieldName) { return false; }
  173. public function getEnumerations($fieldName) { return null; }
  174. public function getXsdFieldType($fieldName) {
  175. if ('id' == $fieldName) return 'xsd:string';
  176. if ('login' == $fieldName) return 'xsd:string';
  177. if ('name' == $fieldName) return 'xsd:string';
  178. }
  179. public function isGeomField($fldName) { return false; }
  180. }