TestPermsStorageAcl.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('FileStorage');
  4. class Schema_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. // $cols[100010] = 'File1';
  18. // $cols[100011] = 'File2';
  19. // $cols[100001] = 'NestedObjectTest';
  20. $cols[100002] = 'AccessGroupRead';
  21. $cols[100003] = 'AccessGroupWrite';
  22. $cols[100004] = 'AccessOwner';
  23. return $cols;
  24. }
  25. public function getFields() {// @returns array - $this->_fields
  26. $fields = $this->parentAcl->getFields();
  27. $fields[100000] = ['name'=>'File', 'perms'=>'RWXC', 'opis'=>'Plik', 'sort_prio'=>999, 'label'=>'Plik'];
  28. // $fields[100001] = 'NestedObjectTest';
  29. $fields[100002] = ['name'=>'AccessGroupRead', 'perms'=>'RWXC', 'opis'=>'Odczyt dla', 'sort_prio'=>999, 'label'=>'Odczyt dla'];
  30. $fields[100003] = ['name'=>'AccessGroupWrite', 'perms'=>'RWXC', 'opis'=>'Zapis dla', 'sort_prio'=>999, 'label'=>'Zapis dla'];
  31. $fields[100004] = ['name'=>'AccessOwner', 'perms'=>'RWXC', 'opis'=>'Osoba odpowiedzialna', 'sort_prio'=>999, 'label'=>'Osoba odp.'];
  32. return $fields;
  33. }
  34. public function getFieldType($colName) {
  35. return null;
  36. }
  37. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  38. public function canCreateField($fieldName) {
  39. if ('File' == $fieldName) return true;
  40. if ('AccessGroupRead' == $fieldName) return true;
  41. if ('AccessGroupWrite' == $fieldName) return true;
  42. if ('AccessOwner' == $fieldName) return true;
  43. return $this->parentAcl->canCreateField($fieldName);
  44. }
  45. public function canReadField($fieldName) {
  46. if ('File' == $fieldName) return true;
  47. if ('AccessGroupRead' == $fieldName) return true;
  48. if ('AccessGroupWrite' == $fieldName) return true;
  49. if ('AccessOwner' == $fieldName) return true;
  50. return $this->parentAcl->canReadField($fieldName);
  51. }
  52. public function canReadObjectField($fieldName, $record) {
  53. if ('File' == $fieldName) return true;
  54. if ('AccessGroupRead' == $fieldName) return true;
  55. if ('AccessGroupWrite' == $fieldName) return true;
  56. if ('AccessOwner' == $fieldName) return true;
  57. return $this->parentAcl->canReadObjectField($fieldName, $record);
  58. }
  59. public function canWriteField($fieldName) {
  60. if ('File' == $fieldName) return true;
  61. if ('AccessGroupRead' == $fieldName) return true;
  62. if ('AccessGroupWrite' == $fieldName) return true;
  63. if ('AccessOwner' == $fieldName) return true;
  64. return $this->parentAcl->canWriteField($fieldName);
  65. }
  66. public function canWriteObjectField($fieldName, $record) {
  67. if ('File' == $fieldName) return true;
  68. if ('AccessGroupRead' == $fieldName) return true;
  69. if ('AccessGroupWrite' == $fieldName) return true;
  70. if ('AccessOwner' == $fieldName) return true;
  71. return $this->parentAcl->canWriteObjectField($fieldName, $record);
  72. }
  73. public function getItems($params = array()) {
  74. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  75. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  76. $items = array();
  77. $rawItems = $this->parentAcl->getItems($params);
  78. foreach ($rawItems as $pk => $item) $items[$pk] = (array)$item;
  79. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  80. if (empty($items)) return $items;
  81. $this->fetchItemRef($items);// TODO: only fields from request
  82. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems after fetchItemRef \$items:";print_r($items);echo "\n";}
  83. return $items;
  84. }
  85. public function fetchItemRef(&$items) {
  86. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  87. $refs = array();// fieldName => xsdType
  88. $aliasRefs = array();// fieldName => aliasName
  89. foreach ($this->getRealFieldListByIdZasob() as $id => $fieldName) {
  90. $fieldType = $this->getXsdFieldType($fieldName);
  91. if ('ref:' == substr($fieldType, 0, 4)) $refs[$fieldName] = substr($fieldType, 4);
  92. else if ('alias_ref:' == substr($fieldType, 0, 10)) {
  93. $refs[$fieldName] = substr($fieldType, 10);
  94. $aliasRefs[$fieldName] = $this->getName() . "__x3A__{$fieldName}";
  95. }
  96. }
  97. if (empty($refs)) return $items;
  98. $pkList = array_keys($items);
  99. $sqlPk = array(); foreach ($pkList as $pk) { $sqlPk[] = DB::getPDO()->quote($pk, PDO::PARAM_STR); } $sqlPk = implode(", ", $sqlPk);
  100. $refRows = array();// $fieldName => [ pk, ... ]
  101. foreach ($refs as $fieldName => $type) {
  102. $acl = $this->getAclFromTypeName($type);
  103. // TODO: Core_AclBase->fetchRefs($fieldName, $pkList = array());// $refPk[$fieldName] = $this->fetchRefs($fieldName, $pkList);
  104. $refTableName = $this->createRefTable($fieldName);
  105. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems loop(\$fieldName:{$fieldName}) \$refTableName:({$refTableName})";echo"\n";}
  106. $refRows[$fieldName] = DB::getPDO()->fetchAllByKey("
  107. select r.*
  108. from `{$refTableName}` r
  109. where r.PRIMARY_KEY in({$sqlPk})
  110. ", $key = 'PRIMARY_KEY');
  111. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems loop(\$fieldName:{$fieldName}) \$refTableName:({$refTableName}), \$refRows[$fieldName]";print_r($refRows[$fieldName]);echo"\n";}
  112. }
  113. foreach ($refRows as $fieldName => $refList) {
  114. foreach ($refList as $pk => $ref) {
  115. // if (array_key_exists($fieldName, $aliasRefs)) {
  116. // $items[ $pk ][ $aliasRefs[$fieldName] ][] = array('xlink' => "{$refs[$fieldName]}.{$ref['REMOTE_PRIMARY_KEY']}");
  117. // } else {
  118. $items[ $pk ][ $fieldName ][] = array('xlink' => "{$refs[$fieldName]}.{$ref['REMOTE_PRIMARY_KEY']}");
  119. // }
  120. }
  121. }
  122. }
  123. public function addItem($itemTodo) {
  124. return $this->parentAcl->addItem($itemTodo);
  125. }
  126. public function updateItem($itemPatch) {
  127. return $this->parentAcl->updateItem($itemPatch);
  128. }
  129. public function getGeomFieldType($fieldName) { return null; }
  130. public function getPrimaryKeyField() { return 'ID'; }
  131. public function getID() { return 0; }
  132. public function getAttributesFromZasoby() { return array(); }
  133. public function isEnumerationField($fieldName) {
  134. if ('A_STATUS' == $fieldName) return true;
  135. return false;
  136. }
  137. public function getEnumerations($fieldName) {
  138. if ('A_STATUS' == $fieldName) return $this->parentAcl->getEnumerations($fieldName);
  139. return null;
  140. }
  141. public function getXsdFieldType($fieldName) {
  142. if ('File' == $fieldName) return 'ref:p5_objects:File';
  143. // if ('File1' == $fieldName) return 'alias_ref:p5_objects:File';
  144. // if ('File2' == $fieldName) return 'alias_ref:p5_objects:File';
  145. // if ('NestedObjectTest' == $fieldName) return 'local_ref:p5_objects:NestedObjectTest';
  146. if ('AccessGroupRead' == $fieldName) return 'alias_ref:p5_objects:AccessGroup';
  147. if ('AccessGroupWrite' == $fieldName) return 'alias_ref:p5_objects:AccessGroup';
  148. if ('AccessOwner' == $fieldName) return 'ref:p5_objects:AccessOwner';
  149. return $this->parentAcl->getXsdFieldType($fieldName);
  150. }
  151. public function isGeomField($fldName) {
  152. if ('File' == $fieldName) return false;
  153. if ('AccessGroupRead' == $fieldName) return false;
  154. if ('AccessGroupWrite' == $fieldName) return false;
  155. if ('AccessOwner' == $fieldName) return false;
  156. // if ('NestedObjectTest' == $fieldName) return false;
  157. return $this->parentAcl->isGeomField($fldName);
  158. }
  159. }