Commit 3e9fea12 authored by Martin's avatar Martin
Browse files

IOS-2885 - fix IOS-2893 issue

parent e1cb27e0
......@@ -42,6 +42,8 @@ public class VerifiableAccount: VerifiableAccountProtocol {
/// Someone who tells us whether or not to create a pEp folder for storing sync messages for
/// synced accounts.
private let usePEPFolderProvider: UsePEPFolderProviderProtocol?
public var originalImapPassword: String?
public var originalSmtpPassword: String?
// MARK: - VerifiableAccountProtocol (delegate)
......@@ -91,7 +93,9 @@ public class VerifiableAccount: VerifiableAccountProtocol {
manuallyTrustedImapServer: Bool = false,
keySyncEnable: Bool = true,
containsCompleteServerInfo: Bool = false,
usePEPFolderProvider: UsePEPFolderProviderProtocol? = nil) {
usePEPFolderProvider: UsePEPFolderProviderProtocol? = nil,
originalImapPassword: String? = nil,
originalSmtpPassword: String? = nil) {
self.verifiableAccountDelegate = verifiableAccountDelegate
self.address = address
self.userName = userName
......@@ -112,6 +116,8 @@ public class VerifiableAccount: VerifiableAccountProtocol {
self.keySyncEnable = keySyncEnable
self.containsCompleteServerInfo = containsCompleteServerInfo
self.usePEPFolderProvider = usePEPFolderProvider
self.originalImapPassword = originalImapPassword
self.originalSmtpPassword = originalSmtpPassword
}
// MARK: - VerifiableAccountProtocol (behaviour)
......@@ -201,16 +207,41 @@ extension VerifiableAccount {
switch theImapResult {
case .failure(let error):
resetPasswordsInKeychain()
verifiableAccountDelegate?.didEndVerification(result: .failure(error))
case .success(()):
switch theSmtpResult {
case .failure(let error):
resetPasswordsInKeychain()
verifiableAccountDelegate?.didEndVerification(result: .failure(error))
case .success(()):
self.verifiableAccountDelegate?.didEndVerification(result: .success(()))
verifiableAccountDelegate?.didEndVerification(result: .success(()))
}
}
}
private func resetPasswordsInKeychain() {
do {
guard let (_, cdAccount, _, _) = try createAccount() else {
// Assuming this is caused by invalid data.
throw VerifiableAccountValidationError.invalidUserData
}
// Set the original passwords again before moc.rollback to save it in Key Chain.
let account = cdAccount.account()
if let originalPassword = originalImapPassword {
account.moc.performAndWait {
account.imapServer?.credentials.password = originalPassword
}
}
if let originalPassword = originalSmtpPassword {
account.moc.performAndWait {
account.smtpServer?.credentials.password = originalPassword
}
}
} catch {
Log.shared.errorAndCrash("Can not create an account")
}
}
}
// MARK: - Private Validation Helpers
......@@ -494,7 +525,9 @@ extension VerifiableAccount {
/// to find out if server data is still missing or not.
/// - Parameter type: The account type
public static func verifiableAccount(for type: AccountType,
usePEPFolderProvider: UsePEPFolderProviderProtocol? = nil) -> VerifiableAccountProtocol {
usePEPFolderProvider: UsePEPFolderProviderProtocol? = nil,
originalImapPassword: String? = nil,
originalSmtpPassword: String? = nil) -> VerifiableAccountProtocol {
var account = VerifiableAccount(verifiableAccountDelegate: nil,
address: nil,
userName: nil,
......@@ -514,7 +547,10 @@ extension VerifiableAccount {
manuallyTrustedImapServer: false,
keySyncEnable: true,
containsCompleteServerInfo: false,
usePEPFolderProvider: usePEPFolderProvider)
usePEPFolderProvider: usePEPFolderProvider,
originalImapPassword: originalImapPassword,
originalSmtpPassword: originalSmtpPassword)
switch type {
case .gmail:
account = VerifiableAccount(verifiableAccountDelegate: nil,
......
......@@ -56,7 +56,7 @@ class EditableAccountSettingsViewModel {
private var passwordChanged: Bool = false
private var originalImapPassword: String?
private var originalSMTPPassword: String?
private var originalSmtpPassword: String?
/// Retrieves the name of an transport security option.
/// - Parameter index: The index of the option.
......@@ -112,7 +112,7 @@ class EditableAccountSettingsViewModel {
}
} else {
originalImapPassword = account.imapServer?.credentials.password
originalSMTPPassword = account.smtpServer?.credentials.password
originalSmtpPassword = account.smtpServer?.credentials.password
}
self.generateSections()
}
......@@ -435,7 +435,7 @@ extension EditableAccountSettingsViewModel {
private func update(input: Input) {
var theVerifier = verifiableAccount ??
VerifiableAccount.verifiableAccount(for: .other)
VerifiableAccount.verifiableAccount(for: .other, originalImapPassword: originalImapPassword, originalSmtpPassword: originalSmtpPassword)
theVerifier.verifiableAccountDelegate = self
verifiableAccount = theVerifier
......@@ -462,8 +462,8 @@ extension EditableAccountSettingsViewModel {
if originalImapPassword != nil {
theVerifier.imapPassword = originalImapPassword
}
if originalSMTPPassword != nil {
theVerifier.smtpPassword = originalSMTPPassword
if originalSmtpPassword != nil {
theVerifier.smtpPassword = originalSmtpPassword
} else {
Log.shared.errorAndCrash("Is not OAuth2, hasn't got a new password, nor original password")
return
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment