From 96d97d95e188da0e1a2eea79880737d545a16186 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Thu, 2 May 2013 20:40:12 -0400 Subject: [PATCH] New user on Mac. [ADDED] Mac: Support for creating a new user. --- MasterPassword/ObjC/Mac/MPMacAppDelegate.m | 29 +++++++++++++++++++ .../ObjC/Mac/MPPasswordWindowController.m | 28 +++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/MasterPassword/ObjC/Mac/MPMacAppDelegate.m b/MasterPassword/ObjC/Mac/MPMacAppDelegate.m index 84664997..96cf8000 100644 --- a/MasterPassword/ObjC/Mac/MPMacAppDelegate.m +++ b/MasterPassword/ObjC/Mac/MPMacAppDelegate.m @@ -154,6 +154,35 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven } - (IBAction)newUser:(NSMenuItem *)sender { + + NSAlert *alert = [NSAlert alertWithMessageText:@"New User" + defaultButton:@"Create User" alternateButton:nil otherButton:@"Cancel" + informativeTextWithFormat:@"To begin, enter your full name.\n\n" + @"IMPORTANT: Enter your name correctly, including the right capitalization, " + @"as you would on an official document."]; + NSTextField *nameField = [[NSTextField alloc] initWithFrame:NSMakeRect( 0, 0, 200, 22 )]; + [alert setAccessoryView:nameField]; + [alert layout]; + [nameField becomeFirstResponder]; + if ([alert runModal] != NSAlertDefaultReturn) + return; + + NSString *name = [(NSSecureTextField *)alert.accessoryView stringValue]; + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { + MPUserEntity *newUser = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass( [MPUserEntity class] ) + inManagedObjectContext:moc]; + newUser.name = name; + [moc saveToStore]; + NSError *error = nil; + if (![moc obtainPermanentIDsForObjects:@[ newUser ] error:&error]) + err(@"Failed to obtain permanent object ID for new user: %@", error); + + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [self updateUsers]; + [self setActiveUser:newUser]; + [self showPasswordWindow]; + }]; + }]; } - (IBAction)lock:(id)sender { diff --git a/MasterPassword/ObjC/Mac/MPPasswordWindowController.m b/MasterPassword/ObjC/Mac/MPPasswordWindowController.m index 00794ed1..3b7f6727 100644 --- a/MasterPassword/ObjC/Mac/MPPasswordWindowController.m +++ b/MasterPassword/ObjC/Mac/MPPasswordWindowController.m @@ -58,6 +58,7 @@ }]; [[NSNotificationCenter defaultCenter] addObserverForName:MPSignedOutNotification object:nil queue:nil usingBlock:^(NSNotification *note) { + _activeElementOID = nil; [self.window close]; }]; @@ -143,27 +144,28 @@ self.contentContainer.alphaValue = 0; [self.progressView startAnimation:nil]; self.inProgress = YES; - [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { + + NSString *password = [(NSSecureTextField *)alert.accessoryView stringValue]; + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc_) { NSError *error = nil; - MPUserEntity *activeUser_ = (MPUserEntity *)[moc existingObjectWithID:activeUser.objectID error:&error]; + MPUserEntity *activeUser_ = (MPUserEntity *)[moc_ existingObjectWithID:activeUser.objectID error:&error]; if (!activeUser_) - err(@"Failed to retrieve active use while logging in: %@", error); - - BOOL success = [[MPMacAppDelegate get] signInAsUser:activeUser saveInContext:moc - usingMasterPassword:[(NSSecureTextField *)alert.accessoryView stringValue]]; + err(@"Failed to retrieve active use while logging in: %@", error); + + BOOL success = [[MPMacAppDelegate get] signInAsUser:activeUser saveInContext:moc_ + usingMasterPassword:password]; self.inProgress = NO; - + dispatch_async( dispatch_get_main_queue(), ^{ [self.progressView stopAnimation:nil]; - + if (success) self.contentContainer.alphaValue = 1; else { [[NSAlert alertWithError:[NSError errorWithDomain:MPErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey : PearlString( @"Incorrect master password for user %@", - activeUser.name ) - }]] beginSheetModalForWindow:self.window modalDelegate:self - didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:MPAlertIncorrectMP]; + NSLocalizedDescriptionKey : PearlString( @"Incorrect master password for user %@", activeUser.name ) + }]] beginSheetModalForWindow:self.window modalDelegate:self + didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:MPAlertIncorrectMP]; } } ); }]; @@ -194,7 +196,7 @@ NSError *error = nil; NSArray *siteResults = [context executeFetchRequest:fetchRequest error:&error]; if (!siteResults) - err(@"While fetching elements for completion: %@", error); + err(@"While fetching elements for completion: %@", error); else if ([siteResults count]) { _activeElementOID = ((NSManagedObject *)[siteResults objectAtIndex:0]).objectID; for (MPElementEntity *element in siteResults)