CCLoginWeb.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.customer, colorText: NCBrandColor.sharedInstance.customerText, 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.hasPrefix(NCBrandOptions.sharedInstance.webLoginAutenticationProtocol) == true && urlString.contains("login") == true) {
  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 NC 12
  49. if (NCBrandOptions.sharedInstance.use_login_web_personalized == false && serverUrl.hasPrefix("http://") == false && serverUrl.hasPrefix("https://") == false) {
  50. serverUrl = urlBase.replacingOccurrences(of: flowEndpoint, with: "")
  51. }
  52. if (serverUrl.last == "/") {
  53. serverUrl = String(serverUrl.dropLast())
  54. }
  55. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  56. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  57. let account : String = "\(username) \(serverUrl)"
  58. // Login Flow
  59. if (loginType == loginModifyPasswordUser && NCBrandOptions.sharedInstance.use_login_web_personalized == false) {
  60. // Verify if change the active account
  61. guard let activeAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  62. self.viewController?.dismiss(animated: true, completion: nil)
  63. return
  64. }
  65. if (activeAccount.account != account) {
  66. self.viewController?.dismiss(animated: true, completion: nil)
  67. return
  68. }
  69. // Change Password
  70. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountPassword(account, password: password) else {
  71. self.viewController?.dismiss(animated: true, completion: nil)
  72. return
  73. }
  74. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  75. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  76. self.viewController?.dismiss(animated: true, completion: nil)
  77. }
  78. if (loginType == loginAdd || loginType == loginAddForced) {
  79. // Add new account
  80. NCManageDatabase.sharedInstance.deleteAccount(account)
  81. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password, loginFlow: true)
  82. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  83. self.viewController?.dismiss(animated: true, completion: nil)
  84. return
  85. }
  86. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  87. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  88. self.viewController?.dismiss(animated: true, completion: nil)
  89. }
  90. }
  91. }
  92. }
  93. }
  94. public func didFinishLoading(success: Bool, url: URL) {
  95. print("Finished loading. Success: \(success).")
  96. }
  97. public func loginDisappear() {
  98. self.delegate?.loginDisappear()
  99. }
  100. }