TestPermsStorageAcl.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('FileStorage');
  4. class TestPermsStorageAcl extends Core_AclBase {
  5. public function __construct() {
  6. $this->parentAcl = User::getAcl()->getObjectAcl('default_db', 'TEST_PERMS');
  7. // DBG::_(true, true, "parentAcl", $this->parentAcl, __CLASS__, __FUNCTION__, __LINE__);
  8. }
  9. public function getSourceName() { return 'objects'; }
  10. public function init($force = false) {}
  11. public function isInitialized() { return true; }
  12. public function getName() { return 'TestPerms'; }
  13. public function getRootTableName() { return 'TEST_PREMS'; }
  14. public function getRealFieldListByIdZasob($force = false) {
  15. $cols = $this->parentAcl->getRealFieldListByIdZasob();
  16. foreach ($cols as $id => $name) {
  17. if ('ID' == $name) $cols[$id] = 'id';
  18. }
  19. $cols[100000] = 'File';
  20. return $cols;
  21. }
  22. public function getFieldIdByName($fieldName) {
  23. $fields = $this->getRealFieldListByIdZasob();
  24. if (empty($fieldName)) return null;
  25. foreach ($fields as $idField => $vFieldName) {
  26. if ($vFieldName == $fieldName) return $idField;
  27. }
  28. return null;
  29. }
  30. public function getFieldType($colName) {
  31. switch ($colName) {
  32. case 'id': return array(); break;
  33. }
  34. return null;
  35. }
  36. public function isAllowed($idZasob, $taskPerm, $record = null) {
  37. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  38. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  39. return false;
  40. }
  41. public function hasFieldPerm($idZasob, $taskPerm) {
  42. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  43. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  44. return false;
  45. }
  46. public function getItems($params = array()) {
  47. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  48. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  49. if (!empty($params['ogc:Filter'])) $params['ogc:Filter'] = str_replace('<ogc:PropertyName>id</ogc:PropertyName>', '<ogc:PropertyName>ID</ogc:PropertyName>', $params['ogc:Filter']);
  50. $items = array();
  51. $rawItems = $this->parentAcl->getItems($params);
  52. foreach ($rawItems as $pk => $item) $items[$pk] = (array)$item;
  53. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  54. if (empty($items)) return $items;
  55. $this->fetchItemRef($items);
  56. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems after fetchItemRef \$items:";print_r($items);echo "\n";}
  57. return $items;
  58. }
  59. public function fetchItemRef(&$items) {
  60. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  61. $refs = array();// fieldName => xsdType
  62. foreach ($this->getRealFieldListByIdZasob() as $id => $fieldName) {
  63. $fieldType = $this->getXsdFieldType($fieldName);
  64. if ('ref:' == substr($fieldType, 0, 4)) $refs[$fieldName] = substr($fieldType, 4);
  65. }
  66. if (empty($refs)) return $items;
  67. $fidList = array_keys($items);
  68. $sqlPk = array(); foreach ($fidList as $pk) { $sqlPk[] = "'{$pk}'"; } $sqlPk = implode(", ", $sqlPk);
  69. $baseRefTableName = $this->getRootTableName() . "__#REF__";
  70. $refRows = array();// $fieldName => [ pk, ... ]
  71. foreach ($refs as $fieldName => $type) {
  72. $acl = $this->getAclFromTypeName($type);
  73. $refTableName = $this->getRootTableName() . "__#REF__" . $acl->getRootTableName();
  74. $refRows[$fieldName] = DB::getPDO()->fetchAllByKey("
  75. select r.*
  76. from `{$refTableName}` r
  77. where r.PRIMARY_KEY in({$sqlPk})
  78. -- TODO and r.INSTANCE = '{$refInstanceName}' -- for multiple types based on the same root table
  79. ", $key = 'PRIMARY_KEY');
  80. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems loop(\$fieldName:{$fieldName}) \$refTableName:({$refTableName}), \$refRows[$fieldName]";print_r($refRows[$fieldName]);echo"\n";}
  81. }
  82. foreach ($refRows as $fieldName => $refList) {
  83. foreach ($refList as $pk => $ref) {
  84. $items[ $pk ][ $fieldName ][] = array('xlink' => "{$refs[$fieldName]}.{$ref['REMOTE_PRIMARY_KEY']}");
  85. }
  86. }
  87. }
  88. public function addItem($itemTodo) {
  89. return $this->parentAcl->addItem($itemTodo);
  90. }
  91. public function updateItem($itemPatch) {
  92. $itemPatch['ID'] = $itemPatch['id'];
  93. unset($itemPatch['id']);
  94. return $this->parentAcl->updateItem($itemPatch);
  95. }
  96. public function getGeomFieldType($fieldName) { return null; }
  97. public function getPrimaryKeyField() { return 'id'; }
  98. public function getID() { return 0; }
  99. public function getAttributesFromZasoby() {
  100. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  101. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  102. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  103. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  104. return $attributes;
  105. }
  106. public function isEnumerationField($fieldName) { return false; }
  107. public function validateFieldAction($fieldName, $taskPerm, $record = null) {
  108. if ('File' == $fieldName) {
  109. // return 'ref:p5_objects:File';
  110. return true;
  111. }
  112. if ('id' == $fieldName) $fieldName = 'ID';
  113. return $this->parentAcl->isAllowed($fieldID = $this->parentAcl->getFieldIdByName($fieldName), $taskPerm, $record);
  114. }
  115. public function getXsdFieldType($fieldName) {
  116. if ('File' == $fieldName) return 'ref:p5_objects:File';
  117. return $this->parentAcl->getXsdFieldType($fieldName);
  118. }
  119. public function isGeomField($fldName) { return $this->parentAcl->isGeomField($fldName); }
  120. }