Skip to content

Commit 92ae92d

Browse files
committed
bug #257 Fix clients filtering when using doctrine persistence (ajgarlag)
This PR was merged into the 1.x-dev branch. Discussion ---------- Fix clients filtering when using `doctrine` persistence It was using an 'IN' clause in fields stored as a space separated string of values. Commits ------- 2ce4f54 Fix clients filtering when using `doctrine` persistence
2 parents 7fa74ed + 2ce4f54 commit 92ae92d

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

src/Manager/Doctrine/ClientManager.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace League\Bundle\OAuth2ServerBundle\Manager\Doctrine;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use Doctrine\ORM\QueryBuilder;
89
use League\Bundle\OAuth2ServerBundle\Event\PreSaveClientEvent;
910
use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter;
1011
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
@@ -74,38 +75,45 @@ public function remove(ClientInterface $client): void
7475
public function list(?ClientFilter $clientFilter): array
7576
{
7677
$repository = $this->entityManager->getRepository($this->clientFqcn);
77-
$criteria = self::filterToCriteria($clientFilter);
78+
$qb = $repository->createQueryBuilder('c');
79+
self::setQueryBuilderFilters($qb, $clientFilter);
7880

7981
/** @var list<AbstractClient> */
80-
return $repository->findBy($criteria);
82+
return $qb->getQuery()->getResult();
8183
}
8284

83-
/**
84-
* @return array{grants?: list<Grant>, redirect_uris?: list<RedirectUri>, scopes?: list<Scope>}
85-
*/
86-
private static function filterToCriteria(?ClientFilter $clientFilter): array
85+
private static function setQueryBuilderFilters(QueryBuilder $qb, ?ClientFilter $clientFilter): void
8786
{
8887
if (null === $clientFilter || false === $clientFilter->hasFilters()) {
89-
return [];
88+
return;
9089
}
9190

92-
$criteria = [];
91+
self::setFieldFilter($qb, 'grants', $clientFilter->getGrants());
9392

94-
$grants = $clientFilter->getGrants();
95-
if ($grants) {
96-
$criteria['grants'] = $grants;
97-
}
93+
self::setFieldFilter($qb, 'redirect_uris', $clientFilter->getRedirectUris());
9894

99-
$redirectUris = $clientFilter->getRedirectUris();
100-
if ($redirectUris) {
101-
$criteria['redirect_uris'] = $redirectUris;
102-
}
95+
self::setFieldFilter($qb, 'scopes', $clientFilter->getScopes());
96+
}
10397

104-
$scopes = $clientFilter->getScopes();
105-
if ($scopes) {
106-
$criteria['scopes'] = $scopes;
98+
/**
99+
* @param list<Scope>|list<RedirectUri>|list<Grant> $values
100+
*/
101+
private static function setFieldFilter(QueryBuilder $qb, string $field, array $values): void
102+
{
103+
foreach ($values as $key => $value) {
104+
$qb->andWhere(
105+
$qb->expr()->orX(
106+
$qb->expr()->eq('c.' . $field, ':' . $field . $key),
107+
$qb->expr()->like('c.' . $field, ':space_' . $field . $key),
108+
$qb->expr()->like('c.' . $field, ':' . $field . '_space' . $key),
109+
$qb->expr()->like('c.' . $field, ':space_' . $field . '_space' . $key),
110+
)
111+
)
112+
->setParameter($field . $key, (string) $value)
113+
->setParameter('space_' . $field . $key, '% ' . (string) $value)
114+
->setParameter($field . '_space' . $key, (string) $value . ' %')
115+
->setParameter('space_' . $field . '_space' . $key, '% ' . (string) $value . ' %')
116+
;
107117
}
108-
109-
return $criteria;
110118
}
111119
}

tests/Acceptance/ListClientsCommandTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,27 @@ public function testListClientColumns(): void
9191

9292
public function testListFiltersClients(): void
9393
{
94-
$clientA = $this->fakeAClient('client', 'client-a', 'client-a-secret');
94+
$clientA = $this->fakeAClient('client', 'client-a', 'client-a-secret')
95+
->setScopes(new Scope('client-a-scope'), new Scope('other-scope-1'));
9596
$this->getClientManager()->save($clientA);
9697

9798
$clientB = $this
9899
->fakeAClient('client', 'client-b', 'client-b-secret')
99-
->setScopes(new Scope('client-b-scope'))
100+
->setScopes(new Scope('client-b-scope'), new Scope('other-scope'))
100101
;
101102
$this->getClientManager()->save($clientB);
102103

104+
$clientC = $this
105+
->fakeAClient('client', 'client-c', 'client-c-secret')
106+
->setScopes(new Scope('client-c-scope'), new Scope('1-other-scope'))
107+
;
108+
$this->getClientManager()->save($clientC);
109+
103110
$command = $this->command();
104111
$commandTester = new CommandTester($command);
105112
$commandTester->execute([
106113
'command' => $command->getName(),
107-
'--scope' => $clientB->getScopes(),
114+
'--scope' => ['other-scope'],
108115
]);
109116
$output = $commandTester->getDisplay();
110117

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
-------- ------------ ----------------- ---------------- -------------- ------------
3-
name identifier secret scope redirect uri grant type
4-
-------- ------------ ----------------- ---------------- -------------- ------------
5-
client client-b client-b-secret client-b-scope
6-
-------- ------------ ----------------- ---------------- -------------- ------------
2+
-------- ------------ ----------------- ----------------------------- -------------- ------------
3+
name identifier secret scope redirect uri grant type
4+
-------- ------------ ----------------- ----------------------------- -------------- ------------
5+
client client-b client-b-secret client-b-scope, other-scope
6+
-------- ------------ ----------------- ----------------------------- -------------- ------------

0 commit comments

Comments
 (0)