From bdc2cfaaa8509252907faf24498017ab7aedeacd Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Thu, 2 Mar 2017 17:26:12 +0800 Subject: [PATCH] Show error when adding password with same name --- .../Controllers/PasswordsViewController.swift | 22 ++++++++++------ pass/Models/PasswordStore.swift | 25 +++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 529b29f..4b27d42 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -78,15 +78,23 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.show(withStatus: "Saving") DispatchQueue.global(qos: .userInitiated).async { - PasswordStore.shared.add(password: controller.password!, progressBlock: { progress in + do { + try PasswordStore.shared.add(password: controller.password!, progressBlock: { progress in + DispatchQueue.main.async { + SVProgressHUD.showProgress(progress, status: "Encrypting") + } + }) + DispatchQueue.main.async { - SVProgressHUD.showProgress(progress, status: "Encrypting") + SVProgressHUD.showSuccess(withStatus: "Done") + SVProgressHUD.dismiss(withDelay: 1) + NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) + } + } catch { + DispatchQueue.main.async { + SVProgressHUD.showError(withStatus: error.localizedDescription) + SVProgressHUD.dismiss(withDelay: 1) } - }) - DispatchQueue.main.async { - SVProgressHUD.showSuccess(withStatus: "Done") - SVProgressHUD.dismiss(withDelay: 1) - NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) } } } diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index 228f36f..e816cfa 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -185,6 +185,23 @@ class PasswordStore { return fm.fileExists(atPath: Globals.repositoryPath) } + func passwordExisted(password: Password) -> Bool { + print(password.name) + let passwordEntityFetchRequest = NSFetchRequest(entityName: "PasswordEntity") + do { + passwordEntityFetchRequest.predicate = NSPredicate(format: "name = %@", password.name) + let count = try context.count(for: passwordEntityFetchRequest) + if count > 0 { + return true + } else { + return false + } + } catch { + fatalError("Failed to fetch password entities: \(error)") + } + return true + } + func initPGP(pgpPublicKeyURL: URL, pgpPublicKeyLocalPath: String, pgpPrivateKeyURL: URL, pgpPrivateKeyLocalPath: String) throws { let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL) try pgpPublicData.write(to: URL(fileURLWithPath: pgpPublicKeyLocalPath), options: .atomic) @@ -367,10 +384,11 @@ class PasswordStore { passwordEntityFetchRequest.predicate = NSPredicate(format: "synced = %i", 0) return try context.count(for: passwordEntityFetchRequest) } catch { - fatalError("Failed to fetch employees: \(error)") + fatalError("Failed to fetch unsynced passwords: \(error)") } } + func getLatestUpdateInfo(filename: String) -> String { guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks, let latestCommitTime = blameHunks?.map({ @@ -488,8 +506,11 @@ class PasswordStore { try storeRepository?.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock) } - func add(password: Password, progressBlock: (_ progress: Float) -> Void) { + func add(password: Password, progressBlock: (_ progress: Float) -> Void) throws { progressBlock(0.0) + guard !passwordExisted(password: password) else { + throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot add password: password duplicated."]) + } let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity do { let encryptedData = try passwordEntity.encrypt(password: password)