CCLoginWeb.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  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: CCLoginDelegateWeb?
  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, hideToolbar: true)
  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.lowercased()
  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. NCManageDatabase.sharedInstance.deleteAccount(account)
  51. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password)
  52. let tableAccount : tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account)
  53. if (tableAccount.account == account) {
  54. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activePassword: password)
  55. self.delegate?.loginSuccess(NSInteger(loginType.rawValue))
  56. self.viewController?.dismiss(animated: true, completion: nil)
  57. }
  58. }
  59. }
  60. }
  61. }
  62. public func didFinishLoading(success: Bool, url: URL) {
  63. print("Finished loading. Success: \(success).")
  64. }
  65. }