Show error when adding password with same name

This commit is contained in:
Bob Sun 2017-03-02 17:26:12 +08:00
parent 2d50a40370
commit bdc2cfaaa8
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 38 additions and 9 deletions

View file

@ -78,15 +78,23 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Saving") SVProgressHUD.show(withStatus: "Saving")
DispatchQueue.global(qos: .userInitiated).async { 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 { 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")))
} }
} }
} }

View file

@ -185,6 +185,23 @@ class PasswordStore {
return fm.fileExists(atPath: Globals.repositoryPath) return fm.fileExists(atPath: Globals.repositoryPath)
} }
func passwordExisted(password: Password) -> Bool {
print(password.name)
let passwordEntityFetchRequest = NSFetchRequest<NSFetchRequestResult>(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 { func initPGP(pgpPublicKeyURL: URL, pgpPublicKeyLocalPath: String, pgpPrivateKeyURL: URL, pgpPrivateKeyLocalPath: String) throws {
let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL) let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL)
try pgpPublicData.write(to: URL(fileURLWithPath: pgpPublicKeyLocalPath), options: .atomic) try pgpPublicData.write(to: URL(fileURLWithPath: pgpPublicKeyLocalPath), options: .atomic)
@ -367,10 +384,11 @@ class PasswordStore {
passwordEntityFetchRequest.predicate = NSPredicate(format: "synced = %i", 0) passwordEntityFetchRequest.predicate = NSPredicate(format: "synced = %i", 0)
return try context.count(for: passwordEntityFetchRequest) return try context.count(for: passwordEntityFetchRequest)
} catch { } catch {
fatalError("Failed to fetch employees: \(error)") fatalError("Failed to fetch unsynced passwords: \(error)")
} }
} }
func getLatestUpdateInfo(filename: String) -> String { func getLatestUpdateInfo(filename: String) -> String {
guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks, guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks,
let latestCommitTime = blameHunks?.map({ let latestCommitTime = blameHunks?.map({
@ -488,8 +506,11 @@ class PasswordStore {
try storeRepository?.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock) 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) 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 let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity
do { do {
let encryptedData = try passwordEntity.encrypt(password: password) let encryptedData = try passwordEntity.encrypt(password: password)