Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52a3dd87 authored by Akhil's avatar Akhil 🙂
Browse files

Merge branch 'check-recovery-email-change' into 'main'

Handle recovery email change also

See merge request !19
parents 80ed4249 d83cd299
Loading
Loading
Loading
Loading
Loading
+30 −16
Original line number Diff line number Diff line
@@ -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();
        
        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);