NCNetworkingCheckRemoteUser.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. import UIKit
  23. import NextcloudKit
  24. @objc class NCNetworkingCheckRemoteUser: NSObject {
  25. @objc public static let shared: NCNetworkingCheckRemoteUser = {
  26. let instance = NCNetworkingCheckRemoteUser()
  27. return instance
  28. }()
  29. var checkRemoteUserInProgress = false
  30. @objc func checkRemoteUser(account: String, error: NKError) {
  31. if self.checkRemoteUserInProgress {
  32. return
  33. } else {
  34. self.checkRemoteUserInProgress = true
  35. }
  36. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  37. guard let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) else {
  38. self.checkRemoteUserInProgress = false
  39. return
  40. }
  41. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion17 {
  42. if error.errorCode == NCGlobal.shared.errorUnauthorized {
  43. let token = CCUtility.getPassword(account)!
  44. if token == "" {
  45. self.checkRemoteUserInProgress = false
  46. return
  47. }
  48. NextcloudKit.shared.getRemoteWipeStatus(serverUrl: tableAccount.urlBase, token: token) { account, wipe, error in
  49. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  50. if wipe {
  51. appDelegate.deleteAccount(account, wipe: true)
  52. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_wipe_account_")
  53. NCContentPresenter.shared.messageNotification(tableAccount.user, error: error, delay: NCGlobal.shared.dismissAfterSecondLong, type: NCContentPresenter.messageType.error, priority: .max)
  54. NextcloudKit.shared.setRemoteWipeCompletition(serverUrl: tableAccount.urlBase, token: token) { _, _ in print("wipe") }
  55. } else {
  56. if UIApplication.shared.applicationState == .active && NextcloudKit.shared.isNetworkReachable() && !CCUtility.getPassword(account).isEmpty && !appDelegate.deletePasswordSession {
  57. let description = String.localizedStringWithFormat(NSLocalizedString("_error_check_remote_user_", comment: ""), tableAccount.user, tableAccount.urlBase)
  58. let error = NKError(errorCode: error.errorCode, errorDescription: description)
  59. NCContentPresenter.shared.showError(error: error, priority: .max)
  60. CCUtility.setPassword(account, password: nil)
  61. appDelegate.deletePasswordSession = true
  62. }
  63. }
  64. self.checkRemoteUserInProgress = false
  65. }
  66. } else {
  67. NCContentPresenter.shared.showError(error: error, priority: .max)
  68. self.checkRemoteUserInProgress = false
  69. }
  70. } else if CCUtility.getPassword(account) != "" {
  71. if UIApplication.shared.applicationState == .active && NextcloudKit.shared.isNetworkReachable() {
  72. let description = String.localizedStringWithFormat(NSLocalizedString("_error_check_remote_user_", comment: ""), tableAccount.user, tableAccount.urlBase)
  73. let error = NKError(errorCode: error.errorCode, errorDescription: description)
  74. NCContentPresenter.shared.showError(error: error, priority: .max)
  75. CCUtility.setPassword(account, password: nil)
  76. }
  77. self.checkRemoteUserInProgress = false
  78. }
  79. }
  80. }