浏览代码

goback unencrypted DB, sorry

marinofaggiana 6 年之前
父节点
当前提交
8fd9b87c20
共有 2 个文件被更改,包括 15 次插入80 次删除
  1. 14 79
      iOSClient/Database/NCManageDatabase.swift
  2. 1 1
      iOSClient/Settings/HCEditProfile.swift

+ 14 - 79
iOSClient/Database/NCManageDatabase.swift

@@ -32,49 +32,14 @@ class NCManageDatabase: NSObject {
     
     
     override init() {
     override init() {
         
         
-        var realm = try! Realm()
-        
         let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
         let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
         let databaseFilePath = dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseDefault)")
         let databaseFilePath = dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseDefault)")
-        let databaseEncryptedFilePath = dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseEncryptedDefault)")
 
 
-        // Migrate unencrypted database to encrypted datadase
-        
-        if FileManager.default.fileExists(atPath: databaseFilePath!.path) {
-        
-            let configMigration = Realm.Configuration(
-                
-                fileURL: databaseFilePath,
-                schemaVersion: UInt64(k_databaseSchemaVersion)
-            )
-            
-            do {
-                try FileManager.default.removeItem(at: databaseEncryptedFilePath!)
-            } catch let error {
-                print("error: \(error)")
-            }
-
-            do {
-                realm = try Realm(configuration: configMigration)
-                try realm.writeCopy(toFile: databaseEncryptedFilePath!, encryptionKey: CCUtility.getDatabaseEncryptionKey())
-            } catch let error {
-                print("error: \(error)")
-            }
+        let configCompact = Realm.Configuration(
             
             
-            do {
-                try FileManager.default.removeItem(at: databaseFilePath!)
-            } catch let error {
-                print("error: \(error)")
-            }
-        }
-        
-        // Compact Database
-        
-        var configCompact = Realm.Configuration(
-            
-            fileURL: databaseEncryptedFilePath,
+            fileURL: databaseFilePath,
             schemaVersion: UInt64(k_databaseSchemaVersion),
             schemaVersion: UInt64(k_databaseSchemaVersion),
-            
+
             shouldCompactOnLaunch: { totalBytes, usedBytes in
             shouldCompactOnLaunch: { totalBytes, usedBytes in
             // totalBytes refers to the size of the file on disk in bytes (data + free space)
             // totalBytes refers to the size of the file on disk in bytes (data + free space)
             // usedBytes refers to the number of bytes used by data in the file
             // usedBytes refers to the number of bytes used by data in the file
@@ -84,24 +49,20 @@ class NCManageDatabase: NSObject {
             return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
             return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
         })
         })
         
         
-        // Encrypting the database file on disk with AES-256+SHA2 by supplying a 64-byte encryption key
-        configCompact.encryptionKey = CCUtility.getDatabaseEncryptionKey()
-    
         do {
         do {
-            realm = try Realm(configuration: configCompact)
-        } catch let error {
-            print("error: \(error)")
+            // Realm is compacted on the first open if the configuration block conditions were met.
+            _ = try Realm(configuration: configCompact)
+        } catch {
+            // handle error compacting or opening Realm
         }
         }
         
         
-        // Open default Database
-
-        var config = Realm.Configuration(
+        let config = Realm.Configuration(
         
         
-            fileURL: databaseEncryptedFilePath,
+            fileURL: dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseDefault)"),
             schemaVersion: UInt64(k_databaseSchemaVersion),
             schemaVersion: UInt64(k_databaseSchemaVersion),
             
             
             migrationBlock: { migration, oldSchemaVersion in
             migrationBlock: { migration, oldSchemaVersion in
-                
+                // We haven’t migrated anything yet, so oldSchemaVersion == 0
                 /*
                 /*
                 if (oldSchemaVersion < 37) {
                 if (oldSchemaVersion < 37) {
                     migration.enumerateObjects(ofType: tableMetadata.className()) { oldObject, newObject in
                     migration.enumerateObjects(ofType: tableMetadata.className()) { oldObject, newObject in
@@ -134,20 +95,9 @@ class NCManageDatabase: NSObject {
                     }
                     }
                 }
                 }
         })
         })
-
-        // Encrypting the database file on disk with AES-256+SHA2 by supplying a 64-byte encryption key
-        config.encryptionKey = CCUtility.getDatabaseEncryptionKey()
-
-        #if targetEnvironment(simulator)
-        print("[LOG] Database encryption key:\n" + CCUtility.hexRepresentation(config.encryptionKey, spaces: false))
-        #endif
         
         
         Realm.Configuration.defaultConfiguration = config
         Realm.Configuration.defaultConfiguration = config
-        do {
-            realm = try Realm()
-        } catch let error {
-            print("error: \(error)")
-        }
+        _ = try! Realm()
     }
     }
     
     
     //MARK: -
     //MARK: -
@@ -252,21 +202,6 @@ class NCManageDatabase: NSObject {
         }
         }
     }
     }
     
     
-    @objc func updateAccount(_ account: tableAccount) {
-        
-        let realm = try! Realm()
-        
-        do {
-            try realm.write {
-                if var result = realm.objects(tableAccount.self).filter("active = true").first {
-                    result = account
-                }
-            }
-        } catch let error {
-            print("[LOG] Could not write to database: ", error)
-        }
-    }
-    
     @objc func deleteAccount(_ account: String) {
     @objc func deleteAccount(_ account: String) {
         
         
         let realm = try! Realm()
         let realm = try! Realm()
@@ -296,7 +231,7 @@ class NCManageDatabase: NSObject {
             return nil
             return nil
         }
         }
         
         
-        return tableAccount.init(value: result)
+        return result
     }
     }
 
 
     @objc func getAccounts() -> [String]? {
     @objc func getAccounts() -> [String]? {
@@ -393,7 +328,7 @@ class NCManageDatabase: NSObject {
                     if result.account == account {
                     if result.account == account {
                     
                     
                         result.active = true
                         result.active = true
-                        activeAccount = tableAccount.init(value: result)
+                        activeAccount = result
                     
                     
                     } else {
                     } else {
                     
                     
@@ -532,7 +467,7 @@ class NCManageDatabase: NSObject {
             print("[LOG] Could not write to database: ", error)
             print("[LOG] Could not write to database: ", error)
         }
         }
         
         
-        return tableAccount.init(value: activeAccount)
+        return activeAccount
     }
     }
     
     
     @objc func setAccountHCFeatures(_ features: HCFeatures) -> tableAccount? {
     @objc func setAccountHCFeatures(_ features: HCFeatures) -> tableAccount? {

+ 1 - 1
iOSClient/Settings/HCEditProfile.swift

@@ -271,7 +271,7 @@ class HCEditProfile: XLFormViewController {
         
         
         OCNetworking.sharedManager()?.putHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, address: tableAccount?.address, businesssize: String(describing: tableAccount?.businessSize), businesstype: tableAccount?.businessType, city: tableAccount?.city, company: tableAccount?.company, country: tableAccount?.country, displayname: tableAccount?.displayName, email: tableAccount?.email, phone: tableAccount?.phone, role_: tableAccount?.role, twitter: tableAccount?.twitter, website: tableAccount?.webpage, zip: tableAccount?.zip, completion: { (account, message, errorCode) in
         OCNetworking.sharedManager()?.putHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, address: tableAccount?.address, businesssize: String(describing: tableAccount?.businessSize), businesstype: tableAccount?.businessType, city: tableAccount?.city, company: tableAccount?.company, country: tableAccount?.country, displayname: tableAccount?.displayName, email: tableAccount?.email, phone: tableAccount?.phone, role_: tableAccount?.role, twitter: tableAccount?.twitter, website: tableAccount?.webpage, zip: tableAccount?.zip, completion: { (account, message, errorCode) in
             if errorCode == 0 && account == self.appDelegate.activeAccount {
             if errorCode == 0 && account == self.appDelegate.activeAccount {
-                _ = NCManageDatabase.sharedInstance.updateAccount(self.tableAccount!)
+//                _ = NCManageDatabase.sharedInstance.updateAccount(self.tableAccount!)
                 self.navigationController?.popViewController(animated: true)
                 self.navigationController?.popViewController(animated: true)
                 self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
                 self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
             } else if errorCode != 0 {
             } else if errorCode != 0 {