瀏覽代碼

updated WPS create and delete Relations

Piotr Labudda 8 年之前
父節點
當前提交
6f6efd485b

+ 4 - 4
SE/se-lib/ACL.php

@@ -243,12 +243,12 @@ class ACL {
 		return $refConfig->source;
 	}
 
-	public static function addRef($ns, $typeName, $pk, $remotePk) {
-		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+	public static function addRef($ns, $remoteTypeName, $pk, $remotePk) {
+		$refTable = ACL::getRefTable($ns, $remoteTypeName); // TODO: RefConfig::fetch($ns, $remoteTypeName);
 		DB::getPDO()->insert($refTable, [ 'PRIMARY_KEY' => $pk, 'REMOTE_PRIMARY_KEY' => $remotePk ]);
 	}
-	public static function addListRef($ns, $typeName, $pk, $listRemotePk) {
-		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+	public static function addListRef($ns, $remoteTypeName, $pk, $listRemotePk) {
+		$refTable = ACL::getRefTable($ns, $remoteTypeName); // TODO: RefConfig::fetch($ns, $remoteTypeName);
 		foreach ($listRemotePk as $remotePk) {
 			DB::getPDO()->insert($refTable, [ 'PRIMARY_KEY' => $pk, 'REMOTE_PRIMARY_KEY' => $remotePk ]);
 		}

+ 12 - 5
SE/se-lib/Api/Process/P5/CreateRelation.php → SE/se-lib/Api/Process/P5/CreateRelations.php

@@ -2,7 +2,7 @@
 
 Lib::loadClass('Relations');
 
-class Api_Process_P5_CreateRelation { // TODO: extends Api_ProcessBase
+class Api_Process_P5_CreateRelations { // TODO: extends Api_ProcessBase
 
 	static function run($args, $responseForm) {
 		$typeName = (!empty($args['typeName'][0])) ? $args['typeName'][0] : '';
@@ -14,11 +14,18 @@ class Api_Process_P5_CreateRelation { // TODO: extends Api_ProcessBase
 		$listRemotePrimaryKeys = (!empty($args['primaryKey'])) ? $args['remotePrimaryKey'] : [];
 		if (empty($listRemotePrimaryKeys)) throw new Api_OwsException("Missing value for 'remotePrimaryKey'", 501, null, 'MissingParameterValue', 'request');
 
-		if (!Relations::isAllowedToCreateRelation($userLogin, $typeName, $primaryKey, $remoteTypeName)) {
-			throw new Api_OwsException("User is not allowed to create relation from '{$typeName}.{$primaryKey}' to '{$remoteTypeName}' ", 501, null, 'AccessDenied', 'request');
+		try {
+			Relations::isAllowedToCreateRelation($userLogin = User::getLogin(), $typeName, $primaryKey, $remoteTypeName);
+			Relations::createRelations($typeName, $primaryKey, $remoteTypeName, $listRemotePrimaryKeys);
+			// $listRelations = Relations::getRelations($typeName, $primaryKey, $remoteTypeName);
+			// $totalRelations = count($listRelations);
+		} catch (HttpException $e) {
+			DBG::log($e);
+			throw new Api_OwsException($e->getMessage(), $e->getCode(), null, Http::getStatusText($e->getCode()), 'request');
+		} catch (Exception $e) {
+			DBG::log($e);
+			throw new Api_OwsException("User is not allowed to read relations from '{$typeName}.{$primaryKey}' to '{$remoteTypeName}'.\n" . $e->getMessage(), 501, null, 'AccessDenied', 'request');
 		}
-		Relations::createRelations($typeName, $primaryKey, $remoteTypeName, $listRemotePrimaryKeys);
-		$listSelectedState = Relations::getRelations($typeName, $primaryKey);
 
 		header('Content-Type: application/json');
 		echo json_encode([

+ 8 - 5
SE/se-lib/Api/Process/P5/DeleteRelation.php → SE/se-lib/Api/Process/P5/DeleteRelations.php

@@ -2,7 +2,7 @@
 
 Lib::loadClass('Relations');
 
-class Api_Process_P5_DeleteRelation { // TODO: extends Api_ProcessBase
+class Api_Process_P5_DeleteRelations { // TODO: extends Api_ProcessBase
 
 	static function run($args, $responseForm) {
 		$typeName = (!empty($args['typeName'][0])) ? $args['typeName'][0] : '';
@@ -14,14 +14,17 @@ class Api_Process_P5_DeleteRelation { // TODO: extends Api_ProcessBase
 		$listRemotePrimaryKeys = (!empty($args['primaryKey'])) ? $args['remotePrimaryKey'] : [];
 		if (empty($listRemotePrimaryKeys)) throw new Api_OwsException("Missing value for 'remotePrimaryKey'", 501, null, 'MissingParameterValue', 'request');
 
-		if (!Relations::isAllowedToDeleteRelation($userLogin, $typeName, $primaryKey, $remoteTypeName)) {
-			throw new Api_OwsException("User is not allowed to create relation from '{$typeName}.{$primaryKey}' to '{$remoteTypeName}' ", 501, null, 'AccessDenied', 'request');
-		}
 		try {
+			Relations::isAllowedToDeleteRelation($userLogin = User::getLogin(), $typeName, $primaryKey, $remoteTypeName);
 			Relations::deleteRelations($typeName, $primaryKey, $remoteTypeName, $listRemotePrimaryKeys);
+			// $listRelations = Relations::getRelations($typeName, $primaryKey, $remoteTypeName);
+			// $totalRelations = count($listRelations);
+		} catch (HttpException $e) {
+			DBG::log($e);
+			throw new Api_OwsException($e->getMessage(), $e->getCode(), null, Http::getStatusText($e->getCode()), 'request');
 		} catch (Exception $e) {
 			DBG::log($e);
-			throw new Api_OwsException($e->getMessage(), 501, null, 'AccessDenied', 'request');
+			throw new Api_OwsException("User is not allowed to delete relations from '{$typeName}.{$primaryKey}' to '{$remoteTypeName}'.\n" . $e->getMessage(), 501, null, 'AccessDenied', 'request');
 		}
 
 		header('Content-Type: application/json');

+ 2 - 2
SE/se-lib/Api/WpsHelper.php

@@ -139,8 +139,8 @@ class Api_WpsHelper {
 					$wpsArgRemoteTypeName,
 				],
 			]);
-			$defaultWpsProcessList['p5:createRelation'] = array_merge([ 'title' => "Create relation between features" ], $relationsBase);
-			$defaultWpsProcessList['p5:deleteRelation'] = array_merge([ 'title' => "Delete relation between features" ], $relationsBase);
+			$defaultWpsProcessList['p5:createRelations'] = array_merge([ 'title' => "Create relations between features" ], $relationsBase);
+			$defaultWpsProcessList['p5:deleteRelations'] = array_merge([ 'title' => "Delete relations between features" ], $relationsBase);
 		}
 
 		array_walk($defaultWpsProcessList, function (&$wpsFunction, $identifier) {

+ 14 - 6
SE/se-lib/Relations.php

@@ -6,8 +6,14 @@ Lib::loadClass('Api_WfsNs');
 class Relations {
 
 	static function isAllowedToCreateRelation($userLogin, $typeName, $primaryKey, $remoteTypeName) {
-		DBG::log("TODO: Relations::isAllowedToCreateRelation()...");
-		return false;
+		if ($userLogin !== User::getLogin()) throw new Exception("Not Implemented - isAllowedToCreateRelation for another user");
+		$acl = ACL::getAclByNamespace( Api_WfsNs::toNamespace($typeName) );
+		if (!$acl->hasField($remoteTypeName)) throw new Exception("Missing field in given object. Field '{$remoteTypeName}' not exists in '{$typeName}'");
+		if (!$acl->canCreateField($remoteTypeName)) throw new HttpException("Forbidden create relations from {$typeName} to {$remoteTypeName} ", 403);
+		$item = $acl->getItem($primaryKey);
+		if (!$item) throw new HttpException("Object not found {$typeName}.{$primaryKey}", 404);
+		if (!$acl->canWriteObjectField($remoteTypeName, $item)) throw new HttpException("Forbidden create relations from {$typeName}.{$primaryKey} to {$remoteTypeName} ", 403);
+		return true;
 	}
 
 	static function isAllowedToGetRelation($userLogin, $typeName, $primaryKey, $remoteTypeName) {
@@ -32,13 +38,15 @@ class Relations {
 	}
 
 	static function createRelations($typeName, $primaryKey, $remoteTypeName, $listRemotePrimaryKeys) {
-		$acl = ACL::getAclByNamespace( Api_WfsNs::toNamespace($typeName) );
-		throw new Exception("TODO: Relations::createRelation");
+		$namespace = Api_WfsNs::toNamespace($typeName);
+		$acl = ACL::getAclByNamespace($namespace);
+		ACL::addListRef($namespace, $remoteTypeName, $primaryKey, $listRemotePrimaryKeys);
 	}
 
 	static function deleteRelations($typeName, $primaryKey, $remoteTypeName, $listRemotePrimaryKeys) {
-		$acl = ACL::getAclByNamespace( Api_WfsNs::toNamespace($typeName) );
-		throw new Exception("TODO: Relations::deleteRelations");
+		$namespace = Api_WfsNs::toNamespace($typeName);
+		$acl = ACL::getAclByNamespace($namespace);
+		ACL::removeListRef($namespace, $remoteTypeName, $primaryKey, $listRemotePrimaryKeys);
 	}
 
 }