AccessOwnerStorageAcl.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 getRealFieldListByIdZasob($force = false) {
  15. $cols = array();
  16. $cols[100000] = 'id';// ADMIN_USERS.ID
  17. $cols[100001] = 'login';// ADMIN_USERS.ADM_ACCOUNT
  18. $cols[100002] = 'name';// ADMIN_USERS.ADM_NAME
  19. return $cols;
  20. }
  21. public function getFields() {
  22. $fields = array();
  23. $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100];
  24. $fields[100001] = ['name'=>'login', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101];
  25. $fields[100002] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102];
  26. return $fields;
  27. }
  28. public function getFieldType($fieldName) { return null; }
  29. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  30. public function canCreateField($fieldName) { return false; }
  31. public function canReadField($fieldName) { return true; }
  32. public function canReadObjectField($fieldName, $record) {return true; }
  33. public function canWriteField($fieldName) { return false; }
  34. public function canWriteObjectField($fieldName, $record) { return false; }
  35. public function getTotal($params = array()) {
  36. return count($this->getItems($params));
  37. }
  38. public function getItem($primaryKey) {
  39. $items = $this->getItems(['primaryKey'=>$primaryKey]);
  40. return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
  41. }
  42. public function getItems($params = array()) {
  43. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  44. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  45. $items = DB::getPDO()->fetchAllByKey("
  46. select u.ID as id, u.ADM_ACCOUNT as login, u.ADM_NAME as name
  47. from ADMIN_USERS u
  48. where u.A_STATUS = 'NORMAL'
  49. and u.ADM_TECH_WORKER != 'NO'
  50. ", $key = 'id');
  51. if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
  52. if (!array_key_exists($pk, $items)) return array();
  53. $items = array($pk => $items[$pk]);
  54. }
  55. if (!empty($params['ogc:Filter'])) {
  56. $parser = new ParseOgcFilter();
  57. $parser->loadOgcFilter($params['ogc:Filter']);
  58. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  59. DBG::_('DBG_DS', '>2', "ogc:Filter \$queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  60. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  61. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  62. }
  63. $filterId = trim(V::get('f_id', '', $params));
  64. if (strlen($filterId)) {// allow '0'
  65. $queryWhereBuilder = new SqlQueryWhereBuilder();
  66. if (is_numeric($filterId)) {
  67. $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId);
  68. } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) {
  69. $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId);
  70. } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  71. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2));
  72. } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  73. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2));
  74. } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  75. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1));
  76. } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  77. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1));
  78. } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  79. $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1));
  80. } else {
  81. $filterId = null;// TODO: BUG uniimplemented comparison sign
  82. }
  83. if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  84. }
  85. foreach (['login', 'name'] as $fieldName) {
  86. $filterValue = trim(V::get("f_{$fieldName}", '', $params));
  87. if (strlen($filterValue)) {// allow '0'
  88. $queryWhereBuilder = new SqlQueryWhereBuilder();
  89. if (!is_scalar($filterValue)) {
  90. } else if ('=' == substr($filterValue, 0, 1)) {
  91. $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1));
  92. } else {
  93. if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}";
  94. if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%";
  95. $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue);
  96. }
  97. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  98. }
  99. }
  100. $orderBy = strtolower(V::get('order_by', 'id', $params));
  101. $orderDir = strtolower(V::get('order_dir', 'desc', $params));
  102. if (!in_array($orderBy, ['id', 'login', 'name'])) throw new HttpException("Bad Request - wrong or missing order by", 400);
  103. if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400);
  104. uasort($items, function ($a, $b) use ($orderBy, $orderDir) {
  105. if ('desc' == $orderDir) {
  106. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1;
  107. } else if ('asc' == $orderDir) {
  108. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1;
  109. }
  110. return 0;
  111. });
  112. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  113. return $items;
  114. }
  115. public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
  116. public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
  117. public function getGeomFieldType($fieldName) { return null; }
  118. public function getPrimaryKeyField() { return 'id'; }
  119. public function getID() { return 0; }
  120. public function getAttributesFromZasoby() { return array(); }
  121. public function isEnumerationField($fieldName) { return false; }
  122. public function getEnumerations($fieldName) { return null; }
  123. public function getXsdFieldType($fieldName) {
  124. if ('id' == $fieldName) return 'xsd:string';
  125. if ('login' == $fieldName) return 'xsd:string';
  126. if ('name' == $fieldName) return 'xsd:string';
  127. }
  128. public function isGeomField($fldName) { return false; }
  129. }