1 /* UIxFolderActions.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSDictionary.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSUserDefaults.h>
26 #import <Foundation/NSURL.h>
27 #import <Foundation/NSValue.h>
29 #import <NGObjWeb/WOContext.h>
30 #import <NGObjWeb/WOContext+SoObjects.h>
31 #import <NGObjWeb/WORequest.h>
32 #import <NGObjWeb/WOResponse.h>
33 #import <NGObjWeb/SoSecurityManager.h>
34 #import <NGObjWeb/SoClassSecurityInfo.h>
36 #import <SoObjects/SOGo/LDAPUserManager.h>
37 #import <SoObjects/SOGo/NSArray+Utilities.h>
38 #import <SoObjects/SOGo/SOGoUser.h>
39 #import <SoObjects/SOGo/SOGoFolder.h>
40 #import <SoObjects/SOGo/SOGoPermissions.h>
42 #import "WODirectAction+SOGo.h"
44 #import "UIxFolderActions.h"
46 @implementation UIxFolderActions
48 #warning some of this code could probably be moved in one of the \
49 clientObject classes...
51 - (void) _setupContext
53 NSString *folder, *mailInvitationParam;
54 NSArray *realFolderPath;
57 activeUser = [context activeUser];
58 login = [activeUser login];
59 clientObject = [self clientObject];
60 owner = [clientObject ownerInContext: nil];
62 baseFolder = [[clientObject container] nameInContainer];
64 um = [LDAPUserManager sharedUserManager];
65 ud = [activeUser userSettings];
66 moduleSettings = [ud objectForKey: baseFolder];
69 moduleSettings = [NSMutableDictionary new];
70 [moduleSettings autorelease];
72 [ud setObject: moduleSettings forKey: baseFolder];
74 realFolderPath = [[clientObject nameInContainer]
75 componentsSeparatedByString: @"_"];
76 if ([realFolderPath count] > 1)
77 folder = [realFolderPath objectAtIndex: 1];
79 folder = [realFolderPath objectAtIndex: 0];
80 subscriptionPointer = [NSString stringWithFormat: @"%@:%@/%@",
81 owner, baseFolder, folder];
84 = [[context request] formValueForKey: @"mail-invitation"];
85 isMailInvitation = [mailInvitationParam boolValue];
88 - (WOResponse *) _realSubscribe: (BOOL) reallyDo
91 NSMutableArray *folderSubscription;
92 NSString *mailInvitationURL;
94 if ([owner isEqualToString: login])
96 response = [self responseWithStatus: 403];
97 [response appendContentString:
98 @"You cannot (un)subscribe to a folder that you own!"];
103 = [moduleSettings objectForKey: @"SubscribedFolders"];
104 if (!(folderSubscription
105 && [folderSubscription isKindOfClass: [NSMutableArray class]]))
107 folderSubscription = [NSMutableArray array];
108 [moduleSettings setObject: folderSubscription
109 forKey: @"SubscribedFolders"];
112 [folderSubscription addObjectUniquely: subscriptionPointer];
114 [folderSubscription removeObject: subscriptionPointer];
118 if (isMailInvitation)
121 = [[clientObject soURLToBaseContainerForCurrentUser]
123 response = [self responseWithStatus: 302];
124 [response setHeader: mailInvitationURL
125 forKey: @"location"];
128 response = [self responseWith204];
134 - (WOResponse *) subscribeAction
136 [self _setupContext];
138 return [self _realSubscribe: YES];
141 - (WOResponse *) unsubscribeAction
143 [self _setupContext];
145 return [self _realSubscribe: NO];
148 - (WOResponse *) canAccessContentAction
150 #warning IMPROVEMENTS REQUIRED!
152 // NSEnumerator *userAcls;
153 // NSString *currentAcl;
155 [self _setupContext];
157 // NSLog(@"canAccessContentAction %@, owner %@", subscriptionPointer, owner);
159 if ([login isEqualToString: owner] || [owner isEqualToString: @"nobody"]) {
160 return [self responseWith204];
163 acls = [clientObject aclsForUser: login];
164 // userAcls = [acls objectEnumerator];
165 // currentAcl = [userAcls nextObject];
166 // while (currentAcl) {
167 // NSLog(@"ACL login %@, owner %@, folder %@: %@",
168 // login, owner, baseFolder, currentAcl);
169 // currentAcl = [userAcls nextObject];
171 if (([[clientObject folderType] isEqualToString: @"Contact"] && [acls containsObject: SOGoRole_ObjectReader]) ||
172 ([[clientObject folderType] isEqualToString: @"Appointment"] && [acls containsObject: SOGoRole_AuthorizedSubscriber])) {
173 return [self responseWith204];
177 return [self responseWithStatus: 403];
180 - (WOResponse *) _realFolderActivation: (BOOL) makeActive
182 NSMutableArray *folderSubscription;
183 NSString *folderName;
185 [self _setupContext];
187 = [moduleSettings objectForKey: @"ActiveFolders"];
188 if (!folderSubscription)
190 folderSubscription = [NSMutableArray array];
191 [moduleSettings setObject: folderSubscription forKey: @"ActiveFolders"];
194 folderName = [clientObject nameInContainer];
196 [folderSubscription addObjectUniquely: folderName];
198 [folderSubscription removeObject: folderName];
202 return [self responseWith204];
205 - (WOResponse *) activateFolderAction
207 return [self _realFolderActivation: YES];
210 - (WOResponse *) deactivateFolderAction
212 return [self _realFolderActivation: NO];
215 - (WOResponse *) deleteFolderAction
217 WOResponse *response;
219 response = (WOResponse *) [[self clientObject] delete];
221 response = [self responseWith204];
226 - (WOResponse *) renameFolderAction
228 WOResponse *response;
229 NSString *folderName;
231 folderName = [[context request] formValueForKey: @"name"];
232 if ([folderName length] > 0)
234 clientObject = [self clientObject];
235 [clientObject renameTo: folderName];
236 response = [self responseWith204];
240 response = [self responseWithStatus: 500];
241 [response appendContentString: @"Missing 'name' parameter."];
247 - (id) batchDeleteAction
249 WOResponse *response;
253 idsParam = [[context request] formValueForKey: @"ids"];
254 ids = [idsParam componentsSeparatedByString: @"/"];
257 clientObject = [self clientObject];
258 [clientObject deleteEntriesWithIds: ids];
259 response = [self responseWith204];
263 response = [self responseWithStatus: 500];
264 [response appendContentString: @"At least 1 id required."];