Explorar el Código

added support for backRef param in AccessGroup

Piotr Labudda hace 8 años
padre
commit
7fafbc15be
Se han modificado 2 ficheros con 64 adiciones y 26 borrados
  1. 3 3
      SE/se-lib/Route/Storage/AclStruct.php
  2. 61 23
      SE/se-lib/Schema/AccessGroupStorageAcl.php

+ 3 - 3
SE/se-lib/Route/Storage/AclStruct.php

@@ -750,8 +750,8 @@ class Route_Storage_AclStruct extends RouteBase {
 			]);
 			UI::table([
 				'rows' => array_map(function ($field) use ($thisGetLink) {
-					$refNamespace = str_replace(['__x3A__', ':'], '/', substr($field['xsdType'], strlen('ref:')));
-					$refNamespace = ACL::getBaseNamespace($refNamespace);
+					$refNs = str_replace(['__x3A__', ':'], '/', substr($field['xsdType'], strlen('ref:')));
+					$refNamespace = ACL::getBaseNamespace($refNs);
 					$isInstalled = (1 == DB::getPDO()->fetchValue("
 						select t.isObjectActive
 						from `CRM_#CACHE_ACL_OBJECT` t
@@ -774,7 +774,7 @@ class Route_Storage_AclStruct extends RouteBase {
 						'ref object' => UI::h('a', [
 							'class' => "btn btn-xs btn-link",
 							'href' => $thisGetLink('', [ 'namespace' => $refNamespace ])
-						], "{$refNamespace}"),
+						], "{$refNs}"),
 						'ref source' => UI::h('div', [],
 							(null === $refSource)
 							? [ "ref object not installed" ] // TODO: link to install? is table struct

+ 61 - 23
SE/se-lib/Schema/AccessGroupStorageAcl.php

@@ -17,6 +17,19 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
 	public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
 	public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
 	public function getVirtualFieldListByIdZasob() { return array(); }
+	public function getXsdTypes() { // @returns [ fieldName => xsdType, ... ]
+		return array_map(function ($field) {
+			return $field['xsdType'];
+		}, $this->getFieldsWithXsdTypes());
+	}
+	public function getFieldsWithXsdTypes() {
+		$xsdTypes = array();
+		foreach ($this->getFields() as $idZasob => $field) {
+			$xsdTypes[ $field['name'] ] = $field;
+			$xsdTypes[ $field['name'] ][ 'xsdType' ] = $this->getXsdFieldType($field['name']);
+		}
+		return $xsdTypes;
+	}
 	public function getRealFieldListByIdZasob($force = false) {
 		$cols[100000] = 'id';// CRM_LISTA_ZASOBOW.ID
 		$cols[100001] = 'name';// CRM_LISTA_ZASOBOW.DESC
@@ -47,32 +60,56 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
 		return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
 	}
 	public function getItems($params = array()) {
-		$DBG = V::get('DBG_DS', 0, $_GET, 'int');
-		if($DBG>2){echo'<pre>';}
-		if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
+		DBG::log($params, 'array', $this->getName() . "::getItems \$params");
 		$items = array();
 		// TODO: fetch groups connectes with current user
-	{
-		$userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
-		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);
+		{
+			$userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
+			DBG::log($userLdapGroups, 'array', $this->getName() . "::getItems \$userLdapGroups");
+			if (empty($userLdapGroups)) throw new Exception("User groups not found", 404);
 
-		foreach ($userLdapGroups as $vLdapGroup) {
-			$allowGroup = false;
-			if ('workgroup' == $vLdapGroup->cn) {
-				$items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
-			} else {
-				$cnTest = str_replace('-', '_', $vLdapGroup->cn);
-				$cnTest = explode('_', $cnTest);
-				$idZasob = $cnTest[0];
-				if (!is_numeric($idZasob)) {
-					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;
+			foreach ($userLdapGroups as $vLdapGroup) {
+				$allowGroup = false;
+				if ('workgroup' == $vLdapGroup->cn) {
+					$items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
+				} else {
+					$cnTest = str_replace('-', '_', $vLdapGroup->cn);
+					$cnTest = explode('_', $cnTest);
+					$idZasob = $cnTest[0];
+					if (!is_numeric($idZasob)) {
+						DBG::log($vLdapGroup->cn, 'array', $this->getName() . "::getItems skip cn - missing id zasob \$vLdapGroup->cn");
+						continue;
+					}
+					$items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
 				}
-				$items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
 			}
 		}
-	}
+
+		$remotePrimaryKey = null;
+		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");
+			$refAcl = ACL::getAclByNamespace($backRef['namespace']);
+			if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source");
+			$sqlRefRootTableName = $refAcl->getRootTableName();
+			$refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
+			$sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
+			$remotePrimaryKey = DB::getPDO()->fetchValue("
+				select refTable.REMOTE_PRIMARY_KEY
+				from `{$refTable}` refTable
+				where refTable.PRIMARY_KEY = {$sqlBackRefPk}
+			");
+			if (!$remotePrimaryKey) return array();
+		}
+		DBG::log($this->getName() . "::getItems \$remotePrimaryKey({$remotePrimaryKey})");
+		if ($remotePrimaryKey) {
+			if (!array_key_exists($remotePrimaryKey, $items)) return array();
+			$items = array($remotePrimaryKey => $items[$remotePrimaryKey]);
+		}
+
 		if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
 			if (!array_key_exists($pk, $items)) return array();
 			$items = array($pk => $items[$pk]);
@@ -81,8 +118,8 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
 			$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";}
+			DBG::log($queryWhereBuilder, 'array', $this->getName() . "::getItems \$queryWhereBuilder");
+			DBG::log($items, 'array', $this->getName() . "::getItems \$items");
 			$items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
 		}
 		$filterId = trim(V::get('f_id', '', $params));
@@ -137,13 +174,14 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
 			return 0;
 		});
 
-		if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
+		DBG::log($items, 'array', $this->getName() . "::getItems \$items");
 		return $items;
 	}
 	public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
 	public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
 	public function getGeomFieldType($fieldName) { return null; }
 	public function getPrimaryKeyField() { return 'id'; }
+	public function getSqlPrimaryKeyField() { return 'ID'; }
 	public function getAttributesFromZasoby() { return array(); }
 	public function isEnumerationField($fieldName) { return false; }
 	public function getEnumerations($fieldName) { return null; }