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

Verified Commit aee30974 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

fix: remove Mail account on SSO logout

When logging out from AccountManager using SSO, the associated Mail account was not being removed whereas for non-SSO it was being removed as expected.

The `AccountManager.KEY_ACCOUNT_NAME` intent extra contains the username part of the email address (e.g., `username` from `username@example.com`).

For non-SSO accounts, accountName and email used to be tha same. However, for SSO accounts, the accountName contains only the username part of the email address.

This commit updates the logic to find the matching account by comparing
this username with the username part of the account's email address.
Previously, it attempted to match the full email address with just the
username, which would always fail.
parent f6a26b4f
Loading
Loading
Loading
Loading
Loading
+20 −11
Original line number Diff line number Diff line
@@ -20,8 +20,7 @@ import android.accounts.AccountManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import android.util.Patterns
import app.k9mail.core.android.common.accountmanager.AccountManagerConstants
import com.fsck.k9.Account
import com.fsck.k9.Preferences
@@ -47,7 +46,6 @@ class AccountSyncReceiver : BroadcastReceiver(), KoinComponent {
    private val accountRemover: BackgroundAccountRemover by inject()
    private val jobManager: K9JobManager by inject()

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent == null) {
            return
@@ -59,7 +57,6 @@ class AccountSyncReceiver : BroadcastReceiver(), KoinComponent {
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private fun createNewAccount(context: Context?) {
        pushController.init()
        context?.let {
@@ -87,14 +84,26 @@ class AccountSyncReceiver : BroadcastReceiver(), KoinComponent {
            return null
        }

        val account = intent.extras?.getString(AccountManager.KEY_ACCOUNT_NAME) ?: return null
        val accountName = intent.extras?.getString(AccountManager.KEY_ACCOUNT_NAME) ?: return null

        preferences.accounts.forEach {
            if (it.email == account) {
                return it
            }
        return findAccount(accountName)
    }

        return null
    private fun findAccount(accountName: String): Account? = preferences.accounts
        .firstOrNull { account -> matchesAccount(accountName, account) }

    private fun matchesAccount(accountName: String, account: Account) = when {
        isEmail(accountName) -> matchesEmail(account, accountName)
        else -> matchesUserName(account, accountName)
    }

    private fun isEmail(accountName: String) = Patterns.EMAIL_ADDRESS.matcher(accountName).matches()

    private fun matchesEmail(account: Account, accountName: String) = account.email == accountName

    private fun matchesUserName(account: Account, accountName: String) =
        account.email.substringBefore("@").equals(
            accountName.substringBefore("@"),
            ignoreCase = true,
        )
}