From e2a2774664f14ce0db01485c37480ab1ebf026a2 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Tue, 7 Feb 2017 16:45:14 +0800 Subject: [PATCH] add erase data function --- pass.xcodeproj/project.pbxproj | 4 ++ .../AdvancedSettingsTableViewController.swift | 33 +++++++++ pass/Base.lproj/Main.storyboard | 68 +++++++++++++++++++ pass/PasswordStore.swift | 67 ++++++++++++++---- pass/PasswordsViewController.swift | 9 +++ pass/SettingsTableViewController.swift | 4 ++ 6 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 pass/AdvancedSettingsTableViewController.swift diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index 244edb3..75e0b00 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 94BA784B85E071D25EE89B59 /* libPods-pass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */; }; DC1208581E35EBE60042942E /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC1208571E35EBE60042942E /* ObjectiveGit.framework */; }; + DC193FFA1E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC193FF91E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift */; }; DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914941E434301007FF592 /* LabelTableViewCell.swift */; }; DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */; }; DC5734AE1E439AD400D09270 /* PasswordsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5734AD1E439AD400D09270 /* PasswordsViewController.swift */; }; @@ -39,6 +40,7 @@ ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-pass.a"; sourceTree = BUILT_PRODUCTS_DIR; }; AEAD6B31EAF5D061447A68CC /* Pods-pass.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.release.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.release.xcconfig"; sourceTree = ""; }; DC1208571E35EBE60042942E /* ObjectiveGit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjectiveGit.framework; path = Carthage/Build/iOS/ObjectiveGit.framework; sourceTree = ""; }; + DC193FF91E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdvancedSettingsTableViewController.swift; sourceTree = ""; }; DC4914941E434301007FF592 /* LabelTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelTableViewCell.swift; sourceTree = ""; }; DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailTableViewController.swift; sourceTree = ""; }; DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Objective-CBridgingHeader.h"; sourceTree = ""; }; @@ -113,6 +115,7 @@ DC917BD51E2E8231000FDF54 /* pass */ = { isa = PBXGroup; children = ( + DC193FF91E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift */, DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */, DC917BE21E2E8231000FDF54 /* Info.plist */, DC917BD61E2E8231000FDF54 /* AppDelegate.swift */, @@ -297,6 +300,7 @@ files = ( DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */, DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */, + DC193FFA1E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift in Sources */, DCA0499C1E3362F400522E8F /* PGPKeySettingTableViewController.swift in Sources */, DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */, DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */, diff --git a/pass/AdvancedSettingsTableViewController.swift b/pass/AdvancedSettingsTableViewController.swift new file mode 100644 index 0000000..a937d05 --- /dev/null +++ b/pass/AdvancedSettingsTableViewController.swift @@ -0,0 +1,33 @@ +// +// AdvancedSettingsTableViewController.swift +// pass +// +// Created by Mingshen Sun on 7/2/2017. +// Copyright © 2017 Bob Sun. All rights reserved. +// + +import UIKit + +class AdvancedSettingsTableViewController: UITableViewController { + + @IBOutlet weak var eraseDataTableViewCell: UITableViewCell! + override func viewDidLoad() { + super.viewDidLoad() + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if tableView.cellForRow(at: indexPath) == eraseDataTableViewCell { + print("erase data") + let alert = UIAlertController(title: "Erase Password Store Data?", message: "This will delete all local data and settings. Password store data on your remote server will not be affected.", preferredStyle: UIAlertControllerStyle.alert) + alert.addAction(UIAlertAction(title: "Erase Password Data", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in + PasswordStore.shared.erase() + NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreErased"))) + self.navigationController!.popViewController(animated: true) + })) + alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:nil)) + self.present(alert, animated: true, completion: nil) + tableView.deselectRow(at: indexPath, animated: true) + } + } + +} diff --git a/pass/Base.lproj/Main.storyboard b/pass/Base.lproj/Main.storyboard index b0ce8de..37bfb84 100644 --- a/pass/Base.lproj/Main.storyboard +++ b/pass/Base.lproj/Main.storyboard @@ -148,6 +148,30 @@ + + + + + + + + + + + + + + + + + + @@ -730,5 +754,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pass/PasswordStore.swift b/pass/PasswordStore.swift index 5c74347..30edc2a 100644 --- a/pass/PasswordStore.swift +++ b/pass/PasswordStore.swift @@ -123,18 +123,9 @@ class PasswordStore { } func updatePasswordEntityCoreData() { - let passwordEntityFetchRequest = NSFetchRequest(entityName: "PasswordEntity") - let passwordCategoryEntityFetchRequest = NSFetchRequest(entityName: "PasswordCategoryEntity") - let passwordEntityDeleteRequest = NSBatchDeleteRequest(fetchRequest: passwordEntityFetchRequest) - let passwordCategoryEntityDeleteRequest = NSBatchDeleteRequest(fetchRequest: passwordCategoryEntityFetchRequest) - - do { - try context.execute(passwordEntityDeleteRequest) - try context.execute(passwordCategoryEntityDeleteRequest) - } catch let error as NSError { - print(error) - } - + deleteCoreData(entityName: "PasswordEntity") + deleteCoreData(entityName: "PasswordCategoryEntity") + let fm = FileManager.default fm.enumerator(atPath: storeURL.path)?.forEach({ (e) in if let e = e as? String, let url = URL(string: e) { @@ -186,4 +177,56 @@ class PasswordStore { func updateRemoteRepo() { } + + func deleteCoreData(entityName: String) { + let deleteFetchRequest = NSFetchRequest(entityName: entityName) + let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetchRequest) + + do { + try context.execute(deleteRequest) + } catch let error as NSError { + print(error) + } + } + + func erase() { + let fm = FileManager.default + do { + if fm.fileExists(atPath: storeURL.path) { + try fm.removeItem(at: storeURL) + } + + if fm.fileExists(atPath: Globals.shared.secringPath) { + try fm.removeItem(atPath: Globals.shared.secringPath) + } + + if fm.fileExists(atPath: Globals.shared.sshPrivateKeyPath.path) { + try fm.removeItem(at: Globals.shared.sshPrivateKeyPath) + } + + if fm.fileExists(atPath: Globals.shared.sshPublicKeyPath.path) { + try fm.removeItem(at: Globals.shared.sshPublicKeyPath) + } + } catch { + print(error) + } + + deleteCoreData(entityName: "PasswordEntity") + deleteCoreData(entityName: "PasswordCategoryEntity") + + Defaults[.pgpKeyURL] = nil + + Defaults[.pgpKeyPassphrase] = "" + Defaults[.pgpKeyID] = "" + Defaults[.pgpKeyUserID] = "" + + Defaults[.gitRepositoryURL] = nil + Defaults[.gitRepositoryAuthenticationMethod] = "" + Defaults[.gitRepositoryUsername] = "" + Defaults[.gitRepositoryPassword] = "" + Defaults[.gitRepositorySSHPublicKeyURL] = nil + Defaults[.gitRepositorySSHPrivateKeyURL] = nil + Defaults[.gitRepositorySSHPrivateKeyPassphrase] = nil + Defaults[.lastUpdatedTime] = nil + } } diff --git a/pass/PasswordsViewController.swift b/pass/PasswordsViewController.swift index 48dfd10..16bf328 100644 --- a/pass/PasswordsViewController.swift +++ b/pass/PasswordsViewController.swift @@ -55,6 +55,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV super.viewDidLoad() passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil) + generateSections(item: passwordEntities!) tableView.delegate = self tableView.dataSource = self @@ -160,6 +162,13 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV print("actOnPasswordUpdatedNotification") } + func actOnPasswordStoreErasedNotification() { + passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() + reloadTableView(data: passwordEntities!) + print("actOnPasswordErasedNotification") + } + + override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "showPasswordDetail" { if Defaults[.pgpKeyID] == "" { diff --git a/pass/SettingsTableViewController.swift b/pass/SettingsTableViewController.swift index 6e902e3..083fb0d 100644 --- a/pass/SettingsTableViewController.swift +++ b/pass/SettingsTableViewController.swift @@ -105,6 +105,10 @@ class SettingsTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) if Defaults[.pgpKeyID] == "" { pgpKeyTableViewCell.detailTextLabel?.text = "Not Set" } else {