TestPermsStorageAcl.php 8.5 KB

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