CCLoginWeb.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. @objc var urlBase = NCBrandOptions.sharedInstance.loginBaseUrl
  24. var viewController : UIViewController?
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var doneButtonVisible: Bool = false
  27. @objc func presentModalWithDefaultTheme(_ vc: UIViewController) {
  28. self.viewController = vc
  29. if (loginType == loginAdd || loginType == loginModifyPasswordUser) {
  30. doneButtonVisible = true
  31. }
  32. let webVC = SwiftModalWebVC(urlString: urlBase, theme: .custom, color: NCBrandColor.sharedInstance.brand, colorText: NCBrandColor.sharedInstance.brandText, doneButtonVisible: doneButtonVisible, hideToolbar: true)
  33. webVC.delegateWeb = self
  34. vc.present(webVC, animated: false, completion: nil)
  35. }
  36. }
  37. extension CCLoginWeb: SwiftModalWebVCDelegate {
  38. public func didStartLoading() {
  39. print("Started loading.")
  40. }
  41. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  42. let urlString: String = url.absoluteString.lowercased()
  43. if (urlString.contains(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true && (loginType == loginAdd || loginType == loginAddForced)) {
  44. let keyValue = url.path.components(separatedBy: "&")
  45. if (keyValue.count == 3) {
  46. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  47. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  48. // Login Flow
  49. if NCBrandOptions.sharedInstance.use_login_web_flow == true {
  50. if (self.urlBase.hasPrefix("http://")) {
  51. serverUrl = "http://" + serverUrl;
  52. } else if (self.urlBase.hasPrefix("https://")) {
  53. serverUrl = "https://" + serverUrl;
  54. }
  55. }
  56. if (serverUrl.last == "/") {
  57. serverUrl = String(serverUrl.dropLast())
  58. }
  59. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  60. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  61. let account : String = "\(username) \(serverUrl)"
  62. NCManageDatabase.sharedInstance.deleteAccount(account)
  63. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password, loginFlow: true)
  64. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  65. return
  66. }
  67. if (tableAccount.account == account) {
  68. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  69. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  70. self.viewController?.dismiss(animated: true, completion: nil)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public func didFinishLoading(success: Bool, url: URL) {
  77. print("Finished loading. Success: \(success).")
  78. }
  79. public func loginDisappear() {
  80. self.delegate?.loginDisappear()
  81. }
  82. }