CCLoginWeb.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 CCLoginDelegateWeb: class {
  10. func loginSuccess(_: NSInteger)
  11. func loginDisappear()
  12. }
  13. public class CCLoginWeb: UIViewController {
  14. /*
  15. @objc enum enumLoginTypeWeb : NSInteger {
  16. case loginAdd = 0
  17. case loginAddForced = 1
  18. case loginModifyPasswordUser = 2
  19. }
  20. */
  21. @objc weak var delegate: CCLoginDelegateWeb?
  22. @objc var loginType = loginAdd
  23. var viewController : UIViewController?
  24. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  25. var doneButtonVisible: Bool = false
  26. @objc func presentModalWithDefaultTheme(_ vc: UIViewController) {
  27. self.viewController = vc
  28. if (loginType == loginAdd || loginType == loginModifyPasswordUser) {
  29. doneButtonVisible = true
  30. }
  31. let webVC = SwiftModalWebVC(urlString: NCBrandOptions.sharedInstance.loginBaseUrl, theme: .custom, color: NCBrandColor.sharedInstance.brand, colorText: NCBrandColor.sharedInstance.navigationBarText, doneButtonVisible: doneButtonVisible, hideToolbar: true)
  32. webVC.delegateWeb = self
  33. vc.present(webVC, animated: false, completion: nil)
  34. }
  35. }
  36. extension CCLoginWeb: SwiftModalWebVCDelegate {
  37. public func didStartLoading() {
  38. print("Started loading.")
  39. }
  40. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  41. let urlString: String = url.absoluteString.lowercased()
  42. if (urlString.contains(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true && (loginType == loginAdd || loginType == loginAddForced)) {
  43. let keyValue = url.path.components(separatedBy: "&")
  44. if (keyValue.count == 3) {
  45. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  46. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  47. if (serverUrl.last == "/") {
  48. serverUrl = String(serverUrl.dropLast())
  49. }
  50. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  51. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  52. let account : String = "\(username) \(serverUrl)"
  53. NCManageDatabase.sharedInstance.deleteAccount(account)
  54. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password)
  55. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  56. return
  57. }
  58. if (tableAccount.account == account) {
  59. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  60. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  61. self.viewController?.dismiss(animated: true, completion: nil)
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public func didFinishLoading(success: Bool, url: URL) {
  68. print("Finished loading. Success: \(success).")
  69. }
  70. public override func viewDidDisappear(_ animated: Bool) {
  71. super.viewDidDisappear(animated)
  72. self.delegate?.loginDisappear()
  73. }
  74. }