AccessGroupStorageAcl.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 getSqlFieldName($fieldName) {
  44. switch ($fieldName) {
  45. case 'id': return 'ID';
  46. case 'name': return 'DESC';
  47. case 'uid': return 'ID';
  48. }
  49. throw new Exception("Unknown field '{$fieldName}' in AccessGroup (" . $this->getName() . ")");
  50. }
  51. public function getFieldType($fieldName) { return null; }
  52. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  53. public function canCreateField($fieldName) { return false; }
  54. public function canReadField($fieldName) { return true; }
  55. public function canReadObjectField($fieldName, $record) {return true; }
  56. public function canWriteField($fieldName) { return false; }
  57. public function canWriteObjectField($fieldName, $record) { return false; }
  58. public function getTotal($params = array()) {
  59. return count($this->getItems($params));
  60. }
  61. public function getItem($primaryKey, $params = []) {
  62. $items = $this->getItems(['primaryKey'=>$primaryKey]);
  63. return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
  64. }
  65. public function getItems($params = array()) {
  66. DBG::log($params, 'array', $this->getName() . "::getItems \$params");
  67. $items = array();
  68. // TODO: fetch groups connectes with current user
  69. {
  70. $userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
  71. DBG::log($userLdapGroups, 'array', $this->getName() . "::getItems \$userLdapGroups");
  72. if (empty($userLdapGroups)) throw new Exception("User groups not found", 404);
  73. foreach ($userLdapGroups as $vLdapGroup) {
  74. $allowGroup = false;
  75. if ('workgroup' == $vLdapGroup->cn) {
  76. $items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
  77. } else {
  78. $cnTest = str_replace('-', '_', $vLdapGroup->cn);
  79. $cnTest = explode('_', $cnTest);
  80. $idZasob = $cnTest[0];
  81. if (!is_numeric($idZasob)) {
  82. DBG::log($vLdapGroup->cn, 'array', $this->getName() . "::getItems skip cn - missing id zasob \$vLdapGroup->cn");
  83. continue;
  84. }
  85. $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
  86. }
  87. }
  88. }
  89. $remotePrimaryKey = null;
  90. if (!empty($params['__backRef'])) {
  91. $backRef = $params['__backRef'];
  92. if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
  93. if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
  94. if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
  95. if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
  96. $refAcl = ACL::getAclByNamespace($backRef['namespace']);
  97. if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source");
  98. $sqlRefRootTableName = $refAcl->getRootTableName();
  99. $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
  100. $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
  101. $remotePrimaryKey = DB::getPDO()->fetchValue("
  102. select refTable.REMOTE_PRIMARY_KEY
  103. from `{$refTable}` refTable
  104. where refTable.PRIMARY_KEY = {$sqlBackRefPk}
  105. ");
  106. if (!$remotePrimaryKey) return array();
  107. }
  108. DBG::log($this->getName() . "::getItems \$remotePrimaryKey({$remotePrimaryKey})");
  109. if ($remotePrimaryKey) {
  110. if (!array_key_exists($remotePrimaryKey, $items)) return array();
  111. $items = array($remotePrimaryKey => $items[$remotePrimaryKey]);
  112. }
  113. if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
  114. if (!array_key_exists($pk, $items)) return array();
  115. $items = array($pk => $items[$pk]);
  116. }
  117. if (!empty($params['ogc:Filter'])) {
  118. $parser = new ParseOgcFilter();
  119. $parser->loadOgcFilter($params['ogc:Filter']);
  120. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  121. DBG::log($queryWhereBuilder, 'array', $this->getName() . "::getItems \$queryWhereBuilder");
  122. DBG::log($items, 'array', $this->getName() . "::getItems \$items");
  123. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  124. }
  125. $filterId = trim(V::get('f_id', '', $params));
  126. if (strlen($filterId)) {// allow '0'
  127. $queryWhereBuilder = new SqlQueryWhereBuilder();
  128. if (is_numeric($filterId)) {
  129. $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId);
  130. } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) {
  131. $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId);
  132. } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  133. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2));
  134. } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
  135. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2));
  136. } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  137. $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1));
  138. } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  139. $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1));
  140. } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
  141. $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1));
  142. } else {
  143. $filterId = null;// TODO: BUG uniimplemented comparison sign
  144. }
  145. if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  146. }
  147. foreach (['name', 'uid'] as $fieldName) {
  148. $filterValue = trim(V::get("f_{$fieldName}", '', $params));
  149. if (strlen($filterValue)) {// allow '0'
  150. $queryWhereBuilder = new SqlQueryWhereBuilder();
  151. if (!is_scalar($filterValue)) {
  152. } else if ('=' == substr($filterValue, 0, 1)) {
  153. $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1));
  154. } else {
  155. if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}";
  156. if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%";
  157. $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue);
  158. }
  159. $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
  160. }
  161. }
  162. $orderBy = strtolower(V::get('order_by', 'id', $params));
  163. $orderDir = strtolower(V::get('order_dir', 'desc', $params));
  164. if (!in_array($orderBy, ['id', 'name', 'uid'])) throw new HttpException("Bad Request - wrong or missing order by", 400);
  165. if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400);
  166. uasort($items, function ($a, $b) use ($orderBy, $orderDir) {
  167. if ('desc' == $orderDir) {
  168. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1;
  169. } else if ('asc' == $orderDir) {
  170. return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1;
  171. }
  172. return 0;
  173. });
  174. DBG::log($items, 'array', $this->getName() . "::getItems \$items");
  175. return $items;
  176. }
  177. public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
  178. public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
  179. public function getGeomFieldType($fieldName) { return null; }
  180. public function getPrimaryKeyField() { return 'id'; }
  181. public function getSqlPrimaryKeyField() { return 'ID'; }
  182. public function getAttributesFromZasoby() { return array(); }
  183. public function isEnumerationField($fieldName) { return false; }
  184. public function getEnumerations($fieldName) { return null; }
  185. public function getXsdFieldType($fieldName) {
  186. if ('id' == $fieldName) return 'xsd:string';
  187. if ('name' == $fieldName) return 'xsd:string';
  188. if ('uid' == $fieldName) return 'xsd:string';
  189. }
  190. public function isGeomField($fldName) { return false; }
  191. }