Skip to content

Commit

Permalink
Fix invalid query filter when using memberof
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanvyas22 authored and stevebauman committed Oct 11, 2023
1 parent d0ff61a commit 540f0fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/Query/Filter/ConditionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ protected function extractComponents(string $filter): array
$components = Str::whenContains(
$filter,
$this->operators,
fn ($operator, $filter) => explode($this->operator = $operator, $filter),
fn ($operator, $filter) => explode($this->operator = $operator, $filter, 2),
fn ($filter) => throw new ParserException("Invalid query condition. No operator found in [$filter]"),
);

if (count($components) !== 2) {
throw new ParserException("Invalid query filter [$filter]");
}

return $components;
}
}
28 changes: 21 additions & 7 deletions tests/Unit/Query/Filter/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ public function test_parsing_nested_filter_groups()
$this->assertEquals('(&(objectCategory=person)(objectClass=contact)(|(sn=Smith)(sn=Johnson)))', Parser::assemble($group));
}

public function test_parser_can_parse_value_with_equal_sign()
{
$nodes = Parser::parse('(&(objectClass=inetOrgPerson)(memberof=cn=foo,ou=Groups,dc=example,dc=org))');

$this->assertCount(1, $nodes);
$this->assertInstanceOf(GroupNode::class, $nodes[0]);

$this->assertCount(2, $nodes[0]->getNodes());

$groupNodes = $nodes[0]->getNodes();
$this->assertInstanceOf(ConditionNode::class, $groupNodes[0]);
$this->assertEquals('objectClass', $groupNodes[0]->getAttribute());
$this->assertEquals('=', $groupNodes[0]->getOperator());
$this->assertEquals('inetOrgPerson', $groupNodes[0]->getValue());

$this->assertInstanceOf(ConditionNode::class, $groupNodes[1]);
$this->assertEquals('memberof', $groupNodes[1]->getAttribute());
$this->assertEquals('=', $groupNodes[1]->getOperator());
$this->assertEquals('cn=foo,ou=Groups,dc=example,dc=org', $groupNodes[1]->getValue());
}

public function test_parser_throws_exception_when_missing_open_parenthesis_is_detected()
{
$this->expectException(ParserException::class);
Expand Down Expand Up @@ -167,13 +188,6 @@ public function test_parser_can_process_single_node()
$this->assertEquals('(foo=bar)', Parser::assemble($node));
}

public function test_parser_throws_exception_with_invalid_filter_with_additional_equals()
{
$this->expectExceptionMessage('Invalid query filter [foo=bar=baz]');

Parser::parse('(foo=bar=baz)');
}

public function test_parser_throws_exception_during_assemble_when_invalid_nodes_given()
{
$this->expectException(TypeError::class);
Expand Down

0 comments on commit 540f0fb

Please sign in to comment.