Skip to content

Commit

Permalink
Use cache in LDAP backend's checkPassword
Browse files Browse the repository at this point in the history
Signed-off-by: Akhil <[email protected]>
  • Loading branch information
akhil1508 committed Aug 16, 2024
1 parent 13a72d0 commit 5bf4942
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions apps/user_ldap/lib/User_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ public function canChangeAvatar($uid) {
* @return string|false
* @throws \Exception
*/
public function loginName2UserName($loginName) {
public function loginName2UserName($loginName, bool $forceLdapRefetch = false) {
$cacheKey = 'loginName2UserName-' . $loginName;
$username = $this->access->connection->getFromCache($cacheKey);

if ($username !== null) {
$ignoreCache = ($username === false && $forceLdapRefetch);
if ($username !== null && !$ignoreCache) {
return $username;
}

Expand All @@ -95,6 +96,9 @@ public function loginName2UserName($loginName) {
}
$username = $user->getUsername();
$this->access->connection->writeToCache($cacheKey, $username);
if ($forceLdapRefetch) {
$user->processAttributes($ldapRecord);
}
return $username;
} catch (NotOnLDAP $e) {
$this->access->connection->writeToCache($cacheKey, false);
Expand Down Expand Up @@ -138,16 +142,11 @@ public function getLDAPUserByLoginName($loginName) {
* @return false|string
*/
public function checkPassword($uid, $password) {
try {
$ldapRecord = $this->getLDAPUserByLoginName($uid);
} catch (NotOnLDAP $e) {
$this->logger->debug(
$e->getMessage(),
['app' => 'user_ldap', 'exception' => $e]
);
$username = $this->loginName2UserName($uid, true);
if ($username === false) {
return false;
}
$dn = $ldapRecord['dn'][0];
$dn = $this->access->username2dn($username);
$user = $this->access->userManager->get($dn);

if (!$user instanceof User) {
Expand All @@ -165,7 +164,6 @@ public function checkPassword($uid, $password) {
}

$this->access->cacheUserExists($user->getUsername());
$user->processAttributes($ldapRecord);
$user->markLogin();

return $user->getUsername();
Expand Down

0 comments on commit 5bf4942

Please sign in to comment.