TestPermsStorageAcl.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $cols[100000] = 'File';
  17. return $cols;
  18. }
  19. public function getFieldIdByName($fieldName) {
  20. $fields = $this->getRealFieldListByIdZasob();
  21. if (empty($fieldName)) return null;
  22. foreach ($fields as $idField => $vFieldName) {
  23. if ($vFieldName == $fieldName) return $idField;
  24. }
  25. return null;
  26. }
  27. public function getFieldType($colName) {
  28. return null;
  29. }
  30. public function isAllowed($idZasob, $taskPerm, $record = null) {
  31. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  32. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  33. return false;
  34. }
  35. public function hasFieldPerm($idZasob, $taskPerm) {
  36. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  37. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  38. return false;
  39. }
  40. public function getItems($params = array()) {
  41. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  42. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  43. $items = array();
  44. $rawItems = $this->parentAcl->getItems($params);
  45. foreach ($rawItems as $pk => $item) $items[$pk] = (array)$item;
  46. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  47. if (empty($items)) return $items;
  48. $this->fetchItemRef($items);
  49. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems after fetchItemRef \$items:";print_r($items);echo "\n";}
  50. return $items;
  51. }
  52. public function fetchItemRef(&$items) {
  53. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  54. $refs = array();// fieldName => xsdType
  55. foreach ($this->getRealFieldListByIdZasob() as $id => $fieldName) {
  56. $fieldType = $this->getXsdFieldType($fieldName);
  57. if ('ref:' == substr($fieldType, 0, 4)) $refs[$fieldName] = substr($fieldType, 4);
  58. }
  59. if (empty($refs)) return $items;
  60. $fidList = array_keys($items);
  61. $sqlPk = array(); foreach ($fidList as $pk) { $sqlPk[] = "'{$pk}'"; } $sqlPk = implode(", ", $sqlPk);
  62. $baseRefTableName = $this->getRootTableName() . "__#REF__";
  63. $refRows = array();// $fieldName => [ pk, ... ]
  64. foreach ($refs as $fieldName => $type) {
  65. $acl = $this->getAclFromTypeName($type);
  66. $refTableName = $this->getRootTableName() . "__#REF__" . $acl->getRootTableName();
  67. $refRows[$fieldName] = DB::getPDO()->fetchAllByKey("
  68. select r.*
  69. from `{$refTableName}` r
  70. where r.PRIMARY_KEY in({$sqlPk})
  71. -- TODO and r.INSTANCE = '{$refInstanceName}' -- for multiple types based on the same root table
  72. ", $key = 'PRIMARY_KEY');
  73. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems loop(\$fieldName:{$fieldName}) \$refTableName:({$refTableName}), \$refRows[$fieldName]";print_r($refRows[$fieldName]);echo"\n";}
  74. }
  75. foreach ($refRows as $fieldName => $refList) {
  76. foreach ($refList as $pk => $ref) {
  77. $items[ $pk ][ $fieldName ][] = array('xlink' => "{$refs[$fieldName]}.{$ref['REMOTE_PRIMARY_KEY']}");
  78. }
  79. }
  80. }
  81. public function addItem($itemTodo) {
  82. return $this->parentAcl->addItem($itemTodo);
  83. }
  84. public function updateItem($itemPatch) {
  85. return $this->parentAcl->updateItem($itemPatch);
  86. }
  87. public function getGeomFieldType($fieldName) { return null; }
  88. public function getPrimaryKeyField() { return 'ID'; }
  89. public function getID() { return 0; }
  90. public function getAttributesFromZasoby() {
  91. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  92. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  93. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  94. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  95. return $attributes;
  96. }
  97. public function isEnumerationField($fieldName) { return false; }
  98. public function validateFieldAction($fieldName, $taskPerm, $record = null) {
  99. if ('File' == $fieldName) {
  100. // return 'ref:p5_objects:File';
  101. return true;
  102. }
  103. return $this->parentAcl->isAllowed($fieldID = $this->parentAcl->getFieldIdByName($fieldName), $taskPerm, $record);
  104. }
  105. public function getXsdFieldType($fieldName) {
  106. if ('File' == $fieldName) return 'ref:p5_objects:File';
  107. return $this->parentAcl->getXsdFieldType($fieldName);
  108. }
  109. public function isGeomField($fldName) { return $this->parentAcl->isGeomField($fldName); }
  110. }