diff --git a/File Provider Extension/FileProviderData.swift b/File Provider Extension/FileProviderData.swift index a05552d340..bc271a4649 100644 --- a/File Provider Extension/FileProviderData.swift +++ b/File Provider Extension/FileProviderData.swift @@ -76,7 +76,6 @@ class fileProviderData: NSObject { // NO DOMAIN -> Set default account if domain == nil { guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return nil } - account = activeAccount.account user = activeAccount.user userId = activeAccount.userId @@ -99,7 +98,6 @@ class fileProviderData: NSObject { guard let host = url.host else { continue } let accountDomain = activeAccount.userId + " (" + host + ")" if accountDomain == domain!.identifier.rawValue { - account = activeAccount.account user = activeAccount.user userId = activeAccount.userId diff --git a/Tests/NextcloudIntegrationTests/FilesIntegrationTests.swift b/Tests/NextcloudIntegrationTests/FilesIntegrationTests.swift index cb049761f4..f538479081 100644 --- a/Tests/NextcloudIntegrationTests/FilesIntegrationTests.swift +++ b/Tests/NextcloudIntegrationTests/FilesIntegrationTests.swift @@ -37,7 +37,7 @@ final class FilesIntegrationTests: BaseIntegrationXCTestCase { let serverUrl = "\(TestConstants.server)/remote.php/dav/files/\(TestConstants.username)" let serverUrlFileName = "\(serverUrl)/\(folderName)" - NextcloudKit.shared.setup(account: TestConstants.account, user: TestConstants.username, userId: TestConstants.username, password: appToken, urlBase: TestConstants.server) + NextcloudKit.shared.setup(account: TestConstants.account, user: TestConstants.username, userId: TestConstants.username, password: appToken, urlBase: TestConstants.server, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) // Test creating folder NCNetworking.shared.createFolder(fileName: folderName, serverUrl: serverUrl, account: TestConstants.account, urlBase: TestConstants.server, userId: TestConstants.username, withPush: true, sceneIdentifier: nil) { error in diff --git a/iOSClient/AppDelegate.swift b/iOSClient/AppDelegate.swift index 73a2c99a90..73e4333534 100644 --- a/iOSClient/AppDelegate.swift +++ b/iOSClient/AppDelegate.swift @@ -464,11 +464,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // MARK: - Account - func createAccount(server: String, username: String, password: String, completion: @escaping (_ error: NKError) -> Void) { - var urlBase = server - if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) } - let account: String = "\(username) \(urlBase)" - let user = username + func createAccount(url: 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 + } NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) NextcloudKit.shared.getUserProfile { _, userProfile, _, error in @@ -487,13 +496,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD } func changeAccount(_ account: String, userProfile: NKUserProfile?) { + guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else { + return + } + NCNetworking.shared.cancelAllQueue() NCNetworking.shared.cancelDataTask() NCNetworking.shared.cancelDownloadTasks() NCNetworking.shared.cancelUploadTasks() - guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else { return } - if account != self.account { DispatchQueue.global().async { if NCManageDatabase.shared.getAccounts()?.count == 1 { diff --git a/iOSClient/Login/NCLogin.swift b/iOSClient/Login/NCLogin.swift index 63afdd092f..b2fced42f3 100644 --- a/iOSClient/Login/NCLogin.swift +++ b/iOSClient/Login/NCLogin.swift @@ -28,7 +28,6 @@ import SwiftEntryKit import SwiftUI class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { - @IBOutlet weak var imageBrand: UIImageView! @IBOutlet weak var imageBrandConstraintY: NSLayoutConstraint! @IBOutlet weak var baseUrl: UITextField! @@ -217,15 +216,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 { + appDelegate.createAccount(url: url, user: user, password: password) { _ in } + } else if let user = self.configUsername, let password = configPassword { + getAppPassword(url: url, user: user, password: password) } else { - urlBase = serverUrl + urlBase = url } } } @@ -410,9 +407,7 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { let password = valueArray[1].replacingOccurrences(of: "password:", with: "") let urlBase = valueArray[2].replacingOccurrences(of: "server:", with: "") let serverUrl = urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav - loginButton.isEnabled = false - NextcloudKit.shared.checkServer(serverUrl: serverUrl) { error in self.loginButton.isEnabled = true self.standardLogin(url: urlBase, user: user, password: password, error: error) @@ -426,17 +421,8 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { 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, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) - 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) - + appDelegate.createAccount(url: url, user: user, password: password) { error in + if error == .success { let window = UIApplication.shared.firstWindow if window?.rootViewController is NCMainTabBarController { self.dismiss(animated: true) @@ -483,31 +469,30 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate { } } - private func createAccount(server: String, username: String, password: String) { - appDelegate.createAccount(server: server, username: username, 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 - window?.makeKeyAndVisible() - UIView.animate(withDuration: 0.5) { - mainTabBarController.view.alpha = 1 + private func getAppPassword(url: String, user: String, password: String) { + NextcloudKit.shared.getAppPassword(url: url, user: user, password: password) { token, _, error in + if error == .success, let password = token { + self.appDelegate.createAccount(url: url, 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 + window?.makeKeyAndVisible() + UIView.animate(withDuration: 0.5) { + mainTabBarController.view.alpha = 1 + } + } } + } else { + NCContentPresenter().showError(error: error) + self.dismiss(animated: true, completion: nil) } } - } - } - } - - private func getAppPassword(serverUrl: String, username: String, password: String) { - NextcloudKit.shared.getAppPassword(serverUrl: serverUrl, username: 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) diff --git a/iOSClient/Login/NCLoginPoll.swift b/iOSClient/Login/NCLoginPoll.swift index c7a7a4edaa..40646e8da8 100644 --- a/iOSClient/Login/NCLoginPoll.swift +++ b/iOSClient/Login/NCLoginPoll.swift @@ -127,7 +127,11 @@ private class LoginManager: ObservableObject { NextcloudKit.shared.getLoginFlowV2Poll(token: self.loginFlowV2Token, endpoint: self.loginFlowV2Endpoint) { server, loginName, appPassword, _, error in if error == .success, let server, let loginName, let appPassword { self.isLoading = true - self.createAccount(server: server, username: loginName, password: appPassword) + self.appDelegate.createAccount(url: server, user: loginName, password: appPassword) { error in + if error == .success { + self.pollFinished = true + } + } } } } @@ -135,12 +139,4 @@ private class LoginManager: ObservableObject { func openLoginInBrowser() { UIApplication.shared.open(URL(string: loginFlowV2Login)!) } - - private func createAccount(server: String, username: String, password: String) { - appDelegate.createAccount(server: server, username: username, password: password) { error in - if error == .success { - self.pollFinished = true - } - } - } } diff --git a/iOSClient/Login/NCLoginProvider.swift b/iOSClient/Login/NCLoginProvider.swift index 1714074e40..3d7eb65790 100644 --- a/iOSClient/Login/NCLoginProvider.swift +++ b/iOSClient/Login/NCLoginProvider.swift @@ -128,12 +128,32 @@ extension NCLoginProvider: WKNavigationDelegate { } if !server.isEmpty, !user.isEmpty, !password.isEmpty { - - let server: String = server.replacingOccurrences(of: "/server:", with: "") - let username: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ") + let url: String = server.replacingOccurrences(of: "/server:", with: "") + let user: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ") let password: String = password.replacingOccurrences(of: "password:", with: "") - createAccount(server: server, username: username, password: password) + appDelegate.createAccount(url: url, 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 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 { + 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) + } + } } } } @@ -159,42 +179,4 @@ extension NCLoginProvider: WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { NCActivityIndicator.shared.stop() } - - // MARK: - - - func createAccount(server: String, username: String, password: String) { - var urlBase = server - if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) } - let account: String = "\(username) \(urlBase)" - let user = username - - NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup) - NextcloudKit.shared.getUserProfile { _, userProfile, _, error in - 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) - - 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 { - 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) - } - } - } } diff --git a/iOSClient/Login/NCLoginQRCode.swift b/iOSClient/Login/NCLoginQRCode.swift index 1c80918c7f..6ef2805d9b 100644 --- a/iOSClient/Login/NCLoginQRCode.swift +++ b/iOSClient/Login/NCLoginQRCode.swift @@ -59,7 +59,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate { readerVC.completionBlock = { (_: QRCodeReaderResult?) in self.readerVC.dismiss(animated: true, completion: nil) } - delegate?.present(readerVC, animated: true, completion: nil) } @@ -72,7 +71,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate { switch error.code { case -11852: alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_authorized_", comment: ""), preferredStyle: .alert) - alert.addAction(UIAlertAction(title: NSLocalizedString("_settings_", comment: ""), style: .default, handler: { _ in DispatchQueue.main.async { if let settingsURL = URL(string: UIApplication.openSettingsURLString) { @@ -88,7 +86,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate { } delegate?.present(alert, animated: true, completion: nil) - return false } }