TestPermsStorageAcl.php 8.4 KB

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