From 5906a91397ed42e74ab7671c8b1e98fb9bbf8c73 Mon Sep 17 00:00:00 2001 From: Stuart Connolly Date: Tue, 21 Mar 2017 19:59:55 +0000 Subject: [PATCH] Fix user manager throwing an exception when mysql.user.authentication_string is NULL. --- Source/SPUserManager.h | 6 +++--- Source/SPUserManager.m | 36 +++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Source/SPUserManager.h b/Source/SPUserManager.h index 5ce1e6bcc..ffee376dc 100644 --- a/Source/SPUserManager.h +++ b/Source/SPUserManager.h @@ -81,7 +81,7 @@ BOOL isInitializing; NSMutableString *errorsString; - // MySQL 5.7.6 removes the "Password" columns and only uses the "plugin"+"authentication_string" columns + // MySQL 5.7.6 removes the "Password" columns and only uses the "plugin" + "authentication_string" columns BOOL requiresPost576PasswordHandling; } @@ -114,14 +114,14 @@ - (IBAction)closeErrorsSheet:(id)sender; - (IBAction)doubleClickSchemaPriv:(id)sender; -// Schema Privieges +// Schema privieges - (IBAction)addSchemaPriv:(id)sender; - (IBAction)removeSchemaPriv:(id)sender; // Refresh - (IBAction)refresh:(id)sender; -// Core Data notifications +// Core data notifications - (BOOL)insertUser:(SPUserMO *)user; - (BOOL)deleteUser:(SPUserMO *)user; - (BOOL)updateUser:(SPUserMO *)user; diff --git a/Source/SPUserManager.m b/Source/SPUserManager.m index 8280242f5..c06025d24 100644 --- a/Source/SPUserManager.m +++ b/Source/SPUserManager.m @@ -221,20 +221,24 @@ - (void)_initializeUsers */ - (void)_initializeTree:(NSArray *)items { - // Retrieve all the user data in order to be able to initialise the schema privs for each child, // copying into a dictionary keyed by user, each with all the host rows. NSMutableDictionary *schemaPrivilegeData = [NSMutableDictionary dictionary]; SPMySQLResult *queryResults = [connection queryString:@"SELECT * FROM mysql.db"]; + [queryResults setReturnDataAsStrings:YES]; - for (NSDictionary *privRow in queryResults) { + + for (NSDictionary *privRow in queryResults) + { if (![schemaPrivilegeData objectForKey:[privRow objectForKey:@"User"]]) { [schemaPrivilegeData setObject:[NSMutableArray array] forKey:[privRow objectForKey:@"User"]]; } + [[schemaPrivilegeData objectForKey:[privRow objectForKey:@"User"]] addObject:privRow]; // If "all database" values were found, add them to the schemas list if not already present NSString *schemaName = [privRow objectForKey:@"Db"]; + if ([schemaName isEqualToString:@""] || [schemaName isEqualToString:@"%"]) { if (![schemas containsObject:schemaName]) { [schemas addObject:schemaName]; @@ -269,11 +273,20 @@ - (void)_initializeTree:(NSArray *)items // original values for comparison purposes [parent setPrimitiveValue:username forKey:@"user"]; [parent setPrimitiveValue:username forKey:@"originaluser"]; - if(requiresPost576PasswordHandling) { + + if (requiresPost576PasswordHandling) { [parent setPrimitiveValue:[item objectForKey:@"plugin"] forKey:@"plugin"]; - NSString *pwHash = [item objectForKey:@"authentication_string"]; - [parent setPrimitiveValue:pwHash forKey:@"authentication_string"]; - if([pwHash length]) [parent setPrimitiveValue:@"sequelpro_dummy_password" forKey:@"password"]; // for the UI dialog + + NSString *passwordHash = [item objectForKey:@"authentication_string"]; + + if (![passwordHash isNSNull]) { + [parent setPrimitiveValue:passwordHash forKey:@"authentication_string"]; + + // for the UI dialog + if ([passwordHash length]) { + [parent setPrimitiveValue:@"sequelpro_dummy_password" forKey:@"password"]; + } + } } else { [parent setPrimitiveValue:[item objectForKey:@"Password"] forKey:@"password"]; @@ -535,7 +548,6 @@ - (IBAction)doCancel:(id)sender */ - (IBAction)doApply:(id)sender { - // If editing can't be committed, cancel the apply if (![treeController commitEditing]) { return; @@ -953,6 +965,9 @@ - (void)contextDidChange:(NSNotification *)notification if (!isInitializing) [outlineView reloadData]; } +#pragma mark - +#pragma mark Core data notifications + - (BOOL)updateUser:(SPUserMO *)user { if (![user parent]) { @@ -1290,6 +1305,9 @@ - (BOOL)grantPrivilegesToUser:(SPUserMO *)user skippingRevoke:(BOOL)skipRevoke return YES; } +#pragma mark - +#pragma mark Private API + /** * Gets any NSManagedObject (SPUser) from the managedObjectContext that may * already exist with the given username. @@ -1446,9 +1464,6 @@ - (BOOL)_checkAndDisplayMySqlError return YES; } -#pragma mark - -#pragma mark Private API - /** * Renames a user account using the supplied parameters. * @@ -1511,7 +1526,6 @@ - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - SPClear(managedObjectContext); SPClear(persistentStoreCoordinator); SPClear(managedObjectModel);