Forced password change now is only displayed after unlock and before save.

This commit is contained in:
michael starke
2014-08-26 23:18:06 +02:00
parent d16df3ff02
commit 8d9a6ca881
4 changed files with 53 additions and 48 deletions

View File

@@ -215,7 +215,17 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[weakDocument saveDocument:sender]; [weakDocument saveDocument:sender];
} }
}; };
[self _editPasswordRequiringValidInput:YES]; [self editPassword:nil];
return;
}
else if(document.shouldEnforcePasswordChange) {
__weak MPDocument *weakDocument = [self document];
self.passwordChangedBlock = ^void(BOOL didChangePassword){
if(didChangePassword) {
[weakDocument saveDocument:nil];
}
};
[self _presentPasswordIntervalAlters];
return; return;
} }
/* All set and good ready to save */ /* All set and good ready to save */
@@ -224,17 +234,18 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
- (void)saveDocumentAs:(id)sender { - (void)saveDocumentAs:(id)sender {
self.passwordChangedBlock = nil; self.passwordChangedBlock = nil;
MPDocument *document = [self document]; MPDocument *document = [self document];
if(!document.compositeKey) { if(document.compositeKey) {
__weak MPDocument *weakDocument = [self document]; [[self document] saveDocumentAs:sender];
self.passwordChangedBlock = ^void(BOOL didChangePassword){
if(didChangePassword) {
[weakDocument saveDocumentAs:sender];
}
};
[self _editPasswordRequiringValidInput:YES];
return; return;
} }
[[self document] saveDocumentAs:sender]; /* we need to make sure that a password is set */
__weak MPDocument *weakDocument = [self document];
self.passwordChangedBlock = ^void(BOOL didChangePassword){
if(didChangePassword) {
[weakDocument saveDocumentAs:sender];
}
};
[self editPassword:nil];
} }
- (void)exportAsXML:(id)sender { - (void)exportAsXML:(id)sender {
@@ -283,16 +294,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
} }
- (void)editPassword:(id)sender { - (void)editPassword:(id)sender {
[self _editPasswordRequiringValidInput:YES];
}
- (void)_editPasswordRequiringValidInput:(BOOL)canCancel {
if(!self.passwordEditWindowController) { if(!self.passwordEditWindowController) {
self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] initWithDocument:[self document]]; self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] initWithDocument:[self document]];
self.passwordEditWindowController.delegate = self; self.passwordEditWindowController.delegate = self;
} }
/* Disallow empty password if we want to save afterwards, otherwise the dialog keeps poping up */
self.passwordEditWindowController.allowsEmptyPasswordOrKey = canCancel;
[NSApp beginSheet:[self.passwordEditWindowController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; [NSApp beginSheet:[self.passwordEditWindowController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
} }
@@ -454,7 +459,9 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert setAlertStyle:NSCriticalAlertStyle]; [alert setAlertStyle:NSCriticalAlertStyle];
[alert setMessageText:NSLocalizedString(@"ENFORCE_PASSWORD_CHANGE_ALERT_TITLE", "")]; [alert setMessageText:NSLocalizedString(@"ENFORCE_PASSWORD_CHANGE_ALERT_TITLE", "")];
[alert setInformativeText:NSLocalizedString(@"ENFORCE_PASSWORD_CHANGE_ALERT_DESCRIPTION", "")]; [alert setInformativeText:NSLocalizedString(@"ENFORCE_PASSWORD_CHANGE_ALERT_DESCRIPTION", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")];
[[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_enforcePasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_enforcePasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL];
} }
else if(document.shouldRecommendPasswordChange) { else if(document.shouldRecommendPasswordChange) {
@@ -462,7 +469,7 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert setAlertStyle:NSInformationalAlertStyle]; [alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:NSLocalizedString(@"RECOMMEND_PASSWORD_CHANGE_ALERT_TITLE", "")]; [alert setMessageText:NSLocalizedString(@"RECOMMEND_PASSWORD_CHANGE_ALERT_TITLE", "")];
[alert setInformativeText:NSLocalizedString(@"RECOMMEND_PASSWORD_CHANGE_ALERT_DESCRIPTION", "")]; [alert setInformativeText:NSLocalizedString(@"RECOMMEND_PASSWORD_CHANGE_ALERT_DESCRIPTION", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")];
[[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]]; [[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_recommentPasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_recommentPasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL];
@@ -491,25 +498,23 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
} }
- (void)_recommentPasswordChangeAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { - (void)_recommentPasswordChangeAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if(returnCode == NSAlertFirstButtonReturn) { if(returnCode == NSAlertSecondButtonReturn) {
id __weak welf = self; return;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[welf _editPasswordRequiringValidInput:YES];
});
} }
id __weak welf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[welf editPassword:nil];
});
} }
- (void)_enforcePasswordChangeAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { - (void)_enforcePasswordChangeAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
NSAssert(returnCode == NSAlertFirstButtonReturn, @"Return for password change should always be NSAlertFirstButtonReturn"); if(NSAlertSecondButtonReturn == returnCode) {
return;
}
id __weak welf = self; id __weak welf = self;
self.passwordChangedBlock = ^(BOOL didChangePassword){
if(!didChangePassword) {
[welf _presentPasswordIntervalAlters];
}
};
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[welf _editPasswordRequiringValidInput:NO]; [welf editPassword:nil];
}); });
} }

View File

@@ -31,8 +31,8 @@
@property (weak) IBOutlet NSTextField *errorTextField; @property (weak) IBOutlet NSTextField *errorTextField;
@property (weak) IBOutlet NSButton *changePasswordButton; @property (weak) IBOutlet NSButton *changePasswordButton;
@property (weak) IBOutlet NSButton *hasPasswordSwitchButton; @property (weak) IBOutlet NSButton *hasPasswordSwitchButton;
@property (nonatomic,assign) BOOL allowsEmptyPasswordOrKey;
//@property (nonatomic,assign) BOOL allowsEmptyPasswordOrKey;
@property (weak) id<MPPasswordEditWindowDelegate> delegate; @property (weak) id<MPPasswordEditWindowDelegate> delegate;
/** /**

View File

@@ -35,7 +35,7 @@
- (id)initWithDocument:(MPDocument *)document { - (id)initWithDocument:(MPDocument *)document {
self = [super initWithWindow:nil]; self = [super initWithWindow:nil];
if(self){ if(self){
_allowsEmptyPasswordOrKey = YES; //_allowsEmptyPasswordOrKey = YES;
_showPassword = NO; _showPassword = NO;
_hasValidPasswordOrKey = NO; _hasValidPasswordOrKey = NO;
_currentDocument = document; _currentDocument = document;
@@ -55,22 +55,22 @@
return; return;
} }
self.showPassword = NO; self.showPassword = NO;
// TODO: fix initial view for password edit
//[self.passwordTextField setStringValue:_currentDocument.password ? _currentDocument.password : @""];
//[self.passwordRepeatTextField setStringValue:[self.passwordTextField stringValue]];
//self.keyURL = _currentDocument.key;
NSDictionary *negateOption = @{ NSValueTransformerNameBindingOption : NSNegateBooleanTransformerName }; NSDictionary *negateOption = @{ NSValueTransformerNameBindingOption : NSNegateBooleanTransformerName };
[self.hasPasswordSwitchButton bind:NSValueBinding toObject:self withKeyPath:@"enablePassword" options:nil]; NSString *enablePasswordKeyPath = NSStringFromSelector(@selector(enablePassword));
[self.passwordTextField bind:NSStringFromSelector(@selector(showPassword)) toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil]; NSString *showPasswordKeyPath = NSStringFromSelector(@selector(showPassword));
[self.passwordTextField bind:NSEnabledBinding toObject:self withKeyPath:@"enablePassword" options:nil]; NSString *hasValidPasswordOrKeyKeyPath = NSStringFromSelector(@selector(hasValidPasswordOrKey));
[self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
[self.togglePasswordButton bind:NSEnabledBinding toObject:self withKeyPath:@"enablePassword" options:nil]; [self.hasPasswordSwitchButton bind:NSValueBinding toObject:self withKeyPath:enablePasswordKeyPath options:nil];
[self.passwordRepeatTextField bind:NSEnabledBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:negateOption]; [self.passwordTextField bind:showPasswordKeyPath toObject:self withKeyPath:showPasswordKeyPath options:nil];
[self.passwordRepeatTextField bind:NSEnabledBinding toObject:self withKeyPath:@"enablePassword" options:nil]; [self.passwordTextField bind:NSEnabledBinding toObject:self withKeyPath:enablePasswordKeyPath options:nil];
[self.errorTextField bind:NSHiddenBinding toObject:self withKeyPath:@"hasValidPasswordOrKey" options:nil]; [self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:showPasswordKeyPath options:nil];
[self.changePasswordButton bind:NSEnabledBinding toObject:self withKeyPath:@"hasValidPasswordOrKey" options:nil]; [self.togglePasswordButton bind:NSEnabledBinding toObject:self withKeyPath:enablePasswordKeyPath options:nil];
[self.keyfilePathControl bind:NSValueBinding toObject:self withKeyPath:@"keyURL" options:nil]; [self.passwordRepeatTextField bind:NSEnabledBinding toObject:self withKeyPath:showPasswordKeyPath options:negateOption];
[self.passwordRepeatTextField bind:NSEnabledBinding toObject:self withKeyPath:enablePasswordKeyPath options:nil];
[self.errorTextField bind:NSHiddenBinding toObject:self withKeyPath:hasValidPasswordOrKeyKeyPath options:nil];
[self.changePasswordButton bind:NSEnabledBinding toObject:self withKeyPath:hasValidPasswordOrKeyKeyPath options:nil];
[self.keyfilePathControl bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(keyURL)) options:nil];
[self.passwordRepeatTextField setDelegate:self]; [self.passwordRepeatTextField setDelegate:self];
[self.passwordTextField setDelegate:self]; [self.passwordTextField setDelegate:self];
@@ -169,7 +169,7 @@
BOOL hasPasswordOrKey = (hasKey || hasPassword); BOOL hasPasswordOrKey = (hasKey || hasPassword);
keyOk = hasKey ? keyOk : YES; keyOk = hasKey ? keyOk : YES;
passwordOk = hasPassword ? passwordOk : YES; passwordOk = hasPassword ? passwordOk : YES;
self.hasValidPasswordOrKey = (hasPasswordOrKey || self.allowsEmptyPasswordOrKey ) && passwordOk && keyOk; self.hasValidPasswordOrKey = hasPasswordOrKey && passwordOk && keyOk;
if(!hasPasswordOrKey) { if(!hasPasswordOrKey) {
[self.errorTextField setTextColor:[NSColor controlTextColor]]; [self.errorTextField setTextColor:[NSColor controlTextColor]];

Binary file not shown.