]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxFolderActions.m
Install libs to /usr/lib
[scalable-opengroupware.org] / UI / Common / UIxFolderActions.m
1 /* UIxFolderActions.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
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>
28
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>
35
36 #import <SoObjects/SOGo/LDAPUserManager.h>
37 #import <SoObjects/SOGo/NSArray+Utilities.h>
38 #import <SoObjects/SOGo/SOGoGCSFolder.h>
39 #import <SoObjects/SOGo/SOGoPermissions.h>
40 #import <SoObjects/SOGo/SOGoUser.h>
41
42 #import "WODirectAction+SOGo.h"
43
44 #import "UIxFolderActions.h"
45
46 @implementation UIxFolderActions
47
48 #warning some of this code could probably be moved in one of the \
49          clientObject classes...
50
51 - (void) _setupContext
52 {
53   NSString *folder, *mailInvitationParam;
54   NSArray *realFolderPath;
55   SOGoUser *activeUser;
56
57   activeUser = [context activeUser];
58   login = [activeUser login];
59   clientObject = [self clientObject];
60   owner = [clientObject ownerInContext: nil];
61
62   baseFolder = [[clientObject container] nameInContainer];
63
64   um = [LDAPUserManager sharedUserManager];
65   ud = [activeUser userSettings];
66   moduleSettings = [ud objectForKey: baseFolder];
67   if (!moduleSettings)
68     {
69       moduleSettings = [NSMutableDictionary new];
70       [moduleSettings autorelease];
71     }
72   [ud setObject: moduleSettings forKey: baseFolder];
73
74   realFolderPath = [[clientObject nameInContainer]
75                      componentsSeparatedByString: @"_"];
76   if ([realFolderPath count] > 1)
77     folder = [realFolderPath objectAtIndex: 1];
78   else
79     folder = [realFolderPath objectAtIndex: 0];
80   subscriptionPointer = [NSString stringWithFormat: @"%@:%@/%@",
81                                   owner, baseFolder, folder];
82
83   mailInvitationParam
84     = [[context request] formValueForKey: @"mail-invitation"];
85   isMailInvitation = [mailInvitationParam boolValue];
86 }
87
88 - (WOResponse *) _realSubscribe: (BOOL) reallyDo
89 {
90   WOResponse *response;
91   NSMutableArray *folderSubscription;
92   NSString *mailInvitationURL;
93
94   if ([owner isEqualToString: login])
95     {
96       response = [self responseWithStatus: 403];
97       [response appendContentString:
98                  @"You cannot (un)subscribe to a folder that you own!"];
99     }
100   else
101     {
102       folderSubscription
103         = [moduleSettings objectForKey: @"SubscribedFolders"];
104       if (!(folderSubscription
105             && [folderSubscription isKindOfClass: [NSMutableArray class]]))
106         {
107           folderSubscription = [NSMutableArray array];
108           [moduleSettings setObject: folderSubscription
109                           forKey: @"SubscribedFolders"];
110         }
111       if (reallyDo)
112         [folderSubscription addObjectUniquely: subscriptionPointer];
113       else
114         [folderSubscription removeObject: subscriptionPointer];
115
116       [ud synchronize];
117
118       if (isMailInvitation)
119         {
120           mailInvitationURL
121             = [[clientObject soURLToBaseContainerForCurrentUser]
122                 absoluteString];
123           response = [self responseWithStatus: 302];
124           [response setHeader: mailInvitationURL
125                     forKey: @"location"];
126         }
127       else
128         response = [self responseWith204];
129     }
130
131   return response;
132 }
133
134 - (WOResponse *) subscribeAction
135 {
136   [self _setupContext];
137
138   return [self _realSubscribe: YES];
139 }
140
141 - (WOResponse *) unsubscribeAction
142 {
143   [self _setupContext];
144
145   return [self _realSubscribe: NO];
146 }
147
148 - (WOResponse *) canAccessContentAction
149 {
150   /* We want this action to be authorized managed by the SOPE's internal acl
151      handling. */
152   return [self responseWith204];
153 // #warning IMPROVEMENTS REQUIRED!
154 //   NSArray *acls;
155 // //  NSEnumerator *userAcls;
156 // //  NSString *currentAcl;
157
158 //   [self _setupContext];
159   
160 // //  NSLog(@"canAccessContentAction %@, owner %@", subscriptionPointer, owner);
161
162 //   if ([login isEqualToString: owner] || [owner isEqualToString: @"nobody"]) {
163 //     return [self responseWith204];
164 //   }
165 //   else {
166 //     acls = [clientObject aclsForUser: login];
167 // //    userAcls = [acls objectEnumerator];
168 // //    currentAcl = [userAcls nextObject];
169 // //    while (currentAcl) {
170 // //      NSLog(@"ACL login %@, owner %@, folder %@: %@",
171 // //       login, owner, baseFolder, currentAcl);
172 // //      currentAcl = [userAcls nextObject];
173 // //    }
174 //     if (([[clientObject folderType] isEqualToString: @"Contact"]
175 //       && [acls containsObject: SOGoRole_ObjectViewer]) ||
176 //      ([[clientObject folderType] isEqualToString: @"Appointment"]
177 //       && [acls containsObject: SOGoRole_AuthorizedSubscriber])) {
178 //       return [self responseWith204];
179 //     }
180 //   }
181   
182 //   return [self responseWithStatus: 403];
183 }
184
185 - (WOResponse *) _realFolderActivation: (BOOL) makeActive
186 {
187   NSMutableArray *folderSubscription;
188   NSString *folderName;
189
190   [self _setupContext];
191   folderSubscription
192     = [moduleSettings objectForKey: @"ActiveFolders"];
193   if (!folderSubscription)
194     {
195       folderSubscription = [NSMutableArray array];
196       [moduleSettings setObject: folderSubscription forKey: @"ActiveFolders"];
197     }
198
199   folderName = [clientObject nameInContainer];
200   if (makeActive)
201     [folderSubscription addObjectUniquely: folderName];
202   else
203     [folderSubscription removeObject: folderName];
204
205   [ud synchronize];
206
207   return [self responseWith204];
208 }
209
210 - (WOResponse *) activateFolderAction
211 {
212   return [self _realFolderActivation: YES];
213 }
214
215 - (WOResponse *) deactivateFolderAction
216 {
217   return [self _realFolderActivation: NO];
218 }
219
220 - (WOResponse *) deleteFolderAction
221 {
222   WOResponse *response;
223
224   response = (WOResponse *) [[self clientObject] delete];
225   if (!response)
226     response = [self responseWith204];
227
228   return response;
229 }
230
231 - (WOResponse *) renameFolderAction
232 {
233   WOResponse *response;
234   NSString *folderName;
235
236   folderName = [[context request] formValueForKey: @"name"];
237   if ([folderName length] > 0)
238     {
239       clientObject = [self clientObject];
240       [clientObject renameTo: folderName];
241       response = [self responseWith204];
242     }
243   else
244     {
245       response = [self responseWithStatus: 500];
246       [response appendContentString: @"Missing 'name' parameter."];
247     }
248
249   return response;
250 }
251
252 - (id) batchDeleteAction
253 {
254   WOResponse *response;
255   NSString *idsParam;
256   NSArray *ids;
257
258   idsParam = [[context request] formValueForKey: @"ids"];
259   ids = [idsParam componentsSeparatedByString: @"/"];
260   if ([ids count])
261     {
262       clientObject = [self clientObject];
263       [clientObject deleteEntriesWithIds: ids];
264       response = [self responseWith204];
265     }
266   else
267     {
268       response = [self responseWithStatus: 500];
269       [response appendContentString: @"At least 1 id required."];
270     }
271   
272   return response;
273 }
274
275 @end