Skip to content

Commit cbc1daa

Browse files
authored
Merge pull request #42653 from nextcloud/backport/42651/stable26
[stable26] perf: Use more performant way to obtain and check the email as a login name with token login
2 parents c00a190 + 926b3ce commit cbc1daa

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

lib/private/AllConfig.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333
namespace OC;
3434

35+
use Doctrine\DBAL\Platforms\OraclePlatform;
3536
use OCP\Cache\CappedMemoryCache;
3637
use OCP\DB\QueryBuilder\IQueryBuilder;
3738
use OCP\IConfig;
@@ -490,12 +491,15 @@ public function getUsersForUserValue($appName, $key, $value) {
490491
$this->fixDIInit();
491492

492493
$qb = $this->connection->getQueryBuilder();
494+
$configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
495+
? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
496+
: 'configvalue';
493497
$result = $qb->select('userid')
494498
->from('preferences')
495499
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
496500
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
497501
->andWhere($qb->expr()->eq(
498-
$qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR),
502+
$configValueColumn,
499503
$qb->createNamedParameter($value, IQueryBuilder::PARAM_STR))
500504
)->orderBy('userid')
501505
->executeQuery();
@@ -524,13 +528,18 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
524528
// Email address is always stored lowercase in the database
525529
return $this->getUsersForUserValue($appName, $key, strtolower($value));
526530
}
531+
527532
$qb = $this->connection->getQueryBuilder();
533+
$configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
534+
? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
535+
: 'configvalue';
536+
528537
$result = $qb->select('userid')
529538
->from('preferences')
530539
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
531540
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
532541
->andWhere($qb->expr()->eq(
533-
$qb->func()->lower($qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)),
542+
$qb->func()->lower($configValueColumn),
534543
$qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR))
535544
)->orderBy('userid')
536545
->executeQuery();

lib/private/User/Session.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,17 @@ public function logClientIn($user,
455455
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
456456
return false;
457457
}
458-
$users = $this->manager->getByEmail($user);
459-
if (!(\count($users) === 1 && $this->login($users[0]->getUID(), $password))) {
458+
459+
if ($isTokenPassword) {
460+
$dbToken = $this->tokenProvider->getToken($password);
461+
$userFromToken = $this->manager->get($dbToken->getUID());
462+
$isValidEmailLogin = $userFromToken->getEMailAddress() === $user;
463+
} else {
464+
$users = $this->manager->getByEmail($user);
465+
$isValidEmailLogin = (\count($users) === 1 && $this->login($users[0]->getUID(), $password));
466+
}
467+
468+
if (!$isValidEmailLogin) {
460469
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
461470
return false;
462471
}

tests/lib/User/SessionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ public function testLogClientInThrottlerEmail() {
11181118

11191119
$userSession->expects($this->once())
11201120
->method('isTokenPassword')
1121-
->willReturn(true);
1121+
->willReturn(false);
11221122
$userSession->expects($this->once())
11231123
->method('login')
11241124
->with('john@foo.bar', 'I-AM-AN-PASSWORD')

0 commit comments

Comments
 (0)