CCLoginWeb.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // CCLoginWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. import UIKit
  9. @objc protocol CCLoginDelegate: class {
  10. func loginSuccess(_: NSInteger)
  11. }
  12. public class CCLoginWeb: UIViewController {
  13. enum enumLoginType : NSInteger {
  14. case loginAdd = 0
  15. case loginAddForced = 1
  16. case loginModifyPasswordUser = 2
  17. }
  18. weak var delegate: CCLoginDelegate?
  19. var viewController : UIViewController?
  20. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  21. var loginType = loginAdd
  22. var doneButtonVisible: Bool = false
  23. func presentModalWithDefaultTheme(_ vc: UIViewController) {
  24. self.viewController = vc
  25. if (loginType == loginAdd || loginType == loginModifyPasswordUser) {
  26. doneButtonVisible = true
  27. }
  28. let webVC = SwiftModalWebVC(urlString: NCBrandOptions.sharedInstance.loginBaseUrl, theme: .custom, color: NCBrandColor.sharedInstance.brand, colorText: NCBrandColor.sharedInstance.navigationBarText, doneButtonVisible: doneButtonVisible)
  29. webVC.delegateWeb = self
  30. vc.present(webVC, animated: false, completion: nil)
  31. }
  32. }
  33. extension CCLoginWeb: SwiftModalWebVCDelegate {
  34. public func didStartLoading() {
  35. print("Started loading.")
  36. }
  37. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  38. let urlString: String = url.absoluteString
  39. if (urlString.contains(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true && (loginType == loginAdd || loginType == loginAddForced)) {
  40. let keyValue = url.path.components(separatedBy: "&")
  41. if (keyValue.count == 3) {
  42. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  43. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  44. if (serverUrl.characters.last == "/") {
  45. serverUrl = String(serverUrl.characters.dropLast())
  46. }
  47. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  48. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  49. let account : String = "\(username) \(serverUrl)"
  50. //CCCoreData.deleteAccount(account)
  51. //CCCoreData.addAccount(account, url: serverUrl, user: username, password: password)
  52. NCManageDatabase.sharedInstance.deleteAccount(account)
  53. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password)
  54. //let tableAccount : TableAccount = CCCoreData.setActiveAccount(account)
  55. let tableAccount : tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account)
  56. if (tableAccount.account == account) {
  57. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activePassword: password)
  58. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  59. self.viewController?.dismiss(animated: true, completion: nil)
  60. }
  61. }
  62. }
  63. }
  64. }
  65. public func didFinishLoading(success: Bool, url: URL) {
  66. print("Finished loading. Success: \(success).")
  67. }
  68. }