Skip to content

Commit

Permalink
Account create (#2987)
Browse files Browse the repository at this point in the history
* coding

Signed-off-by: Marino Faggiana <[email protected]>

* cleaning

Signed-off-by: Marino Faggiana <[email protected]>

* coding

Signed-off-by: Marino Faggiana <[email protected]>

* cod

Signed-off-by: Marino Faggiana <[email protected]>

* cleaning

Signed-off-by: Marino Faggiana <[email protected]>

* cod

Signed-off-by: Marino Faggiana <[email protected]>

* cod

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jul 21, 2024
1 parent 3b198ea commit 610c1a0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 108 deletions.
2 changes: 0 additions & 2 deletions File Provider Extension/FileProviderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class fileProviderData: NSObject {
// NO DOMAIN -> Set default account
if domain == nil {
guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return nil }

account = activeAccount.account
user = activeAccount.user
userId = activeAccount.userId
Expand All @@ -99,7 +98,6 @@ class fileProviderData: NSObject {
guard let host = url.host else { continue }
let accountDomain = activeAccount.userId + " (" + host + ")"
if accountDomain == domain!.identifier.rawValue {

account = activeAccount.account
user = activeAccount.user
userId = activeAccount.userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class FilesIntegrationTests: BaseIntegrationXCTestCase {
let serverUrl = "\(TestConstants.server)/remote.php/dav/files/\(TestConstants.username)"
let serverUrlFileName = "\(serverUrl)/\(folderName)"

NextcloudKit.shared.setup(account: TestConstants.account, user: TestConstants.username, userId: TestConstants.username, password: appToken, urlBase: TestConstants.server)
NextcloudKit.shared.setup(account: TestConstants.account, user: TestConstants.username, userId: TestConstants.username, password: appToken, urlBase: TestConstants.server, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)

// Test creating folder
NCNetworking.shared.createFolder(fileName: folderName, serverUrl: serverUrl, account: TestConstants.account, urlBase: TestConstants.server, userId: TestConstants.username, withPush: true, sceneIdentifier: nil) { error in
Expand Down
25 changes: 18 additions & 7 deletions iOSClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

// MARK: - Account

func createAccount(server: String, username: String, password: String, completion: @escaping (_ error: NKError) -> Void) {
var urlBase = server
if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) }
let account: String = "\(username) \(urlBase)"
let user = username
func createAccount(url: String,
user: String,
password: String,
completion: @escaping (_ error: NKError) -> Void) {
var urlBase = url
if urlBase.last == "/" {
urlBase = String(urlBase.dropLast())
}
let oldAccount: String = "\(user) \(urlBase)"
var account: String = "\(user)@\(urlBase)"
if let accounts = NCManageDatabase.shared.getAccounts(),
accounts.contains(oldAccount) {
account = oldAccount
}

NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
NextcloudKit.shared.getUserProfile { _, userProfile, _, error in
Expand All @@ -487,13 +496,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

func changeAccount(_ account: String, userProfile: NKUserProfile?) {
guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else {
return
}

NCNetworking.shared.cancelAllQueue()
NCNetworking.shared.cancelDataTask()
NCNetworking.shared.cancelDownloadTasks()
NCNetworking.shared.cancelUploadTasks()

guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else { return }

if account != self.account {
DispatchQueue.global().async {
if NCManageDatabase.shared.getAccounts()?.count == 1 {
Expand Down
73 changes: 29 additions & 44 deletions iOSClient/Login/NCLogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import SwiftEntryKit
import SwiftUI

class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {

@IBOutlet weak var imageBrand: UIImageView!
@IBOutlet weak var imageBrandConstraintY: NSLayoutConstraint!
@IBOutlet weak var baseUrl: UITextField!
Expand Down Expand Up @@ -217,15 +216,13 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {
}

// AppConfig
if let serverUrl = configServerUrl {
if let username = self.configUsername, let password = configAppPassword {
createAccount(server: serverUrl, username: username, password: password)
return
} else if let username = self.configUsername, let password = configPassword {
getAppPassword(serverUrl: serverUrl, username: username, password: password)
return
if let url = configServerUrl {
if let user = self.configUsername, let password = configAppPassword {
appDelegate.createAccount(url: url, user: user, password: password) { _ in }
} else if let user = self.configUsername, let password = configPassword {
getAppPassword(url: url, user: user, password: password)
} else {
urlBase = serverUrl
urlBase = url
}
}
}
Expand Down Expand Up @@ -410,9 +407,7 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {
let password = valueArray[1].replacingOccurrences(of: "password:", with: "")
let urlBase = valueArray[2].replacingOccurrences(of: "server:", with: "")
let serverUrl = urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav

loginButton.isEnabled = false

NextcloudKit.shared.checkServer(serverUrl: serverUrl) { error in
self.loginButton.isEnabled = true
self.standardLogin(url: urlBase, user: user, password: password, error: error)
Expand All @@ -426,17 +421,8 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {
if let host = URL(string: url)?.host {
NCNetworking.shared.writeCertificate(host: host)
}
let urlBase = url
let account = user + " " + user

NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
NextcloudKit.shared.getUserProfile { _, userProfile, _, error in
if error == .success, let userProfile {
NCManageDatabase.shared.deleteAccount(account)
NCManageDatabase.shared.addAccount(account, urlBase: url, user: user, userId: userProfile.userId, password: password)

self.appDelegate.changeAccount(account, userProfile: userProfile)

appDelegate.createAccount(url: url, user: user, password: password) { error in
if error == .success {
let window = UIApplication.shared.firstWindow
if window?.rootViewController is NCMainTabBarController {
self.dismiss(animated: true)
Expand Down Expand Up @@ -483,31 +469,30 @@ class NCLogin: UIViewController, UITextFieldDelegate, NCLoginQRCodeDelegate {
}
}

private func createAccount(server: String, username: String, password: String) {
appDelegate.createAccount(server: server, username: username, password: password) { error in
if error == .success {
let window = UIApplication.shared.firstWindow
if window?.rootViewController is NCMainTabBarController {
self.dismiss(animated: true)
} else {
if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
mainTabBarController.modalPresentationStyle = .fullScreen
mainTabBarController.view.alpha = 0
window?.rootViewController = mainTabBarController
window?.makeKeyAndVisible()
UIView.animate(withDuration: 0.5) {
mainTabBarController.view.alpha = 1
private func getAppPassword(url: String, user: String, password: String) {
NextcloudKit.shared.getAppPassword(url: url, user: user, password: password) { token, _, error in
if error == .success, let password = token {
self.appDelegate.createAccount(url: url, user: user, password: password) { error in
if error == .success {
let window = UIApplication.shared.firstWindow
if window?.rootViewController is NCMainTabBarController {
self.dismiss(animated: true)
} else {
if let mainTabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
mainTabBarController.modalPresentationStyle = .fullScreen
mainTabBarController.view.alpha = 0
window?.rootViewController = mainTabBarController
window?.makeKeyAndVisible()
UIView.animate(withDuration: 0.5) {
mainTabBarController.view.alpha = 1
}
}
}
} else {
NCContentPresenter().showError(error: error)
self.dismiss(animated: true, completion: nil)
}
}
}
}
}

private func getAppPassword(serverUrl: String, username: String, password: String) {
NextcloudKit.shared.getAppPassword(serverUrl: serverUrl, username: username, password: password) { token, _, error in
if error == .success, let password = token {
self.createAccount(server: serverUrl, username: username, password: password)
} else {
NCContentPresenter().showError(error: error)
self.dismiss(animated: true, completion: nil)
Expand Down
14 changes: 5 additions & 9 deletions iOSClient/Login/NCLoginPoll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,16 @@ private class LoginManager: ObservableObject {
NextcloudKit.shared.getLoginFlowV2Poll(token: self.loginFlowV2Token, endpoint: self.loginFlowV2Endpoint) { server, loginName, appPassword, _, error in
if error == .success, let server, let loginName, let appPassword {
self.isLoading = true
self.createAccount(server: server, username: loginName, password: appPassword)
self.appDelegate.createAccount(url: server, user: loginName, password: appPassword) { error in
if error == .success {
self.pollFinished = true
}
}
}
}
}

func openLoginInBrowser() {
UIApplication.shared.open(URL(string: loginFlowV2Login)!)
}

private func createAccount(server: String, username: String, password: String) {
appDelegate.createAccount(server: server, username: username, password: password) { error in
if error == .success {
self.pollFinished = true
}
}
}
}
66 changes: 24 additions & 42 deletions iOSClient/Login/NCLoginProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,32 @@ extension NCLoginProvider: WKNavigationDelegate {
}

if !server.isEmpty, !user.isEmpty, !password.isEmpty {

let server: String = server.replacingOccurrences(of: "/server:", with: "")
let username: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ")
let url: String = server.replacingOccurrences(of: "/server:", with: "")
let user: String = user.replacingOccurrences(of: "user:", with: "").replacingOccurrences(of: "+", with: " ")
let password: String = password.replacingOccurrences(of: "password:", with: "")

createAccount(server: server, username: username, password: password)
appDelegate.createAccount(url: url, user: user, password: password) { error in
if error == .success {
let window = UIApplication.shared.firstWindow
if window?.rootViewController is NCMainTabBarController {
self.dismiss(animated: true)
} else {
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
controller.modalPresentationStyle = .fullScreen
controller.view.alpha = 0
window?.rootViewController = controller
window?.makeKeyAndVisible()
UIView.animate(withDuration: 0.5) {
controller.view.alpha = 1
}
}
}
} else {
let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
self.present(alertController, animated: true)
}
}
}
}
}
Expand All @@ -159,42 +179,4 @@ extension NCLoginProvider: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
NCActivityIndicator.shared.stop()
}

// MARK: -

func createAccount(server: String, username: String, password: String) {
var urlBase = server
if urlBase.last == "/" { urlBase = String(urlBase.dropLast()) }
let account: String = "\(username) \(urlBase)"
let user = username

NextcloudKit.shared.setup(account: account, user: user, userId: user, password: password, urlBase: urlBase, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
NextcloudKit.shared.getUserProfile { _, userProfile, _, error in
if error == .success, let userProfile {
NCManageDatabase.shared.deleteAccount(account)
NCManageDatabase.shared.addAccount(account, urlBase: urlBase, user: user, userId: userProfile.userId, password: password)

self.appDelegate.changeAccount(account, userProfile: userProfile)

let window = UIApplication.shared.firstWindow
if window?.rootViewController is NCMainTabBarController {
self.dismiss(animated: true)
} else {
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
controller.modalPresentationStyle = .fullScreen
controller.view.alpha = 0
window?.rootViewController = controller
window?.makeKeyAndVisible()
UIView.animate(withDuration: 0.5) {
controller.view.alpha = 1
}
}
}
} else {
let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: error.errorDescription, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
self.present(alertController, animated: true)
}
}
}
}
3 changes: 0 additions & 3 deletions iOSClient/Login/NCLoginQRCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate {
readerVC.completionBlock = { (_: QRCodeReaderResult?) in
self.readerVC.dismiss(animated: true, completion: nil)
}

delegate?.present(readerVC, animated: true, completion: nil)
}

Expand All @@ -72,7 +71,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate {
switch error.code {
case -11852:
alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_authorized_", comment: ""), preferredStyle: .alert)

alert.addAction(UIAlertAction(title: NSLocalizedString("_settings_", comment: ""), style: .default, handler: { _ in
DispatchQueue.main.async {
if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
Expand All @@ -88,7 +86,6 @@ class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate {
}

delegate?.present(alert, animated: true, completion: nil)

return false
}
}
Expand Down

0 comments on commit 610c1a0

Please sign in to comment.