Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Jul 23, 2024
1 parent 9577885 commit 9ecc768
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 136 deletions.
10 changes: 6 additions & 4 deletions iOSClient/Account Settings/NCAccountSettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
54 changes: 22 additions & 32 deletions iOSClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -464,41 +465,38 @@ 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
if error == .success, let userProfile {
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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
})
}

Expand Down
4 changes: 0 additions & 4 deletions iOSClient/Data/NCManageDatabase+Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion iOSClient/Files/NCFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 34 additions & 79 deletions iOSClient/Login/NCLogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions iOSClient/Login/NCLoginPoll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion iOSClient/Login/NCLoginProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Loading

0 comments on commit 9ecc768

Please sign in to comment.