Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/1109 bug incorrect display of hashlists in the super hashlist table #1115

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/inc/apiv2/common/AbstractModelAPI.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use DBA\JoinFilter;
use DBA\Factory;
use DBA\ContainFilter;
use DBA\OrderFilter;
use DBA\QueryFilter;
use Middlewares\Utils\HttpErrorException;


Expand Down Expand Up @@ -120,31 +118,30 @@ final protected static function getManyToOneRelation(


/**
* Retrieve ManyToOne relalation for $objects ('parents') of type $targetFactory via 'intermidate'
* Retrieve ManyToMany relation for $objects ('parents') of type $targetFactory via 'intermediate'
* of $intermediateFactory joining on $joinField (between 'intermediate' and 'target'). Filtered by
* $filterField at $intermediateFactory.
*
* @param array $objects Objects Fetch relation for selected Objects
* @param string $objectField Field to use as base for $objects
* @param object $intermediateFactory Factory used as intermediate between parentObject and targetObject
* @param string $filterField Filter field of intermadiateObject to filter against $objects field
* @param string $filterField Filter field of intermediateObject to filter against $objects field
* @param object $targetFactory Object properties of objects returned
* @param string $joinField Field to connect 'intermediate' to 'target'

* @return array
*/
final protected static function getManyToOneRelationViaIntermediate(
final protected static function getManyToManyRelation(
array $objects,
string $objectField,
object $intermediateFactory,
string $filterField,
object $targetFactory,
string $joinField,
string $joinField
): array {
assert($intermediateFactory instanceof AbstractModelFactory);
assert($targetFactory instanceof AbstractModelFactory);
$retval = array();

$many2Many = array();

/* Retrieve Parent -> Intermediate -> Target objects */
$objectIds = [];
Expand All @@ -156,20 +153,21 @@ final protected static function getManyToOneRelationViaIntermediate(
$jF = new JoinFilter($intermediateFactory, $joinField, $joinField);
$hO = $targetFactory->filter([Factory::FILTER => $qF, Factory::JOIN => $jF]);

/* Build mapping Parent -> Intermediate */
$i2p = [];
foreach($hO[$intermediateFactory->getModelName()] as $intermidiateObject) {
$kv = $intermidiateObject->getKeyValueDict();
$i2p[$kv[$joinField]] = $kv[$filterField];
}

/* Associate Target -> Parent (via Intermediate) */
foreach($hO[$targetFactory->getModelName()] as $targetObject) {
$parent = $i2p[$targetObject->getKeyValueDict()[$joinField]];
$retval[$parent][] = $targetObject;
$intermediateObjectList = $hO[$intermediateFactory->getModelName()];
$targetObjectList = $hO[$targetFactory->getModelName()];

$intermediateObject = current($intermediateObjectList);
$targetObject = current($targetObjectList);

while ($intermediateObject && $targetObject) {
$kv = $intermediateObject->getKeyValueDict();
$many2Many[$kv[$filterField]][] = $targetObject;

$intermediateObject = next($intermediateObjectList);
$targetObject = next($targetObjectList);
}

return $retval;
return $many2Many;
}

/**
Expand Down Expand Up @@ -216,7 +214,7 @@ public function deleteOne(Request $request, Response $response, array $args): Re


/**
* Request single object from database & validate permissons
* Request single object from database & validate permissions
*/
protected function doFetch(Request $request, string $pk): mixed
{
Expand Down
4 changes: 2 additions & 2 deletions src/inc/apiv2/model/accessgroups.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'userMembers':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
AccessGroup::ACCESS_GROUP_ID,
Factory::getAccessGroupUserFactory(),
Expand All @@ -39,7 +39,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
User::USER_ID
);
case 'agentMembers':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
AccessGroup::ACCESS_GROUP_ID,
Factory::getAccessGroupAgentFactory(),
Expand Down
2 changes: 1 addition & 1 deletion src/inc/apiv2/model/agents.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'accessGroups':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Agent::AGENT_ID,
Factory::getAccessGroupAgentFactory(),
Expand Down
4 changes: 2 additions & 2 deletions src/inc/apiv2/model/hashlists.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
);
case 'hashlists':
/* PARENT_HASHLIST_ID in use in intermediate table */
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Hashlist::HASHLIST_ID,
Factory::getHashlistHashlistFactory(),
Expand All @@ -67,7 +67,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
Hashlist::HASHLIST_ID,
);
case 'tasks':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Hashlist::HASHLIST_ID,
Factory::getTaskWrapperFactory(),
Expand Down
2 changes: 1 addition & 1 deletion src/inc/apiv2/model/pretasks.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'pretaskFiles':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Pretask::PRETASK_ID,
Factory::getFilePretaskFactory(),
Expand Down
2 changes: 1 addition & 1 deletion src/inc/apiv2/model/supertasks.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'pretasks':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Supertask::SUPERTASK_ID,
Factory::getSupertaskPretaskFactory(),
Expand Down
6 changes: 3 additions & 3 deletions src/inc/apiv2/model/tasks.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'assignedAgents':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Task::TASK_ID,
Factory::getAssignmentFactory(),
Expand All @@ -57,7 +57,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
CrackerBinaryType::CRACKER_BINARY_TYPE_ID
);
case 'hashlist':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Task::TASK_WRAPPER_ID,
Factory::getTaskWrapperFactory(),
Expand All @@ -73,7 +73,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
Speed::TASK_ID
);
case 'files':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
Task::TASK_ID,
Factory::getFileTaskFactory(),
Expand Down
2 changes: 1 addition & 1 deletion src/inc/apiv2/model/users.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function fetchExpandObjects(array $objects, string $expand): mixed {
/* Expand requested section */
switch($expand) {
case 'accessGroups':
return $this->getManyToOneRelationViaIntermediate(
return $this->getManyToManyRelation(
$objects,
User::USER_ID,
Factory::getAccessGroupUserFactory(),
Expand Down
Loading