Skip to content

Commit

Permalink
fix: second join and where conditions added only when search param is…
Browse files Browse the repository at this point in the history
… available

Signed-off-by: yemkareems <yemkareems@gmail.com>
  • Loading branch information
yemkareems committed Jul 4, 2024
1 parent 89d0aeb commit 0164f5e
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,23 +504,26 @@ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string
$this->fixDIInit();

$query = $this->connection->getQueryBuilder();

$query->selectDistinct('uid')
->from('users', 'u')
->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
$query->expr()->eq('p.userid', 'uid'),
$query->expr()->eq('p.appid', $query->expr()->literal('login')),
$query->expr()->eq('p.configkey', $query->expr()->literal('lastLogin')))
)->leftJoin('u', 'preferences', 'p1', $query->expr()->andX(
$query->expr()->eq('p1.userid', 'uid'),
$query->expr()->eq('p1.appid', $query->expr()->literal('settings')),
$query->expr()->eq('p1.configkey', $query->expr()->literal('email')))
)
// sqlite doesn't like re-using a single named parameter here
->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
->orWhere($query->expr()->iLike('p1.configvalue', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
->orderBy($query->func()->lower('p.configvalue'), 'DESC')
);
if($search !== '') {
$query->leftJoin('u', 'preferences', 'p1', $query->expr()->andX(
$query->expr()->eq('p1.userid', 'uid'),
$query->expr()->eq('p1.appid', $query->expr()->literal('settings')),
$query->expr()->eq('p1.configkey', $query->expr()->literal('email')))
)
// sqlite doesn't like re-using a single named parameter here
->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
->orWhere($query->expr()->iLike('p1.configvalue', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%'))
);
}
$query->orderBy($query->func()->lower('p.configvalue'), 'DESC')
->addOrderBy('uid_lower', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit);
Expand Down

0 comments on commit 0164f5e

Please sign in to comment.