From d83cd2995eac55a6d49eaa5b28e9608c0a5512b5 Mon Sep 17 00:00:00 2001 From: Akhil Date: Tue, 5 Jul 2022 21:00:19 +0530 Subject: [PATCH] Handle recovery email change also --- lib/Listeners/UserChangedListener.php | 46 +++++++++++++++++---------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 40c782c2..d6942f61 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -17,6 +17,8 @@ class UserChangedListener implements IEventListener { private const QUOTA_FEATURE = 'quota'; + private const RECOVERY_EMAIL_FEATURE = 'recovery-email'; + private $util; private $logger; @@ -40,39 +42,51 @@ class UserChangedListener implements IEventListener } $feature = $event->getFeature(); - - if ($feature !== self::QUOTA_FEATURE) { - return; - } $user = $event->getUser(); $username = $user->getUID(); - $updatedQuota = $event->getValue(); - $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); - $backend = $user->getBackend()->getBackendName(); + + if ($feature === self::QUOTA_FEATURE) { + $updatedQuota = $event->getValue(); + $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); + $backend = $user->getBackend()->getBackendName(); + + $this->updateQuota($username, $backend, $quotaInBytes); + } + + if ($feature === self::RECOVERY_EMAIL_FEATURE) { + $recoveryEmail = $event->getValue(); + $recoveryEmailAttribute = [ + 'recoveryMailAddress' => $recoveryEmail + ]; + $this->updateAttributesInLDAP($username, $recoveryEmailAttribute); + } + } + + private function updateQuota(string $username, string $backend, int $quotaInBytes) + { try { if ($backend === 'SQL raw') { $this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes); } if ($backend === 'LDAP') { - $this->updateQuotaInLDAP($username, $quotaInBytes); + $quotaAttribute = [ + 'quota' => $quotaInBytes + ]; + $this->updateAttributesInLDAP($username, $quotaAttribute); } } catch (Exception $e) { $this->logger->error("Error setting quota for user $username " . $e->getMessage()); } } - - private function updateQuotaInLDAP(string $username, int $quota) + + private function updateAttributesInLDAP(string $username, array $attributes) { if ($this->ldapConnectionService->isLDAPEnabled()) { $conn = $this->ldapConnectionService->getLDAPConnection(); $userDn = $this->ldapConnectionService->username2dn($username); - - $entry = [ - 'quota' => $quota - ]; - - if (!ldap_modify($conn, $userDn, $entry)) { + + if (!ldap_modify($conn, $userDn, $attributes)) { throw new Exception('Could not modify user entry at LDAP server!'); } $this->ldapConnectionService->closeLDAPConnection($conn); -- GitLab