diff options
author | stuconnolly <stuart02@gmail.com> | 2010-03-09 15:25:36 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2010-03-09 15:25:36 +0000 |
commit | 2e77b3e6b1064403c8518d4c6bae6be8a7cdd3de (patch) | |
tree | 09a1bdac4daddb818a590b684a320bc9b529d064 /Source/SPUserManager.m | |
parent | 6560b2e188308d41d16c1e5d8c5b3bc05dbae8fe (diff) | |
download | sequelpro-2e77b3e6b1064403c8518d4c6bae6be8a7cdd3de.tar.gz sequelpro-2e77b3e6b1064403c8518d4c6bae6be8a7cdd3de.tar.bz2 sequelpro-2e77b3e6b1064403c8518d4c6bae6be8a7cdd3de.zip |
Fix an exception in the user manager caused by sorting the users list by making the user manager KVO compliant for the sort descriptors array. Also, add a bunch of comments and general tidy up on SPUserManager.m.
Diffstat (limited to 'Source/SPUserManager.m')
-rw-r--r-- | Source/SPUserManager.m | 176 |
1 files changed, 120 insertions, 56 deletions
diff --git a/Source/SPUserManager.m b/Source/SPUserManager.m index 0cc2b125..9ce7e5d4 100644 --- a/Source/SPUserManager.m +++ b/Source/SPUserManager.m @@ -62,6 +62,7 @@ @synthesize schemas; @synthesize grantedSchemaPrivs; @synthesize availablePrivs; +@synthesize treeSortDescriptors; - (id)init { @@ -88,27 +89,6 @@ return self; } -/** - * Dealloc. Get rid of everything. - */ -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self - name:NSManagedObjectContextDidSaveNotification - object:nil]; - [managedObjectContext release]; - [persistentStoreCoordinator release]; - [managedObjectModel release]; - [privColumnToGrantMap release]; - [mySqlConnection release]; - [privsSupportedByServer release]; - [schemas release]; - [availablePrivs release]; - [grantedSchemaPrivs release]; - - [super dealloc]; -} - /** * UI specific items to set up when the window loads. This is different than awakeFromNib * as it's only called once. @@ -128,6 +108,11 @@ [self _initializeUsers]; [self _initializeSchemaPrivs]; + + treeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; + + [self setTreeSortDescriptors:[NSArray arrayWithObject:treeSortDescriptor]]; + [super windowDidLoad]; } @@ -193,11 +178,12 @@ isInitializing = FALSE; } +/** + * Initialize the outline view tree. The NSOutlineView gets it's data from a NSTreeController which gets + * it's data from the SPUser Entity objects in the current managedObjectContext. + */ - (void)_initializeTree:(NSArray *)items -{ - // The NSOutlineView gets it's data from a NSTreeController which gets - // it's data from the SPUser Entity objects in the current managedObjectContext. - +{ // Go through each item that contains a dictionary of key-value pairs // for each user currently in the database. for(NSInteger i = 0; i < [items count]; i++) @@ -243,12 +229,16 @@ } [parentResults release]; } + // Reload data of the outline view with the changes. [outlineView reloadData]; [treeController rearrangeObjects]; } -- (void) _initializeAvailablePrivs +/** + * Initialize the available user privileges. + */ +- (void)_initializeAvailablePrivs { // Initialize available privileges NSManagedObjectContext *moc = self.managedObjectContext; @@ -256,6 +246,7 @@ inManagedObjectContext:moc]; NSArray *props = [privEntityDescription attributeKeys]; [availablePrivs removeAllObjects]; + for (NSString *prop in props) { if ([prop hasSuffix:@"_priv"] && [[self.privsSupportedByServer objectForKey:prop] boolValue]) @@ -266,25 +257,31 @@ } } + [availableController rearrangeObjects]; - } + +/** + * Initialize the available schema privileges. + */ - (void)_initializeSchemaPrivs { // Initialize Databases MCPResult *results = [self.mySqlConnection listDBs]; + if ([results numOfRows]) { [results dataSeek:0]; } + for (int i = 0; i < [results numOfRows]; i++) { [schemas addObject:[results fetchRowAsDictionary]]; } - [schemaController rearrangeObjects]; - [self _initializeAvailablePrivs]; - + [schemaController rearrangeObjects]; + [self _initializeAvailablePrivs]; } + /** * Set NSManagedObject with values from the passed in dictionary. */ @@ -325,6 +322,9 @@ } } +/** + * Initialize the schema privileges for the supplied child object. + */ - (void)_initializeSchemaPrivsForChild:(NSManagedObject *)child { // Assumes that the child has already been initialized with values from the @@ -361,7 +361,6 @@ NSMutableSet *privs = [child mutableSetValueForKey:@"schema_privileges"]; [privs addObject:dbPriv]; } - } /** @@ -531,13 +530,6 @@ return YES; } -- (NSArray *)treeSortDescriptors -{ - NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES] autorelease]; - - return [NSArray arrayWithObject:descriptor]; -} - #pragma mark - #pragma mark General IBAction methods @@ -614,9 +606,11 @@ @catch (NSException * e) { } } - } +/** + * Adds a new user to the current database. + */ - (IBAction)addUser:(id)sender { // Adds a new SPUser objects to the managedObjectContext and sets default values @@ -636,6 +630,9 @@ [[self window] makeFirstResponder:userNameTextField]; } +/** + * Removes the currently selected user from the current database. + */ - (IBAction)removeUser:(id)sender { NSString *username = [[[treeController selectedObjects] objectAtIndex:0] @@ -646,9 +643,13 @@ { [child setPrimitiveValue:username forKey:@"user"]; } + [treeController remove:sender]; } +/** + * Adds a new host to the currently selected user. + */ - (IBAction)addHost:(id)sender { if ([[treeController selectedObjects] count] > 0) @@ -658,6 +659,7 @@ [self _selectParentFromSelection]; } } + [treeController addChild:sender]; // The newly added item will be selected as it is added, but only after the next iteration of the @@ -665,12 +667,17 @@ [self performSelector:@selector(editNewHost) withObject:nil afterDelay:0.1]; } -// Perform a deferred edit of the currently selected row. +/** + * Perform a deferred edit of the currently selected row. + */ - (void)editNewHost { [outlineView editColumn:0 row:[outlineView selectedRow] withEvent:nil select:TRUE]; } +/** + * Removes the currently selected host from it's parent user. + */ - (IBAction)removeHost:(id)sender { // Set the username on the child so that it's accessabile when building @@ -679,6 +686,7 @@ NSManagedObject *parent = [child valueForKey:@"parent"]; [child setPrimitiveValue:[[child valueForKey:@"parent"] valueForKey:@"user"] forKey:@"user"]; [treeController remove:sender]; + if ([[parent valueForKey:@"children"] count] == 0) { NSAlert *alert = [NSAlert alertWithMessageText:@"User doesn't have any hosts." @@ -690,6 +698,9 @@ } } +/** + * Adds a new schema privilege. + */ - (IBAction)addSchemaPriv:(id)sender { NSArray *selectedObjects = [availableController selectedObjects]; @@ -700,6 +711,9 @@ [self _setSchemaPrivValues:selectedObjects enabled:YES]; } +/** + * Removes a schema privilege. + */ - (IBAction)removeSchemaPriv:(id)sender { NSArray *selectedObjects = [grantedController selectedObjects]; @@ -708,9 +722,11 @@ [grantedController removeObjects:selectedObjects]; [grantedTableView reloadData]; [self _setSchemaPrivValues:selectedObjects enabled:NO]; - } +/** + * Refreshes the current list of users. + */ - (IBAction)refresh:(id)sender { if ([self.managedObjectContext hasChanges]) @@ -720,18 +736,20 @@ alternateButton:NSLocalizedString(@"Cancel", @"cancel button") otherButton:nil informativeTextWithFormat:@"Window has changes. All changes will be lost!"]; + [alert setAlertStyle:NSWarningAlertStyle]; + if ([alert runModal] == NSAlertAlternateReturn) // cancel { return; } } + [self.managedObjectContext reset]; [grantedSchemaPrivs removeAllObjects]; [grantedTableView reloadData]; [self _initializeAvailablePrivs]; [treeController fetch:nil]; - } - (void)_setSchemaPrivValues:(NSArray *)objects enabled:(BOOL)enabled @@ -770,8 +788,8 @@ NSMutableSet *privs = [selectedHost mutableSetValueForKey:@"schema_privileges"]; [privs addObject:priv]; } - } + - (void)_clearData { [managedObjectContext reset]; @@ -779,6 +797,9 @@ managedObjectContext = nil; } +/** + * Menu item validation. + */ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { // Only allow removing hosts of a host node is selected. @@ -881,7 +902,8 @@ - (BOOL)updateUsers:(NSArray *)updatedUsers { - for (NSManagedObject *user in updatedUsers) { + for (NSManagedObject *user in updatedUsers) + { if ([[[user entity] name] isEqualToString:@"Privileges"]) { [self grantDbPrivilegesWithPrivilege:user]; @@ -905,7 +927,6 @@ } else { [self grantPrivilegesToUser:user]; } - } return YES; @@ -914,6 +935,7 @@ - (BOOL)deleteUsers:(NSArray *)deletedUsers { NSMutableString *droppedUsers = [NSMutableString string]; + for (NSManagedObject *user in deletedUsers) { if (![[[user entity] name] isEqualToString:@"Privileges"] && [user valueForKey:@"host"] != nil) @@ -924,9 +946,11 @@ } } + droppedUsers = [[droppedUsers substringToIndex:[droppedUsers length]-2] mutableCopy]; [self.mySqlConnection queryString:[NSString stringWithFormat:@"DROP USER %@", droppedUsers]]; [droppedUsers release]; + return TRUE; } @@ -966,14 +990,13 @@ if ([self checkAndDisplayMySqlError]) [self grantPrivilegesToUser:user]; } - } + return YES; } /** - * Grant or revoke db privileges to the given user - * + * Grant or revoke DB privileges for the supplied user. */ - (BOOL)grantDbPrivilegesWithPrivilege:(NSManagedObject *)schemaPriv { @@ -981,6 +1004,7 @@ NSMutableArray *revokePrivileges = [NSMutableArray array]; NSString *dbName = [schemaPriv valueForKey:@"db"]; + for (NSString *key in self.privsSupportedByServer) { if (![key hasSuffix:@"_priv"]) continue; @@ -997,6 +1021,7 @@ @catch (NSException * e) { } } + // Grant privileges if ([grantPrivileges count] > 0) { @@ -1021,11 +1046,13 @@ DLog(@"%@", revokeStatement); [self.mySqlConnection queryString:revokeStatement]; [self checkAndDisplayMySqlError]; - } + } + return TRUE; } + /** - * Grant or revoke privileges to the given user + * Grant or revoke privileges for the supplied user. */ - (BOOL)grantPrivilegesToUser:(NSManagedObject *)user { @@ -1052,6 +1079,7 @@ @catch (NSException * e) { } } + // Grant privileges if ([grantPrivileges count] > 0) { @@ -1077,7 +1105,7 @@ } } - for(NSManagedObject *priv in [user valueForKey:@"schema_privileges"]) + for (NSManagedObject *priv in [user valueForKey:@"schema_privileges"]) { [self grantDbPrivilegesWithPrivilege:priv]; } @@ -1085,8 +1113,10 @@ return TRUE; } -// Gets any NSManagedObject (SPUser) from the managedObjectContext that may -// already exist with the given username. +/** + * Gets any NSManagedObject (SPUser) from the managedObjectContext that may + * already exist with the given username. + */ - (NSArray *)_fetchUserWithUserName:(NSString *)username { NSManagedObjectContext *moc = [self managedObjectContext]; @@ -1100,6 +1130,7 @@ NSError *error = nil; NSArray *array = [moc executeFetchRequest:request error:&error]; + if (error != nil) { [[NSApplication sharedApplication] presentError:error]; @@ -1120,15 +1151,18 @@ NSError *error = nil; NSArray *array = [moc executeFetchRequest:request error:&error]; + if (error != nil) { [[NSApplication sharedApplication] presentError:error]; } - return array; + return array; } -// Creates a new NSManagedObject and inserts it into the managedObjectContext. +/** + * Creates a new NSManagedObject and inserts it into the managedObjectContext. + */ - (NSManagedObject *)_createNewSPUser { NSManagedObject *user = [NSEntityDescription insertNewObjectForEntityForName:@"SPUser" @@ -1137,7 +1171,9 @@ return user; } -// Displays alert panel if there is an error condition currently on the mySqlConnection +/** + * Displays an alert panel if there was an error condition on the MySQL connection. + */ - (BOOL)checkAndDisplayMySqlError { if (![[self.mySqlConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1208,14 +1244,18 @@ [grantedSchemaPrivs removeAllObjects]; [grantedTableView reloadData]; [self _initializeAvailablePrivs]; + if ([[treeController selectedObjects] count] > 0 && [[schemaController selectedObjects] count] > 0) { NSManagedObject *user = [[treeController selectedObjects] objectAtIndex:0]; + // Check to see if the user host node was selected if ([user valueForKey:@"host"]) { NSString *selectedSchema = [[[schemaController selectedObjects] objectAtIndex:0] valueForKey:@"Database"]; NSArray *results = [self _fetchPrivsWithUser:[[user parent] valueForKey:@"user"] schema:selectedSchema host:[user valueForKey:@"host"]]; + if ([results count] > 0) { NSManagedObject *priv = [results objectAtIndex:0]; + for (NSPropertyDescription *property in [priv entity]) { if ([[property name] hasSuffix:@"_priv"] && [[priv valueForKey:[property name]] boolValue]) @@ -1266,4 +1306,28 @@ } } +#pragma mark - + +/** + * Dealloc. Get rid of everything. + */ +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self + name:NSManagedObjectContextDidSaveNotification + object:nil]; + [managedObjectContext release]; + [persistentStoreCoordinator release]; + [managedObjectModel release]; + [privColumnToGrantMap release]; + [mySqlConnection release]; + [privsSupportedByServer release]; + [schemas release]; + [availablePrivs release]; + [grantedSchemaPrivs release]; + [treeSortDescriptor release]; + + [super dealloc]; +} + @end |