Quellcode durchsuchen

added support for backRef param in AccessOwner

Piotr Labudda vor 8 Jahren
Ursprung
Commit
4d5184409f
1 geänderte Dateien mit 30 neuen und 5 gelöschten Zeilen
  1. 30 5
      SE/se-lib/Schema/AccessOwnerStorageAcl.php

+ 30 - 5
SE/se-lib/Schema/AccessOwnerStorageAcl.php

@@ -68,20 +68,44 @@ class Schema_AccessOwnerStorageAcl extends Core_AclBase {
 	public function getItems($params = array()) {
 		$DBG = V::get('DBG_DS', 0, $_GET, 'int');
 		if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
+		DBG::log($params, 'array', "AccessOwner::getItems \$params");
 
 		$sqlWhereAndPk = "";
 		if ($pk = V::get('@primaryKey', null, $params)) {// [@primaryKey] => TODO: int or array of int
 			if (!is_array($pk)) $sqlPk = array($pk);
 			else $sqlPk = $pk;
-			$sqlWhereAndPk = "and u.ID in (" . implode(", ", $sqlPk) . ")";
+			$sqlWhereAndPk = "and t.ID in (" . implode(", ", $sqlPk) . ")";
+		}
+
+		$sqlWhereAndBackRef = "";
+		if (!empty($params['__backRef'])) {
+			$backRef = $params['__backRef'];
+			if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
+			if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
+			if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
+			if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
+			// TODO: $this->_query->where([ '__backRef' ]); or	$this->_query->join([ '__backRef' ]);
+			$refAcl = ACL::getAclByNamespace($backRef['namespace']);
+			if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source");
+			$refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
+			$sqlPk = $this->getAclSqlPrimaryKeyField();
+			$sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
+			$sqlWhereAndBackRef = "
+				and t.{$sqlPk} in (
+					select refTable.REMOTE_PRIMARY_KEY
+					from `{$refTable}` refTable
+					where refTable.PRIMARY_KEY = {$sqlBackRefPk}
+				)
+			";
 		}
 
 		$items = DB::getPDO()->fetchAllByKey("
-			select u.ID as id, u.ADM_ACCOUNT as login, u.ADM_NAME as name
-			from ADMIN_USERS u
-			where u.A_STATUS = 'NORMAL'
-				and u.ADM_TECH_WORKER != 'NO'
+			select t.ID as id, t.ADM_ACCOUNT as login, t.ADM_NAME as name
+			from ADMIN_USERS t
+			where t.A_STATUS = 'NORMAL'
+				and t.ADM_TECH_WORKER != 'NO'
 				{$sqlWhereAndPk}
+				{$sqlWhereAndBackRef}
 		", $key = 'id');
 
 		if ($pk = V::get('primaryKey', 0, $params, 'int')) {// [primaryKey] => 2948
@@ -156,6 +180,7 @@ class Schema_AccessOwnerStorageAcl extends Core_AclBase {
 	public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
 	public function getGeomFieldType($fieldName) { return null; }
 	public function getPrimaryKeyField() { return 'id'; }
+	public function getAclSqlPrimaryKeyField() { return 'ID'; }
 	public function getAttributesFromZasoby() { return array(); }
 	public function isEnumerationField($fieldName) { return false; }
 	public function getEnumerations($fieldName) { return null; }