Просмотр исходного кода

added support for filter ogc:PropertyIsEqualTo in AccessGroup

Piotr Labudda 9 лет назад
Родитель
Сommit
5040f41767

+ 14 - 4
SE/se-lib/Schema/AccessGroupStorageAcl.php

@@ -3,6 +3,7 @@
 Lib::loadClass('Core_AclBase');
 Lib::loadClass('User');
 Lib::loadClass('UsersHelper');
+Lib::loadClass('ParseOgcFilter');
 
 class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
 
@@ -36,13 +37,13 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
     // TODO: fetch groups connectes with current user
   {
     $userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
-    if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$userLdapGroups:";print_r($userLdapGroups);echo "\n";}
+    if($DBG>4){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$userLdapGroups:";print_r($userLdapGroups);echo "\n";}
     if (empty($userLdapGroups)) throw new Exception("User groups not found", 404);
 
     foreach ($userLdapGroups as $vLdapGroup) {
       $allowGroup = false;
       if ('workgroup' == $vLdapGroup->cn) {
-        $items[1] = ['id'=>0, 'name'=>$vLdapGroup->cn, 'uid'=>$vLdapGroup->cn];
+        $items[1] = ['id'=>0, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
       } else {
         $cnTest = str_replace('-', '_', $vLdapGroup->cn);
         $cnTest = explode('_', $cnTest);
@@ -51,14 +52,23 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
           if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems - skip cn - missing id zasob \$vLdapGroup->cn:";print_r($vLdapGroup->cn);echo "\n";}
           continue;
         }
-        $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->cn, 'uid'=>$vLdapGroup->cn];
+        $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
       }
     }
   }
     if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
       if (!array_key_exists($pk, $items)) return array();
-      return array($pk => $items[$pk]);
+      $items = array($pk => $items[$pk]);
     }
+    if (!empty($params['ogc:Filter'])) {
+      $parser = new ParseOgcFilter();
+      $parser->loadOgcFilter($params['ogc:Filter']);
+      $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
+      DBG::_('DBG_DS', '>2', "ogc:Filter \$queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
+      if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
+      $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
+    }
+
     if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
     return $items;
   }

+ 118 - 0
SE/se-lib/SqlQueryWhereBuilder.php

@@ -57,6 +57,124 @@ class SqlQueryWhereBuilder {
 		return str_replace('{tablePrefix}', $sqlTablePrefix, $sqlWhereRaw);
 	}
 
+	public function filterRawArray($item) {
+		$lowerItem = array(); foreach ((array)$item as $fieldName => $value) $lowerItem[strtolower($fieldName)] = $value;
+		DBG::_('DBG_DS_OGC', '>2', "\$lowerItem", $lowerItem, __CLASS__, __FUNCTION__, __LINE__);
+
+		$sqlBlocksStack = array();
+		$sqlValuesStack = array();
+		$arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
+		foreach ($this->_log as $idxLog => $log) {
+			switch ($log) {
+				case 'open_block_and':
+				case 'open_block_or':
+				case 'open_block_not': {
+					$blockType = substr($log, 11);// 'and', 'or', 'not'
+					array_push($sqlBlocksStack, $blockType);
+					$arr = array(); array_push($sqlValuesStack, $arr);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
+				}
+					break;
+				case 'close_block_and':
+				case 'close_block_or': {
+					$blockType = substr($log, 12);// 'and', 'or'
+					if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
+					$stackBlockType = array_pop($sqlBlocksStack);
+					if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
+
+					// parse to string and add to parent value stack
+					$stackValue = array_pop($sqlValuesStack);
+					if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
+					if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
+					$sqlFromStack = false;
+					foreach ($stackValue as $bool) {
+						if (!is_scalar($bool) && ($bool == true or $bool == false)) throw new Exception("parse query failed - expected bool values in block or");
+						if ($bool) $sqlFromStack = true;
+					}
+					$parentStackValue = array_pop($sqlValuesStack);
+					if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
+					array_push($parentStackValue, $sqlFromStack);
+					array_push($sqlValuesStack, $parentStackValue);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
+				}
+					break;
+				case 'close_block_not': {
+					$blockType = substr($log, 12);// 'not'
+					if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
+					$stackBlockType = array_pop($sqlBlocksStack);
+					if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
+
+					// parse to string and add to parent value stack
+					$stackValue = array_pop($sqlValuesStack);
+					if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
+					if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
+					if (1 != count($stackValue)) throw new Exception("parse sql query failed - stack value count is not equal to 1");
+					$stackValue = reset($stackValue);
+					$sqlFromStack = " ! ({$stackValue}) ";
+					$parentStackValue = array_pop($sqlValuesStack);
+					if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
+					array_push($parentStackValue, $sqlFromStack);
+					array_push($sqlValuesStack, $parentStackValue);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
+				}
+					break;
+				default: {
+					if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
+						$fieldName = strtolower($log[1]);
+						if (!array_key_exists($fieldName, $lowerItem)) throw new Exception("field '{$fieldName}' not defined");
+						switch ($log[2]) {
+							case '=': {
+								$sqlFromStack = ($lowerItem[$fieldName] == $log[3]);
+								DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log[0]}' comparison({$lowerItem[$fieldName]} == {$log[3]}) \$sqlFromStack", $sqlFromStack, __CLASS__, __FUNCTION__, __LINE__);
+								break;
+							}
+							default: throw new Exception("compaison sign '{$log[2]}' not defined");
+						}
+						// $this->_usedFields[$sqlFieldName] = true;
+						// $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
+						$stackValue = array_pop($sqlValuesStack);
+						if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
+						array_push($stackValue, $sqlFromStack);
+						array_push($sqlValuesStack, $stackValue);
+					} else if (is_array($log) && 5 == count($log) && 'comparisonFieldFunToValue' == $log[0]) {
+						$sqlFieldFunName = $log[1];
+						if (strtolower($sqlFieldFunName) != 'geometrytype') throw new Exception("Unsupported db function {$sqlFieldName}");
+						$sqlFieldName = $log[2];
+						// $this->_usedFields[$sqlFieldName] = true;
+						$sqlFromStack = "{$sqlFieldFunName}({tablePrefix}`{$sqlFieldName}`) {$log[3]} '{$log[4]}'";
+						$stackValue = array_pop($sqlValuesStack);
+						if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
+						array_push($stackValue, $sqlFromStack);
+						array_push($sqlValuesStack, $stackValue);
+					} else if (is_array($log) && 2 == count($log) && 'comparisonFieldIsNull' == $log[0]) {
+						$sqlFieldName = $log[1];
+						$sqlFromStack = "{tablePrefix}`{$sqlFieldName}` is null";
+						$stackValue = array_pop($sqlValuesStack);
+						if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
+						array_push($stackValue, $sqlFromStack);
+						array_push($sqlValuesStack, $stackValue);
+					} else {
+						throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
+					}
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
+				}
+			}
+		}
+		if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
+		if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
+
+		if (1 !== count($sqlValuesStack[0])) throw new Exception("parse sql query failed - values stack is empty");
+		$bool = $sqlValuesStack[0][0];
+		if (!is_scalar($bool) && ($bool == true or $bool == false)) throw new Exception("parse query failed - expected bool in stack value");
+
+		DBG::_('DBG_DS_OGC', '>2', "\nOK return ".($bool ? 'true' : 'false')." for item:", $item, __CLASS__, __FUNCTION__, __LINE__);
+		return $bool;
+	}
+
 	public function parseQueryWhere() {
 		$this->_raw = "";
 		$this->_usedFields = array();

+ 1 - 1
SE/se-lib/UsersLdapHelper.php

@@ -102,7 +102,7 @@ class UsersLdapHelper {
 	public static function getUserGroups($userName, $authLDAPSubGroupDepth = 3) {
 		$userLdapGroups = array();
 
-		$attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
+		$attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn', 'apple-group-realname'=>'name');// (givenName, sn) = cn
 
 		Lib::loadClass('LDAP');
 		$ldap = LDAP::getInstance();