From 5e84f2817f8ac3e24ad1a9d6c8085fe4d48c44cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Amor=20Garc=C3=ADa?= Date: Wed, 27 May 2015 16:41:22 +0200 Subject: [PATCH 1/2] sogo-tool: Added create-folder command --- Tools/GNUmakefile | 1 + Tools/SOGoToolCreateFolder.m | 175 +++++++++++++++++++++++++++++++++++ Tools/SOGoToolRestore.h | 48 ++++++++++ Tools/SOGoToolRestore.m | 22 +---- 4 files changed, 226 insertions(+), 20 deletions(-) create mode 100644 Tools/SOGoToolCreateFolder.m create mode 100644 Tools/SOGoToolRestore.h diff --git a/Tools/GNUmakefile b/Tools/GNUmakefile index 869fb52c6..32f9119e4 100644 --- a/Tools/GNUmakefile +++ b/Tools/GNUmakefile @@ -22,6 +22,7 @@ $(SOGO_TOOL)_OBJC_FILES += \ SOGoToolRemoveDoubles.m \ SOGoToolRenameUser.m \ SOGoToolRestore.m \ + SOGoToolCreateFolder.m \ SOGoToolUserPreferences.m \ SOGoToolManageEAS.m TOOL_NAME += $(SOGO_TOOL) diff --git a/Tools/SOGoToolCreateFolder.m b/Tools/SOGoToolCreateFolder.m new file mode 100644 index 000000000..423ec7266 --- /dev/null +++ b/Tools/SOGoToolCreateFolder.m @@ -0,0 +1,175 @@ +/* SOGoToolCreateFolder.m - this file is part of SOGo + * Implementation of create-folder command for sogo-tool + * + * Copyright (C) 2015 Javier Amor Garcia + * + * 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 "SOGoToolRestore.h" + +@interface SOGoToolCreateFolder : SOGoToolRestore +{ + NSMutableArray *foldersContents; + NSString *folderType; +} + +@end + +@implementation SOGoToolCreateFolder + ++ (NSString *) command +{ + return @"create-folder"; +} + ++ (NSString *) description +{ + return @"create folders for a user"; +} + +- (id) init +{ + if ((self = [super init])) + { + foldersContents = nil; + folderType = nil; + } + + return self; +} + +- (void) dealloc +{ + [foldersContents release]; + [folderType release]; + [super dealloc]; +} + +- (void) usage +{ + fprintf (stderr, "create-folder user type [displayname ...]\n\n" + " user the user on which the folder(s) will be created\n" + " type type of directory. Calendar or Contacts\n" + " displayname display name(s) for the created folder(s)\n\n" + "Examples: sogo-tool create-folder user1 Calendar cal1 cal2\n" + " sogo-tool create-folder user1 Contacts agenda\n"); +} + +- (BOOL) createNewFolderOfType: (NSString *) type + withContent: (NSDictionary *) content +{ + NSString *folder; + NSString *guid; + GCSFolderManager *fm; + GCSFolder *gcsFolder; + BOOL rc; + + guid = [SOGoObject globallyUniqueObjectId]; + folder= [NSString stringWithFormat: @"/Users/%@/%@/%@", + userID, type, guid]; + fm = [GCSFolderManager defaultFolderManager]; + + rc = [self createFolder: folder withFM: fm]; + if (!rc) + { + NSLog (@"Create directory failed at path %s", folder); + return NO; + } + + gcsFolder = [fm folderAtPath: folder]; + if (!gcsFolder) + { + NSLog (@"folder '%@' could not be created", folder); + return NO; + } + + rc = [self restoreDisplayName: [content objectForKey: @"displayname"] + ofFolder: gcsFolder + withFM: fm]; + return rc; +} + +- (BOOL) proceed +{ + BOOL rc; + NSUInteger count, i; + NSDictionary * content; + + rc = YES; + count = [foldersContents count]; + for (i = 0; i < count; i++) + { + content = [foldersContents objectAtIndex: i]; + if (![self createNewFolderOfType: folderType withContent: content]) + { + rc = NO; + } + } + + return rc; +} + +- (BOOL) parseArguments +{ + NSString *identifier; + NSUInteger count, i; + NSDictionary *content; + + count = [arguments count]; + if (count < 3) + { + [self usage]; + return NO; + } + + identifier = [arguments objectAtIndex: 0]; + if (![self fetchUserID: identifier]) + { + fprintf (stderr, "Invalid user:%s\n", [identifier cString]); + return NO; + } + + folderType = [arguments objectAtIndex: 1]; + if (!([folderType isEqualToString: @"Contacts"] || [folderType isEqualToString: @"Calendar"])) + { + fprintf (stderr, "Invalid folder type:%s\n", [folderType cString]); + return NO; + } + + foldersContents = [[NSMutableArray alloc] init]; + for (i = 2; i < count; i++) + { + content = [NSDictionary dictionaryWithObject: [arguments objectAtIndex: i] + forKey: @"displayname"]; + [foldersContents addObject: content]; + } + + return YES; +} + +@end diff --git a/Tools/SOGoToolRestore.h b/Tools/SOGoToolRestore.h new file mode 100644 index 000000000..f5be03928 --- /dev/null +++ b/Tools/SOGoToolRestore.h @@ -0,0 +1,48 @@ +/* SOGoToolRestore.h - this file is part of SOGo + * Header to allow subclassing of SOGoToolRestore class + * + * Copyright (C) 2015 Javier Amor Garcia + * + * 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 "SOGoTool.h" + +typedef enum SOGoToolRestoreMode { + SOGoToolRestoreFolderMode, + SOGoToolRestoreFolderDestructiveMode, + SOGoToolRestoreListFoldersMode, + SOGoToolRestorePreferencesMode +} SOGoToolRestoreMode; + +@interface SOGoToolRestore : SOGoTool +{ + NSString *directory; + NSString *userID; + NSString *restoreFolder; + BOOL destructive; /* destructive mode not handled */ + SOGoToolRestoreMode restoreMode; +} + +- (BOOL) fetchUserID: (NSString *) identifier; +- (BOOL) createFolder: (NSString *) folder + withFM: (GCSFolderManager *) fm; +- (BOOL) restoreDisplayName: (NSString *) newDisplayName + ofFolder: (GCSFolder *) gcsFolder + withFM: (GCSFolderManager *) fm; + +@end diff --git a/Tools/SOGoToolRestore.m b/Tools/SOGoToolRestore.m index d3fb601f9..54e3179bd 100644 --- a/Tools/SOGoToolRestore.m +++ b/Tools/SOGoToolRestore.m @@ -45,32 +45,14 @@ #import #import -#import "SOGoTool.h" +#import "SOGoToolRestore.h" /* TODO: - respond to "--help restore" - handle database connectivity errors - handle the case where the restored folder has been deleted - write methods in GDLContentStore to get/update displayname - and storing roles */ - -typedef enum SOGoToolRestoreMode { - SOGoToolRestoreFolderMode, - SOGoToolRestoreFolderDestructiveMode, - SOGoToolRestoreListFoldersMode, - SOGoToolRestorePreferencesMode -} SOGoToolRestoreMode; - -@interface SOGoToolRestore : SOGoTool -{ - NSString *directory; - NSString *userID; - NSString *restoreFolder; - BOOL destructive; /* destructive mode not handled */ - SOGoToolRestoreMode restoreMode; -} - -@end + and storing roles */ @implementation SOGoToolRestore From d92b0341fbd5aa8f7b711a4cec9198dc34ca899c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20S=C3=A1ez?= Date: Thu, 28 May 2015 17:28:52 +0200 Subject: [PATCH 2/2] sogo-tool restore works in multidomain environments c_uid is not unique globally when multidomain is enabled --- Tools/SOGoToolRestore.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Tools/SOGoToolRestore.m b/Tools/SOGoToolRestore.m index 54e3179bd..a0f411bd9 100644 --- a/Tools/SOGoToolRestore.m +++ b/Tools/SOGoToolRestore.m @@ -40,6 +40,7 @@ #import #import #import +#import #import #import @@ -154,10 +155,22 @@ BOOL rc; SOGoUserManager *lm; NSDictionary *infos; + SOGoSystemDefaults *sd; + NSString *uid = nil; lm = [SOGoUserManager sharedUserManager]; infos = [lm contactInfosForUserWithUIDorEmail: identifier]; - ASSIGN (userID, [infos objectForKey: @"c_uid"]); + if (infos) + { + sd = [SOGoSystemDefaults sharedSystemDefaults]; + if ([sd enableDomainBasedUID]) + uid = [NSString stringWithFormat: @"%@@%@", + [infos objectForKey: @"c_uid"], + [infos objectForKey: @"c_domain"]]; + else + uid = [infos objectForKey: @"c_uid"]; + } + ASSIGN (userID, uid); if (userID) rc = YES; else