2
0

New user on Mac.

[ADDED]     Mac: Support for creating a new user.
This commit is contained in:
Maarten Billemont 2013-05-02 20:40:12 -04:00
parent 5e1e88bdeb
commit 96d97d95e1
2 changed files with 44 additions and 13 deletions

View File

@ -154,6 +154,35 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven
} }
- (IBAction)newUser:(NSMenuItem *)sender { - (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 { - (IBAction)lock:(id)sender {

View File

@ -58,6 +58,7 @@
}]; }];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserverForName:MPSignedOutNotification object:nil queue:nil usingBlock:^(NSNotification *note) { addObserverForName:MPSignedOutNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
_activeElementOID = nil;
[self.window close]; [self.window close];
}]; }];
@ -143,14 +144,16 @@
self.contentContainer.alphaValue = 0; self.contentContainer.alphaValue = 0;
[self.progressView startAnimation:nil]; [self.progressView startAnimation:nil];
self.inProgress = YES; self.inProgress = YES;
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) {
NSError *error = nil;
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 NSString *password = [(NSSecureTextField *)alert.accessoryView stringValue];
usingMasterPassword:[(NSSecureTextField *)alert.accessoryView stringValue]]; [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc_) {
NSError *error = nil;
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:password];
self.inProgress = NO; self.inProgress = NO;
dispatch_async( dispatch_get_main_queue(), ^{ dispatch_async( dispatch_get_main_queue(), ^{
@ -160,10 +163,9 @@
self.contentContainer.alphaValue = 1; self.contentContainer.alphaValue = 1;
else { else {
[[NSAlert alertWithError:[NSError errorWithDomain:MPErrorDomain code:0 userInfo:@{ [[NSAlert alertWithError:[NSError errorWithDomain:MPErrorDomain code:0 userInfo:@{
NSLocalizedDescriptionKey : PearlString( @"Incorrect master password for user %@", NSLocalizedDescriptionKey : PearlString( @"Incorrect master password for user %@", activeUser.name )
activeUser.name ) }]] beginSheetModalForWindow:self.window modalDelegate:self
}]] beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:MPAlertIncorrectMP];
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:MPAlertIncorrectMP];
} }
} ); } );
}]; }];
@ -194,7 +196,7 @@
NSError *error = nil; NSError *error = nil;
NSArray *siteResults = [context executeFetchRequest:fetchRequest error:&error]; NSArray *siteResults = [context executeFetchRequest:fetchRequest error:&error];
if (!siteResults) if (!siteResults)
err(@"While fetching elements for completion: %@", error); err(@"While fetching elements for completion: %@", error);
else if ([siteResults count]) { else if ([siteResults count]) {
_activeElementOID = ((NSManagedObject *)[siteResults objectAtIndex:0]).objectID; _activeElementOID = ((NSManagedObject *)[siteResults objectAtIndex:0]).objectID;
for (MPElementEntity *element in siteResults) for (MPElementEntity *element in siteResults)