Use cache in LDAP backend's checkPassword

Signed-off-by: Akhil <akhil@e.email>
pull/35867/head
Akhil 2 years ago committed by Akhil
parent 13a72d0f0e
commit b1230cd53d
  1. 20
      apps/user_ldap/lib/User_LDAP.php
  2. 4
      apps/user_ldap/tests/User_LDAPTest.php

@ -76,11 +76,12 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
* @return string|false * @return string|false
* @throws \Exception * @throws \Exception
*/ */
public function loginName2UserName($loginName) { public function loginName2UserName($loginName, bool $forceLdapRefetch = false) {
$cacheKey = 'loginName2UserName-' . $loginName; $cacheKey = 'loginName2UserName-' . $loginName;
$username = $this->access->connection->getFromCache($cacheKey); $username = $this->access->connection->getFromCache($cacheKey);
if ($username !== null) { $ignoreCache = ($username === false && $forceLdapRefetch);
if ($username !== null && !$ignoreCache) {
return $username; return $username;
} }
@ -95,6 +96,9 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
} }
$username = $user->getUsername(); $username = $user->getUsername();
$this->access->connection->writeToCache($cacheKey, $username); $this->access->connection->writeToCache($cacheKey, $username);
if ($forceLdapRefetch) {
$user->processAttributes($ldapRecord);
}
return $username; return $username;
} catch (NotOnLDAP $e) { } catch (NotOnLDAP $e) {
$this->access->connection->writeToCache($cacheKey, false); $this->access->connection->writeToCache($cacheKey, false);
@ -138,16 +142,11 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
* @return false|string * @return false|string
*/ */
public function checkPassword($uid, $password) { public function checkPassword($uid, $password) {
try { $username = $this->loginName2UserName($uid, true);
$ldapRecord = $this->getLDAPUserByLoginName($uid); if ($username === false) {
} catch (NotOnLDAP $e) {
$this->logger->debug(
$e->getMessage(),
['app' => 'user_ldap', 'exception' => $e]
);
return false; return false;
} }
$dn = $ldapRecord['dn'][0]; $dn = $this->access->username2dn($username);
$user = $this->access->userManager->get($dn); $user = $this->access->userManager->get($dn);
if (!$user instanceof User) { if (!$user instanceof User) {
@ -165,7 +164,6 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
} }
$this->access->cacheUserExists($user->getUsername()); $this->access->cacheUserExists($user->getUsername());
$user->processAttributes($ldapRecord);
$user->markLogin(); $user->markLogin();
return $user->getUsername(); return $user->getUsername();

@ -148,6 +148,10 @@ class User_LDAPTest extends TestCase {
->method('dn2username') ->method('dn2username')
->with($this->equalTo('dnOfRoland,dc=test')) ->with($this->equalTo('dnOfRoland,dc=test'))
->willReturn($retVal); ->willReturn($retVal);
$this->access->expects($this->any())
->method('username2dn')
->with($this->equalTo('gunslinger'))
->willReturn('dnOfRoland,dc=test');
$this->access->expects($this->any()) $this->access->expects($this->any())
->method('stringResemblesDN') ->method('stringResemblesDN')
->with($this->equalTo('dnOfRoland,dc=test')) ->with($this->equalTo('dnOfRoland,dc=test'))

Loading…
Cancel
Save