]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxFolderActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1236 d1b88da0-ebda-0310...
[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/SOGoUser.h>
39 #import <SoObjects/SOGo/SOGoFolder.h>
40 #import <SoObjects/SOGo/SOGoPermissions.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 #warning IMPROVEMENTS REQUIRED!
151   NSArray *acls;
152 //  NSEnumerator *userAcls;
153 //  NSString *currentAcl;
154
155   [self _setupContext];
156   
157 //  NSLog(@"canAccessContentAction %@, owner %@", subscriptionPointer, owner);
158
159   if ([login isEqualToString: owner] || [owner isEqualToString: @"nobody"]) {
160     return [self responseWith204];
161   }
162   else {
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];
170 //    }
171     if (([[clientObject folderType] isEqualToString: @"Contact"]     && [acls containsObject: SOGoRole_ObjectReader]) ||
172         ([[clientObject folderType] isEqualToString: @"Appointment"] && [acls containsObject: SOGoRole_AuthorizedSubscriber])) {
173       return [self responseWith204];
174     }
175   }
176   
177   return [self responseWithStatus: 403];
178 }
179
180 - (WOResponse *) _realFolderActivation: (BOOL) makeActive
181 {
182   NSMutableArray *folderSubscription;
183   NSString *folderName;
184
185   [self _setupContext];
186   folderSubscription
187     = [moduleSettings objectForKey: @"ActiveFolders"];
188   if (!folderSubscription)
189     {
190       folderSubscription = [NSMutableArray array];
191       [moduleSettings setObject: folderSubscription forKey: @"ActiveFolders"];
192     }
193
194   folderName = [clientObject nameInContainer];
195   if (makeActive)
196     [folderSubscription addObjectUniquely: folderName];
197   else
198     [folderSubscription removeObject: folderName];
199
200   [ud synchronize];
201
202   return [self responseWith204];
203 }
204
205 - (WOResponse *) activateFolderAction
206 {
207   return [self _realFolderActivation: YES];
208 }
209
210 - (WOResponse *) deactivateFolderAction
211 {
212   return [self _realFolderActivation: NO];
213 }
214
215 - (WOResponse *) deleteFolderAction
216 {
217   WOResponse *response;
218
219   response = (WOResponse *) [[self clientObject] delete];
220   if (!response)
221     response = [self responseWith204];
222
223   return response;
224 }
225
226 - (WOResponse *) renameFolderAction
227 {
228   WOResponse *response;
229   NSString *folderName;
230
231   folderName = [[context request] formValueForKey: @"name"];
232   if ([folderName length] > 0)
233     {
234       clientObject = [self clientObject];
235       [clientObject renameTo: folderName];
236       response = [self responseWith204];
237     }
238   else
239     {
240       response = [self responseWithStatus: 500];
241       [response appendContentString: @"Missing 'name' parameter."];
242     }
243
244   return response;
245 }
246
247 - (id) batchDeleteAction
248 {
249   WOResponse *response;
250   NSString *idsParam;
251   NSArray *ids;
252
253   idsParam = [[context request] formValueForKey: @"ids"];
254   ids = [idsParam componentsSeparatedByString: @"/"];
255   if ([ids count])
256     {
257       clientObject = [self clientObject];
258       [clientObject deleteEntriesWithIds: ids];
259       response = [self responseWith204];
260     }
261   else
262     {
263       response = [self responseWithStatus: 500];
264       [response appendContentString: @"At least 1 id required."];
265     }
266   
267   return response;
268 }
269
270 @end