File: UIxFolderActions.m

package info (click to toggle)
sogo 2.2.9%2Bgit20141017-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 43,812 kB
  • sloc: objc: 115,592; python: 5,696; sh: 1,380; perl: 861; makefile: 240; xml: 114; sql: 53; php: 43
file content (395 lines) | stat: -rw-r--r-- 10,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* UIxFolderActions.m - this file is part of SOGo
 *
 * Copyright (C) 2007-2013 Inverse inc.
 *
 * 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 <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSString.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>

#import <NGObjWeb/WOContext.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/WOResponse.h>
#import <NGObjWeb/SoSecurityManager.h>
#import <NGObjWeb/SoClassSecurityInfo.h>
#import <NGObjWeb/NSException+HTTP.h>

#import <SoObjects/SOGo/SOGoUserManager.h>
#import <SoObjects/SOGo/NSArray+Utilities.h>
#import <SoObjects/SOGo/SOGoContentObject.h>
#import <SoObjects/SOGo/SOGoGCSFolder.h>
#import <SoObjects/SOGo/SOGoParentFolder.h>
#import <SoObjects/SOGo/SOGoPermissions.h>
#import <SoObjects/SOGo/SOGoUser.h>
#import <SoObjects/SOGo/SOGoUserSettings.h>

#import "WODirectAction+SOGo.h"

#import "UIxFolderActions.h"

@implementation UIxFolderActions

- (void) _setupContext
{
  NSString *mailInvitationParam;
  SOGoUser *activeUser;

  activeUser = [context activeUser];
  login = [activeUser login];
  clientObject = [self clientObject];
  owner = [clientObject ownerInContext: nil];

  baseFolder = [[clientObject container] nameInContainer];

  um = [SOGoUserManager sharedUserManager];
  us = [activeUser userSettings];
  moduleSettings = [us objectForKey: baseFolder];
  if (!moduleSettings)
    moduleSettings = [NSMutableDictionary dictionary];
  [us setObject: moduleSettings forKey: baseFolder];

  mailInvitationParam
    = [[context request] formValueForKey: @"mail-invitation"];
  isMailInvitation = [mailInvitationParam boolValue];
}

- (WOResponse *) _subscribeAction: (BOOL) reallyDo
{
  WOResponse *response;
  NSURL *mailInvitationURL;

  response = [context response];
  [response setHeader: @"text/plain; charset=utf-8"
               forKey: @"Content-Type"];

  [self _setupContext];
  if ([owner isEqualToString: login])
    {
      [response setStatus: 403];
      [response appendContentString:
                  @"You cannot (un)subscribe to a folder that you own!"];
    }
  else
    {
      [clientObject subscribeUserOrGroup: login
                                reallyDo: reallyDo
                                response: response];

      if (isMailInvitation)
        {
          mailInvitationURL
            = [clientObject soURLToBaseContainerForCurrentUser];
          [response setStatus: 302];
          [response setHeader: [mailInvitationURL absoluteString]
                       forKey: @"location"];
        }
      else
        [response setStatus: 204];
    }

  return response;
}

- (WOResponse *) subscribeAction
{
  return [self _subscribeAction: YES];
}

- (WOResponse *) unsubscribeAction
{
  return [self _subscribeAction: NO];
}

- (WOResponse *) canAccessContentAction
{
  /* We want this action to be authorized managed by the SOPE's internal acl
     handling. */
  return [self responseWith204];
// #warning IMPROVEMENTS REQUIRED!
//   NSArray *acls;
// //  NSEnumerator *userAcls;
// //  NSString *currentAcl;

//   [self _setupContext];
  
// //  NSLog(@"canAccessContentAction %@, owner %@", subscriptionPointer, owner);

//   if ([login isEqualToString: owner] || [owner isEqualToString: @"nobody"]) {
//     return [self responseWith204];
//   }
//   else {
//     acls = [clientObject aclsForUser: login];
// //    userAcls = [acls objectEnumerator];
// //    currentAcl = [userAcls nextObject];
// //    while (currentAcl) {
// //      NSLog(@"ACL login %@, owner %@, folder %@: %@",
// //	    login, owner, baseFolder, currentAcl);
// //      currentAcl = [userAcls nextObject];
// //    }
//     if (([[clientObject folderType] isEqualToString: @"Contact"]
// 	 && [acls containsObject: SOGoRole_ObjectViewer]) ||
// 	([[clientObject folderType] isEqualToString: @"Appointment"]
// 	 && [acls containsObject: SOGoRole_AuthorizedSubscriber])) {
//       return [self responseWith204];
//     }
//   }
  
//   return [self responseWithStatus: 403];
}

- (WOResponse *) _realFolderActivation: (BOOL) makeActive
{
  NSMutableArray *folderSubscription;
  NSString *folderName;

  [self _setupContext];
  folderSubscription
    = [moduleSettings objectForKey: @"InactiveFolders"];
  if (!folderSubscription)
    {
      folderSubscription = [NSMutableArray array];
      [moduleSettings setObject: folderSubscription forKey: @"InactiveFolders"];
    }

  folderName = [clientObject nameInContainer];
  if (makeActive)
    [folderSubscription removeObject: folderName];
  else
    [folderSubscription addObjectUniquely: folderName];

  [us synchronize];

  return [self responseWith204];
}

- (WOResponse *) activateFolderAction
{
  return [self _realFolderActivation: YES];
}

- (WOResponse *) deactivateFolderAction
{
  return [self _realFolderActivation: NO];
}

- (WOResponse *) renameFolderAction
{
  WOResponse *response;
  NSString *folderName;

  folderName = [[context request] formValueForKey: @"name"];
  if ([folderName length] > 0)
    {
      clientObject = [self clientObject];
      [clientObject renameTo: folderName];
      response = [self responseWith204];
    }
  else
    {
      response = [self responseWithStatus: 500];
      [response appendContentString: @"Missing 'name' parameter."];
    }

  return response;
}

- (id) batchDeleteAction
{
  WOResponse *response;
  NSString *idsParam;
  NSArray *ids;

  idsParam = [[context request] formValueForKey: @"ids"];
  ids = [idsParam componentsSeparatedByString: @","];
  if ([ids count])
    {
      clientObject = [self clientObject];
      [clientObject deleteEntriesWithIds: ids];
      response = [self responseWith204];
    }
  else
    {
      response = [self responseWithStatus: 500];
      [response appendContentString: @"At least 1 id required."];
    }
  
  return response;
}

- (NSException*) _moveContacts: (NSArray*) contactsId 
		      toFolder: (NSString*) destinationFolderId
		   andKeepCopy: (BOOL) keepCopy
{
  NSEnumerator *uids;
  NSException *ex;
  NSString *uid;
  SOGoContentObject *currentChild;
  SOGoGCSFolder *sourceFolder, *destinationFolder;
  SOGoParentFolder *folders;
  SoSecurityManager *sm;
  unsigned int errorCount;

  sm = [SoSecurityManager sharedSecurityManager];
  ex = nil;
  errorCount = 0;

  // Search the specified destination folder
  sourceFolder = [self clientObject];
  folders = [sourceFolder container];
  destinationFolder = [folders lookupName: destinationFolderId
			       inContext: nil
			       acquire: NO];
  if (destinationFolder)
    {
      // Verify write access to the folder
      ex = [sm validatePermission: SoPerm_AddDocumentsImagesAndFiles
	       onObject: destinationFolder
	       inContext: context];
      if (!ex)
	{
	  uids = [contactsId objectEnumerator];
	  while ((uid = [uids nextObject]))
	    {
	      // Search the currentChild ID
	      currentChild = [sourceFolder lookupName: uid
					   inContext: [self context]
					   acquire: NO];
	      if ([currentChild isKindOfClass: [NSException class]])
		errorCount++;
	      else
		{
		  if (keepCopy)
		    ex = [currentChild copyToFolder: destinationFolder];
		  else
		    ex = [currentChild moveToFolder: destinationFolder];
		  if (ex)
		    errorCount++;
		}
	    }
	}
    }
  else
    ex = [NSException exceptionWithName: @"UnknownDestinationFolder"
		      reason: @"Unknown Destination Folder"
		      userInfo: nil];

  if (errorCount > 0)
    // At least one currentChild was not copied
    ex = [NSException exceptionWithHTTPStatus: 400
		      reason: @"Invalid Contact"];
  else if (ex != nil)
    // Destination address book doesn't exist or is not writable
    ex = [NSException exceptionWithHTTPStatus: 403
		      reason: [ex name]];

  return ex;
}

- (id <WOActionResults>) copyAction
{
  WORequest *request;
  id <WOActionResults> response;
  NSString *destinationFolderId;
  NSArray *contactsId;
  NSException *ex;
  
  request = [context request];

  if ((destinationFolderId = [request formValueForKey: @"folder"]) &&
      (contactsId = [request formValuesForKey: @"uid"]))
    ex = [self _moveContacts: contactsId
                    toFolder: destinationFolderId
		 andKeepCopy: YES];
  else
    ex = [NSException exceptionWithHTTPStatus: 400
                                       reason: (@"missing 'folder' and/or"
                                                @" 'uid' parameter")];
  
  if (ex)
    response = (id) ex;
  else
    response = [self responseWith204];

  return response;
}

- (id <WOActionResults>) moveAction
{
  WORequest *request;
  id <WOActionResults> response;
  NSString *destinationFolderId;
  NSArray *contactsId;
  NSException *ex;
  
  request = [context request];

  if ((destinationFolderId = [request formValueForKey: @"folder"])
      && (contactsId = [request formValuesForKey: @"uid"]))
    ex = [self _moveContacts: contactsId
                    toFolder: destinationFolderId
		 andKeepCopy: NO];
  else
    ex = [NSException exceptionWithHTTPStatus: 400
                                       reason: (@"missing 'folder' and/or"
                                                @"'uid' parameter")];
  
  if (ex)
    response = (id <WOActionResults>) ex;
  else
    response = [self responseWith204];
  
  return response;
}

- (id <WOActionResults>) subscribeUsersAction
{
  id <WOActionResults> response;
  NSString *uids;
  NSArray *userIDs;
  SOGoGCSFolder *folder;
  NSException *ex;
  int count, max;
  
  uids = [[context request] formValueForKey: @"uids"];
  if ([uids length])
    {
      userIDs = [uids componentsSeparatedByString: @","];
      folder = [self clientObject];
      max = [userIDs count];
      for (count = 0; count < max; count++)
        [folder subscribeUserOrGroup: [userIDs objectAtIndex: count]
			    reallyDo: YES
                            response: nil];
      ex = nil;
    }
  else
    ex = [NSException exceptionWithHTTPStatus: 400
                                       reason: @"missing 'uids' parameter"];
  
  if (ex)
    response = (id <WOActionResults>) ex;
  else
    response = [self responseWith204];
  
  return response;
}

@end