AccessGroupStorageAcl.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('User');
  4. Lib::loadClass('UsersHelper');
  5. Lib::loadClass('ParseOgcFilter');
  6. Lib::loadClass('SqlQueryWhereBuilder');
  7. class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
  8. public function getNamespace() { return 'default_objects/' . $this->getName(); }
  9. public function getSourceName() { return 'default_objects'; }
  10. public function init($force = false) {}
  11. public function isInitialized() { return true; }
  12. public function getName() { return 'AccessGroup'; }
  13. public function getRootTableName() { return 'CRM_LISTA_ZASOBOW'; }
  14. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  15. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  16. public function getVirtualFieldListByIdZasob() { return array(); }
  17. public function getXsdTypes() { // @returns [ fieldName => xsdType, ... ]
  18. return array_map(function ($field) {
  19. return $field['xsdType'];
  20. }, $this->getFieldsWithXsdTypes());
  21. }
  22. public function getFieldsWithXsdTypes() {
  23. $xsdTypes = array();
  24. foreach ($this->getFields() as $idZasob => $field) {
  25. $xsdTypes[ $field['name'] ] = $field;
  26. $xsdTypes[ $field['name'] ][ 'xsdType' ] = $this->getXsdFieldType($field['name']);
  27. }
  28. return $xsdTypes;
  29. }
  30. public function getRealFieldListByIdZasob($force = false) {
  31. $cols[100000] = 'id';// CRM_LISTA_ZASOBOW.ID
  32. $cols[100001] = 'name';// CRM_LISTA_ZASOBOW.DESC
  33. $cols[100002] = 'uid';// Ldap.uid -> value stored in fields: A_ADM_COMPANY, A_CLASSIFIED
  34. return $cols;
  35. }
  36. public function getFields() {
  37. $fields = array();
  38. $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100];
  39. $fields[100001] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101];
  40. $fields[100002] = ['name'=>'uid', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102];
  41. return $fields;
  42. }
  43. public function getFieldType($fieldName) { return null; }
  44. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  45. public function canCreateField($fieldName) { return false; }
  46. public function canReadField($fieldName) { return true; }
  47. public function canReadObjectField($fieldName, $record) {return true; }
  48. public function canWriteField($fieldName) { return false; }
  49. public function canWriteObjectField($fieldName, $record) { return false; }
  50. public function getTotal($params = array()) {
  51. return count($this->getItems($params));
  52. }
  53. public function getItem($primaryKey, $params = []) {
  54. $items = $this->getItems(['primaryKey'=>$primaryKey]);
  55. return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
  56. }
  57. public function getItems($params = array()) {
  58. DBG::log($params, 'array', $this->getName() . "::getItems \$params");
  59. $items = array();
  60. // TODO: fetch groups connectes with current user
  61. {
  62. $userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
  63. DBG::log($userLdapGroups, 'array', $this->getName() . "::getItems \$userLdapGroups");
  64. if (empty($userLdapGroups)) throw new Exception("User groups not found", 404);
  65. foreach ($userLdapGroups as $vLdapGroup) {
  66. $allowGroup = false;
  67. if ('workgroup' == $vLdapGroup->cn) {
  68. $items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
  69. } else {
  70. $cnTest = str_replace('-', '_', $vLdapGroup->cn);
  71. $cnTest = explode('_', $cnTest);
  72. $idZasob = $cnTest[0];
  73. if (!is_numeric($idZasob)) {
  74. DBG::log($vLdapGroup->cn, 'array', $this->getName() . "::getItems skip cn - missing id zasob \$vLdapGroup->cn");
  75. continue;
  76. }
  77. $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
  78. }
  79. }
  80. }
  81. $remotePrimaryKey = null;
  82. if (!empty($params['__backRef'])) {
  83. $backRef = $params['__backRef'];
  84. if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
  85. if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
  86. if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
  87. if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
  88. $refAcl = ACL::getAclByNamespace($backRef['namespace']);
  89. if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source");
  90. $sqlRefRootTableName = $refAcl->getRootTableName();
  91. $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
  92. $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
  93. $remotePrimaryKey = DB::getPDO()->fetchValue("
  94. select refTable.REMOTE_PRIMARY_KEY
  95. from `{$refTable}` refTable
  96. where refTable.PRIMARY_KEY = {$sqlBackRefPk}
  97. ");
  98. if (!$remotePrimaryKey) return array();
  99. }
  100. DBG::log($this->getName() . "::getItems \$remotePrimaryKey({$remotePrimaryKey})");
  101. if ($remotePrimaryKey) {
  102. if (!array_key_exists($remotePrimaryKey, $items)) return array();
  103. $items = array($remotePrimaryKey => $items[$remotePrimaryKey]);
  104. }
  105. if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
  106. if (!array_key_exists($pk, $items)) return array();
  107. $items = array($pk => $items[$pk]);
  108. }
  109. if (!empty($params['ogc:Filter'])) {
  110. $parser = new ParseOgcFilter();
  111. $parser->loadOgcFilter($params['ogc:Filter']);
  112. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  113. DBG::log($queryWhereBuilder, 'array', $this->getName() . "::getItems \$queryWhereBuilder");
  114. DBG::log($items, 'array', $this->getName() . "::getItems \$items");
  115. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  116. }
  117. $filterId = trim(V::get('f_id', '', $params));
  118. if (strlen($filterId)) {// allow '0'
  119. $queryWhereBuilder = new SqlQueryWhereBuilder();
  120. if (is_numeric($filterId)) {
  121. $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId);
  122. } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) {
  123. $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId);
  124. } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  125. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2));
  126. } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  127. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2));
  128. } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  129. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1));
  130. } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  131. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1));
  132. } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  133. $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1));
  134. } else {
  135. $filterId = null;// TODO: BUG uniimplemented comparison sign
  136. }
  137. if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  138. }
  139. foreach (['name', 'uid'] as $fieldName) {
  140. $filterValue = trim(V::get("f_{$fieldName}", '', $params));
  141. if (strlen($filterValue)) {// allow '0'
  142. $queryWhereBuilder = new SqlQueryWhereBuilder();
  143. if (!is_scalar($filterValue)) {
  144. } else if ('=' == substr($filterValue, 0, 1)) {
  145. $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1));
  146. } else {
  147. if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}";
  148. if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%";
  149. $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue);
  150. }
  151. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  152. }
  153. }
  154. $orderBy = strtolower(V::get('order_by', 'id', $params));
  155. $orderDir = strtolower(V::get('order_dir', 'desc', $params));
  156. if (!in_array($orderBy, ['id', 'name', 'uid'])) throw new HttpException("Bad Request - wrong or missing order by", 400);
  157. if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400);
  158. uasort($items, function ($a, $b) use ($orderBy, $orderDir) {
  159. if ('desc' == $orderDir) {
  160. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1;
  161. } else if ('asc' == $orderDir) {
  162. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1;
  163. }
  164. return 0;
  165. });
  166. DBG::log($items, 'array', $this->getName() . "::getItems \$items");
  167. return $items;
  168. }
  169. public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
  170. public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
  171. public function getGeomFieldType($fieldName) { return null; }
  172. public function getPrimaryKeyField() { return 'id'; }
  173. public function getSqlPrimaryKeyField() { return 'ID'; }
  174. public function getAttributesFromZasoby() { return array(); }
  175. public function isEnumerationField($fieldName) { return false; }
  176. public function getEnumerations($fieldName) { return null; }
  177. public function getXsdFieldType($fieldName) {
  178. if ('id' == $fieldName) return 'xsd:string';
  179. if ('name' == $fieldName) return 'xsd:string';
  180. if ('uid' == $fieldName) return 'xsd:string';
  181. }
  182. public function isGeomField($fldName) { return false; }
  183. }