diff --git a/ChangeLog b/ChangeLog index 6915ae3a5..efb863ebf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2010-04-15 Wolfgang Sourdeau + * Tools/SOGoToolRemove.m: new sogo-tool utility to remove user + data from the database. + * Tools/SOGoSockDOperation.m (_appendEntry:toResult:): include "telephonenumber" in the entry information. diff --git a/NEWS b/NEWS index f7afcb509..d4cc828d6 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,7 @@ - removed remaining .wo templates, thereby easing the effort for future translations - fixed regressions with Courier IMAP and Dovecot - added ability to delete events from a keypress +- added the "remove" command to "sogo-tool", in order to remove user data and settings 1.2-20100219 (1.2.1) -------------------- diff --git a/Tools/SOGoToolRemove.m b/Tools/SOGoToolRemove.m new file mode 100644 index 000000000..77af43cf2 --- /dev/null +++ b/Tools/SOGoToolRemove.m @@ -0,0 +1,177 @@ +/* SOGoToolRemove.m - this file is part of SOGo + * + * Copyright (C) 2010 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#import +#import +#import + +#import + +#import +#import +#import + +#import + +#import "SOGoTool.h" + +/* TODO: + - handle database connectivity errors +*/ + +static GCSFolderManager *fm = nil; +static NSURL *tableURL = nil; + +@interface SOGoToolRemove : SOGoTool +@end + +@implementation SOGoToolRemove + ++ (void) initialize +{ + NSString *profileURL; + SOGoSystemDefaults *sd; + + if (!fm) + fm = [GCSFolderManager defaultFolderManager]; + + if (!tableURL) + { + sd = [SOGoSystemDefaults sharedSystemDefaults]; + profileURL = [sd profileURL]; + if (profileURL) + tableURL = [[NSURL alloc] initWithString: profileURL]; + } +} + ++ (NSString *) command +{ + return @"remove"; +} + ++ (NSString *) description +{ + return @"remove user data and settings from the db"; +} + +- (void) usage +{ + fprintf (stderr, "remove user1 [user2] ...\n\n" + " user the user of whom to remove the data\n"); +} + +- (NSArray *) _userFolderPaths: (NSString *) userId +{ + GCSChannelManager *cm; + EOAdaptorChannel *fc; + NSURL *folderLocation; + NSString *sql; + NSArray *attrs; + NSDictionary *row; + NSMutableArray *paths; + + paths = [NSMutableArray arrayWithCapacity: 16]; + + folderLocation = [fm folderInfoLocation]; + cm = [GCSChannelManager defaultChannelManager]; + fc = [cm acquireOpenChannelForURL: folderLocation]; + if ([fc isOpen]) + { + sql + = [NSString stringWithFormat: (@"SELECT c_path FROM %@" + @" WHERE c_path2 = '%@'"), + [folderLocation gcsTableName], + userId]; + if (![fc evaluateExpressionX: sql]) + { + attrs = [fc describeResults: NO]; + while ((row = [fc fetchAttributes: attrs withZone: NULL])) + [paths addObject: [row objectForKey: @"c_path"]]; + } + [cm releaseChannel: fc]; + } + + return paths; +} + +- (void) _removeUserFolders: (NSString *) userId +{ + NSArray *folderPaths; + int count, max; + + folderPaths = [self _userFolderPaths: userId]; + max = [folderPaths count]; + if (max > 0) + for (count = 0; count < max; count++) + [fm deleteFolderAtPath: [folderPaths objectAtIndex: count]]; + else + NSLog (@"No folder returned for user '%@'", userId); +} + +- (void) _removeUserPreferences: (NSString *) userId +{ + GCSChannelManager *cm; + EOAdaptorChannel *fc; + NSString *sql; + + cm = [GCSChannelManager defaultChannelManager]; + fc = [cm acquireOpenChannelForURL: tableURL]; + if ([fc isOpen]) + { + sql + = [NSString stringWithFormat: (@"DELETE FROM %@" + @" WHERE c_uid = '%@'"), + [tableURL gcsTableName], + userId]; + if ([fc evaluateExpressionX: sql]) + NSLog (@"Unable to delete the preference record for '%@'", userId); + [cm releaseChannel: fc]; + } +} + +- (BOOL) run +{ + NSString *userId; + int count, max; + BOOL rc; + + max = [arguments count]; + if (max > 0) + { + for (count = 0; count < max; count++) + { + userId = [arguments objectAtIndex: count]; + [self _removeUserFolders: userId]; + [self _removeUserPreferences: userId]; + } + rc = YES; + } + else + { + [self usage]; + rc = NO; + } + + return rc; +} + +@end