NCNetworkingCheckRemoteUser.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // NCNetworkingCheckRemoteUser.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 15/05/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import NextcloudKit
  25. class NCNetworkingCheckRemoteUser {
  26. func checkRemoteUser(account: String, error: NKError) {
  27. let token = NCKeychain().getPassword(account: account)
  28. guard let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)),
  29. !token.isEmpty,
  30. let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  31. NCNetworking.shared.cancelDataTask()
  32. NCNetworking.shared.cancelDownloadTasks()
  33. NCNetworking.shared.cancelUploadTasks()
  34. NCNetworking.shared.cancelUploadBackgroundTask()
  35. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion17 {
  36. NextcloudKit.shared.getRemoteWipeStatus(serverUrl: tableAccount.urlBase, token: token) { account, wipe, _, error in
  37. if wipe {
  38. appDelegate.deleteAccount(account, wipe: true)
  39. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_wipe_account_")
  40. NCContentPresenter().messageNotification(tableAccount.user, error: error, delay: NCGlobal.shared.dismissAfterSecondLong, type: NCContentPresenter.messageType.error, priority: .max)
  41. NextcloudKit.shared.setRemoteWipeCompletition(serverUrl: tableAccount.urlBase, token: token) { _, _ in print("wipe") }
  42. } else {
  43. if UIApplication.shared.applicationState == .active && NextcloudKit.shared.isNetworkReachable() {
  44. let description = String.localizedStringWithFormat(NSLocalizedString("_error_check_remote_user_", comment: ""), tableAccount.user, tableAccount.urlBase)
  45. let error = NKError(errorCode: error.errorCode, errorDescription: description)
  46. NCContentPresenter().showError(error: error, priority: .max)
  47. NCKeychain().setPassword(account: account, password: nil)
  48. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Password removed.")
  49. }
  50. }
  51. }
  52. } else {
  53. if UIApplication.shared.applicationState == .active && NextcloudKit.shared.isNetworkReachable() {
  54. let description = String.localizedStringWithFormat(NSLocalizedString("_error_check_remote_user_", comment: ""), tableAccount.user, tableAccount.urlBase)
  55. let error = NKError(errorCode: error.errorCode, errorDescription: description)
  56. NCContentPresenter().showError(error: error, priority: .max)
  57. NCKeychain().setPassword(account: account, password: nil)
  58. }
  59. }
  60. }
  61. }