瀏覽代碼

synced code from biall - acl

Piotr Labudda 8 年之前
父節點
當前提交
957299329a
共有 4 個文件被更改,包括 84 次插入58 次删除
  1. 12 3
      SE/se-lib/Core/AclSimpleSchemaBase.php
  2. 1 1
      SE/se-lib/Schema/TableFactory.php
  3. 37 36
      SE/se-lib/SchemaFactory.php
  4. 34 18
      SE/se-lib/TableAcl.php

+ 12 - 3
SE/se-lib/Core/AclSimpleSchemaBase.php

@@ -103,7 +103,7 @@ class Core_AclSimpleSchemaBase extends Core_AclBase {
     // $this->parseXsdTypes();// parse xsdTypes
     $this->_xsdTypes = array();
     {
-      $generatedIdZasob = 10000;// fake zasob id
+      $generatedIdZasob = 10000; // fake zasob id
       foreach ($this->_simpleSchema['root'] as $key => $value) {
         if ('@' == substr($key, 0, 1)) continue;// skip attributes
         if (is_array($value)) {
@@ -137,6 +137,10 @@ class Core_AclSimpleSchemaBase extends Core_AclBase {
 
   }
 
+  public function getPDO() {
+    return DB::getPDO( ($this->_idDatabase) ? $this->_idDatabase : '' );
+  }
+
   public function __toString() {
     $out = "xsd @prefix(default_db__x3A__{$this->_rootTableName})" . "\n";
     $aliasRefUri = array();
@@ -322,8 +326,13 @@ class Core_AclSimpleSchemaBase extends Core_AclBase {
       if (!empty($field['@ref'])) continue;// skip ref
       if (empty($field['@type'])) continue;// skip wrong simpleType structure - BUG
       if ('xsd:' != substr($field['@type'], 0, 4)) continue;// skip non xsd types - eg. p5:typeSpecialSimpleLink
-      $sqlField = (!empty($field['@alias'])) ? $field['@alias'] : $key;
-      $sqlSelect[] = "{$prefix}.`{$sqlField}` as `$key`";
+      $sqlKey = $this->getPDO()->identifierQuote($key);
+      if (!empty($field['@aliasSql'])) {
+        $sqlSelect[] = "{$field['@aliasSql']} as {$sqlKey}";
+      } else {
+        $sqlField = $this->getPDO()->identifierQuote( (!empty($field['@alias'])) ? $field['@alias'] : $key );
+        $sqlSelect[] = "{$prefix}.{$sqlField} as {$sqlKey}";
+      }
     }
     return implode("\n, ", $sqlSelect);
   }

+ 1 - 1
SE/se-lib/Schema/TableFactory.php

@@ -17,7 +17,7 @@ class Schema_TableFactory {
 		$objectClassName = 'Schema_TableBase';
 		$cleanHostName = str_replace(array(".", "-"), '_', $hostName);
 		$cleanTableName = strtolower($tableName);
-		$cleanSourceName = ($idSource == DB::getPDO()->getZasobId())? 'default_db' : "p5_{$idSource}";
+		$cleanSourceName = ($idSource == DB::getPDO()->getZasobId()) ? 'default_db' : "p5_{$idSource}";
 
 		DBG::log("Schema({$tableName}): cleanHostName({$cleanHostName}) cleanTableName({$cleanTableName}) cleanSourceName({$cleanSourceName})");
 		// TODO: fetch form widgets from file config

+ 37 - 36
SE/se-lib/SchemaFactory.php

@@ -2,45 +2,46 @@
 
 class SchemaFactory {
 
-  public static function loadDefaultObject($name) {
+	public static function loadDefaultObject($namespace) {
 		static $_cache = [];
-		if (array_key_exists($name, $_cache)) return $_cache[$name];
-    $objClassName = "Schema_{$name}StorageAcl";
-    if (!Lib::tryLoadClass($objClassName)) throw new HttpException("Not implemented - storage object not found '{$name}'", 501);
-    $_cache[$name] = new $objClassName();
-    return $_cache[$name];
-  }
+		$clsName = str_replace('/', '_', $namespace);
+		if (array_key_exists($name, $_cache)) return $_cache[$clsName];
+		$objClassName = "Schema_{$clsName}StorageAcl";
+		if (!Lib::tryLoadClass($objClassName)) throw new HttpException("Not implemented - storage object not found '{$namespace}'", 501);
+		$_cache[$clsName] = new $objClassName();
+		return $_cache[$clsName];
+	}
 
-  public static function loadTableObject($tableName, $name) {
-    $className = "Schema_DefaultDb_{$tableName}_{$name}StorageAcl";// TODO: load by Factory class which build from schema file
-    // list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri('default_objects/AccessOwner');
+	public static function loadTableObject($tableName, $name) {
+		$className = "Schema_DefaultDb_{$tableName}_{$name}StorageAcl";// TODO: load by Factory class which build from schema file
+		// list($nsUri, $prefix, $name) = Api_WfsNs::parseObjectNsUri('default_objects/AccessOwner');
 
-    $path = implode('/', [
-      APP_PATH_LIB,
-      'Schema',
-      'DefaultDb',
-      strtolower($tableName),
-      implode('/', explode('_', $name)).'StorageAcl.php',
-    ]);
-    if (file_exists($path)) {
-      require_once $path;
-    } else {
-      $path = implode('/', [
-        APP_PATH_LIB,
-        'Schema',
-        'DefaultDb',
-        strtolower($tableName),
-        $name.'StorageAcl.php',
-      ]);
-      if (file_exists($path)) {
-        require_once $path;
-      }
-    }
-    if (!class_exists($className)) {
-      throw new HttpException("Not implemented - default db storage object not found 'default_db/{$tableName}/{$name}'", 501);
-    }
+		$path = implode('/', [
+			APP_PATH_LIB,
+			'Schema',
+			'DefaultDb',
+			strtolower($tableName),
+			implode('/', explode('_', $name)).'StorageAcl.php',
+		]);
+		if (file_exists($path)) {
+			require_once $path;
+		} else {
+			$path = implode('/', [
+				APP_PATH_LIB,
+				'Schema',
+				'DefaultDb',
+				strtolower($tableName),
+				$name.'StorageAcl.php',
+			]);
+			if (file_exists($path)) {
+				require_once $path;
+			}
+		}
+		if (!class_exists($className)) {
+			throw new HttpException("Not implemented - default db storage object not found 'default_db/{$tableName}/{$name}'", 501);
+		}
 
-    return new $className;
-  }
+		return new $className;
+	}
 
 }

+ 34 - 18
SE/se-lib/TableAcl.php

@@ -52,14 +52,18 @@ class TableAcl extends Core_AclBase {
 		if (empty($this->_name)) return null;// throw new Exception("Table name not defined");
 		if ($this->_rootTableName) return $this->_rootTableName;
 		if (!$this->_db) return null;// throw new Exception("Database not defined in table {$this->_zasobID}");
-		$dbName = DB::getPDO($this->_db)->getDatabaseName();
-		$this->_rootTableName = DB::getPDO()->fetchValue("
-			select t.TABLE_NAME
-			from `information_schema`.`TABLES` t
-			where t.TABLE_SCHEMA = '{$dbName}'
-				and t.TABLE_NAME LIKE '{$this->_name}'
-		");
-		if (!$this->_rootTableName) return null;// throw new Exception("Table '{$this->_name}' not exists!");
+		if (DB::getPDO($this->_db)->getZasobId() === DB::getPDO()->getZasobId()) {
+			$dbName = DB::getPDO($this->_db)->getDatabaseName();
+			$this->_rootTableName = DB::getPDO()->fetchValue("
+				select t.TABLE_NAME
+				from `information_schema`.`TABLES` t
+				where t.TABLE_SCHEMA = '{$dbName}'
+					and t.TABLE_NAME LIKE '{$this->_name}'
+			");
+			if (!$this->_rootTableName) return null;// throw new Exception("Table '{$this->_name}' not exists!");
+		} else {
+			$this->_rootTableName = $this->_name; // TODO: fix for Postgres
+		}
 		DBG::log("TableAcl({$this->_zasobID})->getRootTableName (\$this->_name='{$this->_name}', \$this->_rootTableName='{$this->_rootTableName}')");
 		if ($this->_rootTableName != $this->_name) {
 			if ($this->_zasobID) {
@@ -74,7 +78,9 @@ class TableAcl extends Core_AclBase {
 		return $this->_rootTableName;
 	}
 	public function getID() { return $this->_zasobID; }
-	public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
+	public function getSqlPrimaryKeyField() {
+		return $this->getPrimaryKeyField();
+	} // TODO: read from root object schema (_rootTableName)
 	public function setName($name) {// TODO: used only by setNameByTableId
 		DBG::log("setName('{$name}')");
 		$this->_name = $name;
@@ -1043,7 +1049,7 @@ class TableAcl extends Core_AclBase {
 			$this->_types = array();// clear _types @see $this->isInitialized
 			$userAcl = User::getAcl();
 			$fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
-			// DBG::log(['msg'=>"INIT({$this->_zasobID})::initFieldsFromConfig \$fieldsConfig", '$fieldsConfig'=>$fieldsConfig]);
+			// DBG::log($fieldsConfig, 'array', "INIT({$this->_zasobID})::initFieldsFromConfig \$fieldsConfig");
 			$this->initFieldsFromConfig($fieldsConfig);
 			//DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
 		}
@@ -1053,16 +1059,18 @@ class TableAcl extends Core_AclBase {
 		}
 		DBG::simpleLog('auth', "TableAcl::init(name: '{$this->_name}')");
 
-		// DBG::log(['msg'=>"INIT({$this->_zasobID}):: \$this", '$this'=>$this, 'name'=>$this->getName()]);
+		// DBG::log(['$this'=>$this, 'name'=>$this->getName()], 'array', "INIT({$this->_zasobID}):: \$this");
 		$ds = $this->getDataSource();
-		// DBG::log(['msg'=>"INIT({$this->_zasobID})::getDataSource \$ds", '$ds'=>$ds]);
-		$this->_types = $ds->getFieldTypes();
-		// DBG::log(['msg'=>"INIT({$this->_zasobID})::getFieldTypes \$this->_types", '$this->_types'=>$this->_types]);
+		// DBG::log($ds, 'array', "INIT({$this->_zasobID})::getDataSource \$ds");
+		$this->_types = (is_object($this->_schemaClass) && method_exists($this->_schemaClass, 'getLegacyFieldTypes'))
+		? $this->_schemaClass->getLegacyFieldTypes()
+		: $ds->getFieldTypes();
+		// DBG::log($this->_types, 'array', "INIT({$this->_zasobID})::getFieldTypes \$this->_types");
 
 		uasort($this->_fields, array($this, 'sortFieldsCallback'));
 
 		$this->_fixTypes();
-		// DBG::log(['msg'=>"INIT({$this->_zasobID})::after fixTypes \$this->_types", '$this->_types'=>$this->_types]);
+		// DBG::log($this->_types, 'array', "INIT({$this->_zasobID})::after fixTypes \$this->_types");
 
 		$this->save();
 	}
@@ -1276,16 +1284,17 @@ class TableAcl extends Core_AclBase {
 
 	public function getVisibleFieldList() { return $this->getVisibleFieldListByIdZasob(); }
 	public function getVisibleFieldListByIdZasob() {
+		$primaryKeyField = $this->getSqlPrimaryKeyField();
 		$cols = array();
 		$id = 0;
 		foreach ($this->getFields() as $kFieldID => $vField) {
-			if ($vField['name'] == 'ID') {
+			if ($vField['name'] == $primaryKeyField) {
 				$id = $kFieldID;
 			}
 		}
-		$cols[$id] = 'ID';
+		$cols[$id] = $primaryKeyField;
 		foreach ($this->getFields() as $kFieldID => $vField) {
-			if ($vField['name'] == 'ID') {
+			if ($vField['name'] == $primaryKeyField) {
 				continue;
 			}
 			$cols[$kFieldID] = $vField['name'];
@@ -1598,6 +1607,13 @@ class TableAcl extends Core_AclBase {
 		return $geomType;
 	}
 
+	public function getSqlTableFrom() {
+		if ($this->loadSchema() && method_exists($this->_schemaClass, 'getSqlTableFrom')) {
+			return $this->_schemaClass->getSqlTableFrom();
+		}
+		return DB::getPDO( $this->getDB() )->tableNameQuote( $this->getRootTableName() );
+	}
+
 	public function getHistItems($primaryKey) {
 		if ($this->loadSchema() && method_exists($this->_schemaClass, 'hasGetHistItems') && $this->_schemaClass->hasGetHistItems()) {
 			return $this->_schemaClass->getHistItems($primaryKey);