Loading lib/Listeners/UserChangedListener.php +30 −16 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,8 @@ class UserChangedListener implements IEventListener { { private const QUOTA_FEATURE = 'quota'; private const QUOTA_FEATURE = 'quota'; private const RECOVERY_EMAIL_FEATURE = 'recovery-email'; private $util; private $util; private $logger; private $logger; Loading @@ -40,39 +42,51 @@ class UserChangedListener implements IEventListener } } $feature = $event->getFeature(); $feature = $event->getFeature(); if ($feature !== self::QUOTA_FEATURE) { return; } $user = $event->getUser(); $user = $event->getUser(); $username = $user->getUID(); $username = $user->getUID(); if ($feature === self::QUOTA_FEATURE) { $updatedQuota = $event->getValue(); $updatedQuota = $event->getValue(); $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); $backend = $user->getBackend()->getBackendName(); $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 { try { if ($backend === 'SQL raw') { if ($backend === 'SQL raw') { $this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes); $this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes); } } if ($backend === 'LDAP') { if ($backend === 'LDAP') { $this->updateQuotaInLDAP($username, $quotaInBytes); $quotaAttribute = [ 'quota' => $quotaInBytes ]; $this->updateAttributesInLDAP($username, $quotaAttribute); } } } catch (Exception $e) { } catch (Exception $e) { $this->logger->error("Error setting quota for user $username " . $e->getMessage()); $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()) { if ($this->ldapConnectionService->isLDAPEnabled()) { $conn = $this->ldapConnectionService->getLDAPConnection(); $conn = $this->ldapConnectionService->getLDAPConnection(); $userDn = $this->ldapConnectionService->username2dn($username); $userDn = $this->ldapConnectionService->username2dn($username); $entry = [ if (!ldap_modify($conn, $userDn, $attributes)) { 'quota' => $quota ]; if (!ldap_modify($conn, $userDn, $entry)) { throw new Exception('Could not modify user entry at LDAP server!'); throw new Exception('Could not modify user entry at LDAP server!'); } } $this->ldapConnectionService->closeLDAPConnection($conn); $this->ldapConnectionService->closeLDAPConnection($conn); Loading Loading
lib/Listeners/UserChangedListener.php +30 −16 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,8 @@ class UserChangedListener implements IEventListener { { private const QUOTA_FEATURE = 'quota'; private const QUOTA_FEATURE = 'quota'; private const RECOVERY_EMAIL_FEATURE = 'recovery-email'; private $util; private $util; private $logger; private $logger; Loading @@ -40,39 +42,51 @@ class UserChangedListener implements IEventListener } } $feature = $event->getFeature(); $feature = $event->getFeature(); if ($feature !== self::QUOTA_FEATURE) { return; } $user = $event->getUser(); $user = $event->getUser(); $username = $user->getUID(); $username = $user->getUID(); if ($feature === self::QUOTA_FEATURE) { $updatedQuota = $event->getValue(); $updatedQuota = $event->getValue(); $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); $quotaInBytes = (int) $this->util->computerFileSize($updatedQuota); $backend = $user->getBackend()->getBackendName(); $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 { try { if ($backend === 'SQL raw') { if ($backend === 'SQL raw') { $this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes); $this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes); } } if ($backend === 'LDAP') { if ($backend === 'LDAP') { $this->updateQuotaInLDAP($username, $quotaInBytes); $quotaAttribute = [ 'quota' => $quotaInBytes ]; $this->updateAttributesInLDAP($username, $quotaAttribute); } } } catch (Exception $e) { } catch (Exception $e) { $this->logger->error("Error setting quota for user $username " . $e->getMessage()); $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()) { if ($this->ldapConnectionService->isLDAPEnabled()) { $conn = $this->ldapConnectionService->getLDAPConnection(); $conn = $this->ldapConnectionService->getLDAPConnection(); $userDn = $this->ldapConnectionService->username2dn($username); $userDn = $this->ldapConnectionService->username2dn($username); $entry = [ if (!ldap_modify($conn, $userDn, $attributes)) { 'quota' => $quota ]; if (!ldap_modify($conn, $userDn, $entry)) { throw new Exception('Could not modify user entry at LDAP server!'); throw new Exception('Could not modify user entry at LDAP server!'); } } $this->ldapConnectionService->closeLDAPConnection($conn); $this->ldapConnectionService->closeLDAPConnection($conn); Loading