diff --git a/iOSClient/Account Settings/NCAccountSettingsModel.swift b/iOSClient/Account Settings/NCAccountSettingsModel.swift index 7c72270581..08fa601180 100644 --- a/iOSClient/Account Settings/NCAccountSettingsModel.swift +++ b/iOSClient/Account Settings/NCAccountSettingsModel.swift @@ -168,20 +168,22 @@ class NCAccountSettingsModel: ObservableObject, ViewOnAppearHandling { @objc func changeAccount() { if let activeAccount { - self.appDelegate.changeAccount(activeAccount.account, userProfile: nil) + self.appDelegate.changeAccount(activeAccount.account, userProfile: nil) { } } } /// Function to delete the current account func deleteAccount() { if let activeAccount { - appDelegate.deleteAccount(activeAccount.account, wipe: false) + appDelegate.deleteAccount(activeAccount.account) if let account = NCManageDatabase.shared.getAllAccount().first?.account { - appDelegate.changeAccount(account, userProfile: nil) + appDelegate.changeAccount(account, userProfile: nil) { + onViewAppear() + } } else { dismissView = true + appDelegate.openLogin(selector: NCGlobal.shared.introLogin, openLoginWeb: false) } - onViewAppear() } } } diff --git a/iOSClient/AppDelegate.swift b/iOSClient/AppDelegate.swift index 18b206cf06..9b55094910 100644 --- a/iOSClient/AppDelegate.swift +++ b/iOSClient/AppDelegate.swift @@ -317,8 +317,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD let accounts = NCManageDatabase.shared.getAllAccount() for account in accounts { if account.account == accountPush { - self.changeAccount(account.account, userProfile: nil) - findAccount = true + self.changeAccount(account.account, userProfile: nil) { + findAccount = true + } } } } @@ -464,20 +465,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // MARK: - Account - func createAccount(url: String, + func createAccount(urlBase: String, user: String, password: String, completion: @escaping (_ error: NKError) -> Void) { - var urlBase = url - if urlBase.last == "/" { - urlBase = String(urlBase.dropLast()) - } - let oldAccount: String = "\(user) \(urlBase)" - var account: String = "\(user)@\(urlBase)" - if let accounts = NCManageDatabase.shared.getAccounts(), - accounts.contains(oldAccount) { - account = oldAccount - } + var urlBase = urlBase + if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) } + let account: String = "\(user) \(urlBase)" NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) NextcloudKit.shared.getUserProfile { _, userProfile, _, error in @@ -485,20 +479,24 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD NCManageDatabase.shared.deleteAccount(account) NCManageDatabase.shared.addAccount(account, urlBase: urlBase, user: user, userId: userProfile.userId, password: password) NCKeychain().setClientCertificate(account: account, p12Data: NCNetworking.shared.p12Data, p12Password: NCNetworking.shared.p12Password) - self.changeAccount(account, userProfile: userProfile) + self.changeAccount(account, userProfile: userProfile) { + completion(error) + } } else { NextcloudKit.shared.setup(account: self.account, user: self.user, userId: self.userId, password: self.password, urlBase: self.urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) UIApplication.shared.firstWindow?.rootViewController?.present(alertController, animated: true) + completion(error) } - completion(error) } } - func changeAccount(_ account: String, userProfile: NKUserProfile?) { + func changeAccount(_ account: String, + userProfile: NKUserProfile?, + completion: () -> Void) { guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else { - return + return completion() } NCNetworking.shared.cancelAllQueue() @@ -537,19 +535,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD } NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeUser) + completion() } - func deleteAccount(_ account: String, wipe: Bool) { + func deleteAccount(_ account: String) { UIApplication.shared.allSceneSessionDestructionExceptFirst() if let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) { NCPushNotification.shared.unsubscribingNextcloudServerPushNotification(account: account.account, urlBase: account.urlBase, user: account.user, withSubscribing: false) } - NextcloudKit.shared.deleteAppPassword(serverUrl: urlBase, username: userId, password: password) { _, error in - print(error) - } - let results = NCManageDatabase.shared.getTableLocalFiles(predicate: NSPredicate(format: "account == %@", account), sorted: "ocId", ascending: false) let utilityFileSystem = NCUtilityFileSystem() for result in results { @@ -567,22 +562,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD self.userId = "" self.password = "" - if wipe { - let accounts = NCManageDatabase.shared.getAccounts() - if accounts?.count ?? 0 > 0 { - if let newAccount = accounts?.first { - self.changeAccount(newAccount, userProfile: nil) - } - } else { - openLogin(selector: NCGlobal.shared.introLogin, openLoginWeb: false) - } + /* + NextcloudKit.shared.deleteAppPassword(serverUrl: urlBase, username: userId, password: password) { _, error in + print(error) } + */ } func deleteAllAccounts() { let accounts = NCManageDatabase.shared.getAccounts() accounts?.forEach({ account in - deleteAccount(account, wipe: true) + deleteAccount(account) }) } diff --git a/iOSClient/Data/NCManageDatabase+Account.swift b/iOSClient/Data/NCManageDatabase+Account.swift index 68bc354607..567b60dfc0 100644 --- a/iOSClient/Data/NCManageDatabase+Account.swift +++ b/iOSClient/Data/NCManageDatabase+Account.swift @@ -85,15 +85,11 @@ extension NCManageDatabase { let realm = try Realm() try realm.write { let addObject = tableAccount() - addObject.account = account - NCKeychain().setPassword(account: account, password: password) - addObject.urlBase = urlBase addObject.user = user addObject.userId = userId - realm.add(addObject, update: .all) } } catch let error { diff --git a/iOSClient/Files/NCFiles.swift b/iOSClient/Files/NCFiles.swift index 69ef84747e..b6c6a181bf 100644 --- a/iOSClient/Files/NCFiles.swift +++ b/iOSClient/Files/NCFiles.swift @@ -289,7 +289,7 @@ class NCFiles: NCCollectionViewCommon { if NCManageDatabase.shared.getAllAccount().isEmpty { appDelegate.openLogin(selector: NCGlobal.shared.introLogin, openLoginWeb: false) } else if let account = tableAccount?.account, account != appDelegate.account { - appDelegate.changeAccount(account, userProfile: nil) + appDelegate.changeAccount(account, userProfile: nil) { } } else if isRoot { titleCurrentFolder = getNavigationTitle() navigationItem.title = titleCurrentFolder diff --git a/iOSClient/Login/NCLogin.swift b/iOSClient/Login/NCLogin.swift index f333fa3630..22a445ab24 100644 --- a/iOSClient/Login/NCLogin.swift +++ b/iOSClient/Login/NCLogin.swift @@ -210,15 +210,13 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } // AppConfig - if let serverUrl = configServerUrl { - if let username = self.configUsername, let password = configAppPassword { - createAccount(server: serverUrl, username: username, password: password) - return - } else if let username = self.configUsername, let password = configPassword { - getAppPassword(serverUrl: serverUrl, username: username, password: password) - return + if let url = configServerUrl { + if let user = self.configUsername, let password = configAppPassword { + return createAccount(urlBase: url, user: user, password: password) + } else if let user = self.configUsername, let password = configPassword { + return getAppPassword(urlBase: url, user: user, password: password) } else { - urlBase = serverUrl + urlBase = url } } } @@ -375,100 +373,57 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { loginButton.isEnabled = false NextcloudKit.shared.checkServer(serverUrl: serverUrl) { error in self.loginButton.isEnabled = true - self.standardLogin(url: urlBase, user: user, password: password, error: error) + if error == .success { + self.createAccount(urlBase: urlBase, user: user, password: password) + } else { + let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) + self.present(alertController, animated: true) + } } } } } - func standardLogin(url: String, user: String, password: String, error: NKError) { - if error == .success { - if let host = URL(string: url)?.host { - NCNetworking.shared.writeCertificate(host: host) - } - let urlBase = url - let account = user + " " + user - NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase) - NextcloudKit.shared.getUserProfile { _, userProfile, _, error in - if error == .success, let userProfile { - NCManageDatabase.shared.deleteAccount(account) - NCManageDatabase.shared.addAccount(account, urlBase: url, user: user, userId: userProfile.userId, password: password) - self.appDelegate.changeAccount(account, userProfile: userProfile) - let window = UIApplication.shared.firstWindow - if window?.rootViewController is NCMainTabBarController { - self.dismiss(animated: true) - } else { - if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { - controller.modalPresentationStyle = .fullScreen - controller.view.alpha = 0 - window?.rootViewController = controller - window?.makeKeyAndVisible() - UIView.animate(withDuration: 0.5) { - controller.view.alpha = 1 - } - } - } - } else { - NextcloudKit.shared.setup(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, password: self.appDelegate.password, urlBase: self.appDelegate.urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) - let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) - self.present(alertController, animated: true) - } + private func getAppPassword(urlBase: String, user: String, password: String) { + NextcloudKit.shared.getAppPassword(url: urlBase, user: user, password: password) { token, _, error in + if error == .success, let password = token { + self.createAccount(urlBase: urlBase, user: user, password: password) + } else { + NCContentPresenter().showError(error: error) + self.dismiss(animated: true, completion: nil) } - } else if error.errorCode == NSURLErrorServerCertificateUntrusted { - let alertController = UIAlertController(title: NSLocalizedString("_ssl_certificate_untrusted_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { _ in - if let host = URL(string: url)?.host { - NCNetworking.shared.writeCertificate(host: host) - } - })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { _ in })) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_certificate_details_", comment: ""), style: .default, handler: { _ in - if let navigationController = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil).instantiateInitialViewController() { - self.present(navigationController, animated: true) - } - })) - self.present(alertController, animated: true) - } else { - - let message = NSLocalizedString("_not_possible_connect_to_server_", comment: "") + ".\n" + error.errorDescription - let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: message, preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) - self.present(alertController, animated: true, completion: { }) } } - private func createAccount(server: String, username: String, password: String) { - appDelegate.createAccount(url: server, user: username, password: password) { error in + private func createAccount(urlBase: String, user: String, password: String) { + if let host = URL(string: urlBase)?.host { + NCNetworking.shared.writeCertificate(host: host) + } + self.appDelegate.createAccount(urlBase: urlBase, user: user, password: password) { error in if error == .success { let window = UIApplication.shared.firstWindow if window?.rootViewController is NCMainTabBarController { self.dismiss(animated: true) } else { - if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { - mainTabBarController.modalPresentationStyle = .fullScreen - mainTabBarController.view.alpha = 0 - window?.rootViewController = mainTabBarController + if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController { + controller.modalPresentationStyle = .fullScreen + controller.view.alpha = 0 + window?.rootViewController = controller window?.makeKeyAndVisible() UIView.animate(withDuration: 0.5) { - mainTabBarController.view.alpha = 1 + controller.view.alpha = 1 } } } - } - } - } - - private func getAppPassword(serverUrl: String, username: String, password: String) { - NextcloudKit.shared.getAppPassword(url: serverUrl, user: username, password: password) { token, _, error in - if error == .success, let password = token { - self.createAccount(server: serverUrl, username: username, password: password) } else { - NCContentPresenter().showError(error: error) - self.dismiss(animated: true, completion: nil) + let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in })) + self.present(alertController, animated: true) } } } + } extension NCLogin: NCShareAccountsDelegate { diff --git a/iOSClient/Login/NCLoginPoll.swift b/iOSClient/Login/NCLoginPoll.swift index 40646e8da8..d1d38d0d79 100644 --- a/iOSClient/Login/NCLoginPoll.swift +++ b/iOSClient/Login/NCLoginPoll.swift @@ -125,9 +125,9 @@ private class LoginManager: ObservableObject { func poll() { NextcloudKit.shared.getLoginFlowV2Poll(token: self.loginFlowV2Token, endpoint: self.loginFlowV2Endpoint) { server, loginName, appPassword, _, error in - if error == .success, let server, let loginName, let appPassword { + if error == .success, let urlBase = server, let user = loginName, let appPassword { self.isLoading = true - self.appDelegate.createAccount(url: server, user: loginName, password: appPassword) { error in + self.appDelegate.createAccount(urlBase: urlBase, user: user, password: appPassword) { error in if error == .success { self.pollFinished = true } diff --git a/iOSClient/Login/NCLoginProvider.swift b/iOSClient/Login/NCLoginProvider.swift index d55247fb4f..a7deec3ce2 100644 --- a/iOSClient/Login/NCLoginProvider.swift +++ b/iOSClient/Login/NCLoginProvider.swift @@ -171,7 +171,7 @@ extension NCLoginProvider: WKNavigationDelegate { if error == .success, let userProfile { NCManageDatabase.shared.deleteAccount(account) NCManageDatabase.shared.addAccount(account, urlBase: urlBase, user: user, userId: userProfile.userId, password: password) - self.appDelegate.changeAccount(account, userProfile: userProfile) + self.appDelegate.changeAccount(account, userProfile: userProfile) { } let window = UIApplication.shared.firstWindow if window?.rootViewController is NCMainTabBarController { self.dismiss(animated: true) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift index 0021aca407..b3c36da944 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift @@ -613,7 +613,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS let action = UIAction(title: name, image: image, state: account.active ? .on : .off) { _ in if !account.active { - self.appDelegate.changeAccount(account.account, userProfile: nil) + self.appDelegate.changeAccount(account.account, userProfile: nil) { } self.setEditMode(false) } } diff --git a/iOSClient/Networking/NCNetworkingCheckRemoteUser.swift b/iOSClient/Networking/NCNetworkingCheckRemoteUser.swift index 3cf2bce861..53dd5ea75f 100644 --- a/iOSClient/Networking/NCNetworkingCheckRemoteUser.swift +++ b/iOSClient/Networking/NCNetworkingCheckRemoteUser.swift @@ -27,19 +27,27 @@ import NextcloudKit class NCNetworkingCheckRemoteUser { func checkRemoteUser(account: String, error: NKError) { let token = NCKeychain().getPassword(account: account) - guard let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)), - !token.isEmpty, - let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } + guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, + let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)), + !token.isEmpty else { return } NCNetworking.shared.cancelAllTask() if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion17 { NextcloudKit.shared.getRemoteWipeStatus(serverUrl: tableAccount.urlBase, token: token) { account, wipe, _, error in if wipe { - appDelegate.deleteAccount(account, wipe: true) + appDelegate.deleteAccount(account) let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_wipe_account_") NCContentPresenter().messageNotification(tableAccount.user, error: error, delay: NCGlobal.shared.dismissAfterSecondLong, type: NCContentPresenter.messageType.error, priority: .max) NextcloudKit.shared.setRemoteWipeCompletition(serverUrl: tableAccount.urlBase, token: token) { _, _ in print("wipe") } + let accounts = NCManageDatabase.shared.getAccounts() + if accounts?.count ?? 0 > 0 { + if let newAccount = accounts?.first { + appDelegate.changeAccount(newAccount, userProfile: nil) { } + } else { + appDelegate.openLogin(selector: NCGlobal.shared.introLogin, openLoginWeb: false) + } + } } else { if UIApplication.shared.applicationState == .active && NextcloudKit.shared.isNetworkReachable() { let description = String.localizedStringWithFormat(NSLocalizedString("_error_check_remote_user_", comment: ""), tableAccount.user, tableAccount.urlBase) diff --git a/iOSClient/SceneDelegate.swift b/iOSClient/SceneDelegate.swift index 9f0b1cdaa9..f0845918ce 100644 --- a/iOSClient/SceneDelegate.swift +++ b/iOSClient/SceneDelegate.swift @@ -187,7 +187,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { for account in accounts { let urlBase = URL(string: account.urlBase) if url.contains(urlBase?.host ?? "") && userId == account.userId { - appDelegate.changeAccount(account.account, userProfile: nil) + appDelegate.changeAccount(account.account, userProfile: nil) { } return account } } @@ -387,7 +387,7 @@ extension SceneDelegate: NCAccountRequestDelegate { func accountRequestAddAccount() { } func accountRequestChangeAccount(account: String) { - appDelegate?.changeAccount(account, userProfile: nil) + appDelegate?.changeAccount(account, userProfile: nil) { } } } diff --git a/iOSClient/Settings/Advanced/NCSettingsAdvancedModel.swift b/iOSClient/Settings/Advanced/NCSettingsAdvancedModel.swift index 68a421819f..233cb1db94 100644 --- a/iOSClient/Settings/Advanced/NCSettingsAdvancedModel.swift +++ b/iOSClient/Settings/Advanced/NCSettingsAdvancedModel.swift @@ -162,10 +162,8 @@ class NCSettingsAdvancedModel: ObservableObject, ViewOnAppearHandling { /// /// - Parameter /// exit: Boolean indicating whether to reset the application. - func resetNextCloud(exit: Bool) { - if exit { - self.appDelegate.resetApplication() - } else { } + func resetNextCloud() { + self.appDelegate.resetApplication() } /// Exits the Nextcloud application if specified. diff --git a/iOSClient/Settings/Advanced/NCSettingsAdvancedView.swift b/iOSClient/Settings/Advanced/NCSettingsAdvancedView.swift index 1c60b3b92c..45fcf06880 100644 --- a/iOSClient/Settings/Advanced/NCSettingsAdvancedView.swift +++ b/iOSClient/Settings/Advanced/NCSettingsAdvancedView.swift @@ -263,7 +263,7 @@ struct NCSettingsAdvancedView: View { .tint(Color(UIColor.label)) .alert(NSLocalizedString("_want_exit_", comment: ""), isPresented: $showExitAlert) { Button(NSLocalizedString("_ok_", comment: ""), role: .destructive) { - model.resetNextCloud(exit: showExitAlert) + model.resetNextCloud() } Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel) { } } diff --git a/iOSClient/Settings/NCKeychain.swift b/iOSClient/Settings/NCKeychain.swift index 3abfdbaf3e..00167f9c13 100644 --- a/iOSClient/Settings/NCKeychain.swift +++ b/iOSClient/Settings/NCKeychain.swift @@ -396,7 +396,8 @@ import KeychainAccess @objc func getPassword(account: String) -> String { let key = "password" + account migrate(key: key) - return (try? keychain.get(key)) ?? "" + let password = (try? keychain.get(key)) ?? "" + return password } func setPassword(account: String, password: String?) {