/
SOGoGCSFolder.m
2332 lines (1988 loc) · 69.4 KB
/
SOGoGCSFolder.m
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SOGoGCSFolder.m - this file is part of SOGo
*
* Copyright (C) 2004-2005 SKYRIX Software AG
* Copyright (C) 2006-2014 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/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/SoObject.h>
#import <NGObjWeb/SoObject+SoDAV.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGObjWeb/WOApplication.h>
#import <NGObjWeb/WOResponse.h>
#import <NGExtensions/NSString+misc.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGExtensions/NSObject+Logs.h>
#import <DOM/DOMElement.h>
#import <DOM/DOMProtocols.h>
#import <EOControl/EOFetchSpecification.h>
#import <EOControl/EOQualifier.h>
#import <EOControl/EOSortOrdering.h>
#import <GDLAccess/EOAdaptorChannel.h>
#import <GDLAccess/EOAdaptorContext.h>
#import <GDLContentStore/GCSChannelManager.h>
#import <GDLContentStore/GCSFolderManager.h>
#import <GDLContentStore/GCSFolder.h>
#import <GDLContentStore/NSURL+GCS.h>
#import <SaxObjC/XMLNamespaces.h>
#import "NSDictionary+Utilities.h"
#import "NSArray+Utilities.h"
#import "NSArray+DAV.h"
#import "NSObject+DAV.h"
#import "NSObject+Utilities.h"
#import "NSString+Utilities.h"
#import "NSString+DAV.h"
#import "DOMNode+SOGo.h"
#import "SOGoCache.h"
#import "SOGoContentObject.h"
#import "SOGoDomainDefaults.h"
#import "SOGoGroup.h"
#import "SOGoParentFolder.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserDefaults.h"
#import "SOGoUserSettings.h"
#import "SOGoUserManager.h"
#import "SOGoWebDAVAclManager.h"
#import "WORequest+SOGo.h"
#import "WOResponse+SOGo.h"
#import "SOGoGCSFolder.h"
static NSString *defaultUserID = @"<default>";
static NSArray *childRecordFields = nil;
@implementation SOGoGCSFolder
+ (void) initialize
{
if (!childRecordFields)
{
childRecordFields = [NSArray arrayWithObjects: @"c_name", @"c_version",
@"c_creationdate", @"c_lastmodified",
@"c_component", @"c_content", nil];
[childRecordFields retain];
}
}
+ (SOGoWebDAVAclManager *) webdavAclManager
{
static SOGoWebDAVAclManager *aclManager = nil;
if (!aclManager)
{
aclManager = [SOGoWebDAVAclManager new];
/* [aclManager registerDAVPermission: davElement (@"read", @"DAV:")
abstract: YES
withEquivalent: SoPerm_WebDAVAccess
asChildOf: davElement (@"all", @"DAV:")];
[aclManager registerDAVPermission: davElement (@"read-current-user-privilege-set", @"DAV:")
abstract: YES
withEquivalent: SoPerm_WebDAVAccess
asChildOf: davElement (@"read", @"DAV:")]; */
[aclManager registerDAVPermission: davElement (@"write", @"DAV:")
abstract: NO
withEquivalent: SoPerm_AddDocumentsImagesAndFiles
asChildOf: davElement (@"all", @"DAV:")];
[aclManager registerDAVPermission: davElement (@"bind", @"DAV:")
abstract: NO
withEquivalent: SoPerm_AddDocumentsImagesAndFiles
asChildOf: davElement (@"write", @"DAV:")];
[aclManager registerDAVPermission: davElement (@"unbind", @"DAV:")
abstract: NO
withEquivalent: SoPerm_DeleteObjects
asChildOf: davElement (@"write", @"DAV:")];
[aclManager
registerDAVPermission: davElement (@"write-properties", @"DAV:")
abstract: YES
withEquivalent: SoPerm_ChangePermissions /* hackish */
asChildOf: davElement (@"write", @"DAV:")];
[aclManager
registerDAVPermission: davElement (@"write-content", @"DAV:")
abstract: YES
withEquivalent: nil
asChildOf: davElement (@"write", @"DAV:")];
[aclManager registerDAVPermission: davElement (@"admin", @"urn:inverse:params:xml:ns:inverse-dav")
abstract: YES
withEquivalent: nil
asChildOf: davElement (@"all", @"DAV:")];
[aclManager
registerDAVPermission: davElement (@"read-acl", @"DAV:")
abstract: YES
withEquivalent: SOGoPerm_ReadAcls
asChildOf: davElement (@"admin", @"urn:inverse:params:xml:ns:inverse-dav")];
[aclManager
registerDAVPermission: davElement (@"write-acl", @"DAV:")
abstract: YES
withEquivalent: SoPerm_ChangePermissions
asChildOf: davElement (@"admin", @"urn:inverse:params:xml:ns:inverse-dav")];
}
return aclManager;
}
+ (id) folderWithSubscriptionReference: (NSString *) reference
inContainer: (id) aContainer
{
id newFolder;
NSArray *elements, *pathElements;
NSString *path, *objectPath, *login, *ocsName, *folderName;
WOContext *localContext;
BOOL localIsSubscription;
elements = [reference componentsSeparatedByString: @":"];
login = [elements objectAtIndex: 0];
localContext = [[WOApplication application] context];
objectPath = [elements objectAtIndex: 1];
pathElements = [objectPath componentsSeparatedByString: @"/"];
if ([pathElements count] > 1)
ocsName = [pathElements objectAtIndex: 1];
else
ocsName = @"personal";
path = [NSString stringWithFormat: @"/Users/%@/%@/%@",
login, [pathElements objectAtIndex: 0], ocsName];
localIsSubscription = ![login isEqualToString:
[aContainer ownerInContext: localContext]];
if (localIsSubscription)
folderName = [NSString stringWithFormat: @"%@_%@",
[login asCSSIdentifier], ocsName];
else
folderName = ocsName;
newFolder = [self objectWithName: folderName inContainer: aContainer];
[newFolder setOCSPath: path];
[newFolder setOwner: login];
[newFolder setIsSubscription: localIsSubscription];
if (![newFolder displayName])
newFolder = nil;
return newFolder;
}
- (id) init
{
if ((self = [super init]))
{
ocsPath = nil;
ocsFolder = nil;
childRecords = [NSMutableDictionary new];
userCanAccessAllObjects = NO;
}
return self;
}
- (void) dealloc
{
[ocsFolder release];
[ocsPath release];
[childRecords release];
[super dealloc];
}
/* accessors */
- (void) setFolderPropertyValue: (id) theValue
inCategory: (NSString *) theKey
settings: (SOGoUserSettings *) theSettings
{
NSMutableDictionary *folderSettings, *values;
NSString *module;
module = [container nameInContainer];
folderSettings = [theSettings objectForKey: module];
if (!folderSettings)
{
folderSettings = [NSMutableDictionary dictionary];
[theSettings setObject: folderSettings forKey: module];
}
values = [folderSettings objectForKey: theKey];
if (theValue)
{
if (!values)
{
// Create the property dictionary
values = [NSMutableDictionary dictionary];
[folderSettings setObject: values forKey: theKey];
}
[values setObject: theValue forKey: [self folderReference]];
}
else if (values)
{
// Remove the property for the folder
[values removeObjectForKey: [self folderReference]];
if ([values count] == 0)
// Also remove the property dictionary when empty
[folderSettings removeObjectForKey: theKey];
}
[theSettings synchronize];
}
- (void) setFolderPropertyValue: (id) theValue
inCategory: (NSString *) theKey
{
SOGoUserSettings *settings;
settings = [[context activeUser] userSettings];
[self setFolderPropertyValue: theValue
inCategory: theKey
settings: settings];
}
- (id) folderPropertyValueInCategory: (NSString *) theKey
{
return [self folderPropertyValueInCategory: theKey
forUser: [context activeUser]];
}
- (id) folderPropertyValueInCategory: (NSString *) theKey
forUser: (SOGoUser *) theUser
{
SOGoUserSettings *settings;
NSDictionary *folderSettings;
id value;
settings = [theUser userSettings];
folderSettings = [settings objectForKey: [container nameInContainer]];
value = [[folderSettings objectForKey: theKey]
objectForKey: [self folderReference]];
return value;
}
- (NSString *) _displayNameFromRow: (NSDictionary *) row
{
NSString *name, *primaryDN;
name = nil;
primaryDN = [row objectForKey: @"c_foldername"];
if ([primaryDN length])
{
if ([primaryDN isEqualToString: [container defaultFolderName]])
name = [self labelForKey: primaryDN
inContext: context];
else
name = primaryDN;
}
return name;
}
- (BOOL) synchronize
{
NSNumber *synchronize;
synchronize = [self folderPropertyValueInCategory: @"FolderSynchronize"];
return [synchronize boolValue];
}
- (void) setSynchronize: (BOOL) new
{
NSNumber *synchronize;
if (new)
synchronize = [NSNumber numberWithBool: YES];
else
synchronize = nil;
[self setFolderPropertyValue: synchronize
inCategory: @"FolderSynchronize"];
}
/* This method fetches the display name defined by the owner, but is also the
fallback when a subscriber has not redefined the display name yet in their
environment. */
- (NSString *) _displayNameFromOwner
{
GCSChannelManager *cm;
EOAdaptorChannel *fc;
NSURL *folderLocation;
NSString *name, *sql;
NSArray *attrs;
NSDictionary *row;
name = nil;
cm = [GCSChannelManager defaultChannelManager];
folderLocation = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
fc = [cm acquireOpenChannelForURL: folderLocation];
if (fc)
{
// We use an exception handler here in case the database is down when
// performing the query. This could have unexpected results.
NS_DURING
{
sql = [NSString stringWithFormat: (@"SELECT c_foldername FROM %@"
@" WHERE c_path = '%@'"),
[folderLocation gcsTableName], ocsPath];
[fc evaluateExpressionX: sql];
attrs = [fc describeResults: NO];
row = [fc fetchAttributes: attrs withZone: NULL];
if (row)
name = [self _displayNameFromRow: row];
[fc cancelFetch];
[cm releaseChannel: fc];
}
NS_HANDLER;
NS_ENDHANDLER;
}
return name;
}
- (NSString *) _displayNameFromSubscriber
{
NSDictionary *ownerIdentity, *folderSubscriptionValues;
NSString *name, *displayNameFormat;
SOGoDomainDefaults *dd;
name = [self folderPropertyValueInCategory: @"FolderDisplayNames"];
if (!name)
{
name = [self _displayNameFromOwner];
// We MUST NOT use SOGoUser instances here (by calling -primaryIdentity)
// as it'll load user defaults and user settings which is _very costly_
// since it involves JSON parsing and database requests
ownerIdentity = [[SOGoUserManager sharedUserManager]
contactInfosForUserWithUIDorEmail: owner];
folderSubscriptionValues = [[NSDictionary alloc] initWithObjectsAndKeys: name, @"FolderName",
[ownerIdentity objectForKey: @"cn"], @"UserName",
[ownerIdentity objectForKey: @"c_email"], @"Email", nil];
dd = [[context activeUser] domainDefaults];
displayNameFormat = [dd subscriptionFolderFormat];
// Use a format only if it was defined by the user
if (displayNameFormat)
name = [folderSubscriptionValues keysWithFormat: displayNameFormat];
}
return name;
}
- (NSString *) displayName
{
if (!displayName)
{
if (activeUserIsOwner)
displayName = (NSMutableString *)[self _displayNameFromOwner];
else
{
displayName = (NSMutableString *)[self _displayNameFromSubscriber];
if (!displayName)
displayName = (NSMutableString *)[self _displayNameFromOwner];
}
[displayName retain];
}
return displayName;
}
- (void) setOCSPath: (NSString *) _path
{
if (![ocsPath isEqualToString:_path])
{
if (ocsPath)
[self warnWithFormat: @"GCS path is already set! '%@'", _path];
ASSIGN (ocsPath, _path);
}
}
- (NSString *) ocsPath
{
return ocsPath;
}
- (GCSFolderManager *) folderManager
{
static GCSFolderManager *folderManager = nil;
if (!folderManager)
folderManager = [GCSFolderManager defaultFolderManager];
return folderManager;
}
- (GCSFolder *) ocsFolderForPath: (NSString *) _path
{
NSDictionary *record;
SOGoCache *cache;
cache = [SOGoCache sharedCache];
record = [[cache valueForKey: _path] objectFromJSONString];
if (!record)
{
record = [[self folderManager] recordAtPath: _path];
if (!record)
return nil;
[cache setValue: [record jsonRepresentation] forKey: _path];
}
return [[self folderManager] folderForRecord: record];
}
- (BOOL) folderIsMandatory
{
return [nameInContainer isEqualToString: @"personal"];
}
- (NSString *) folderReference
{
return [NSString stringWithFormat: @"%@:%@/%@",
owner,
[container nameInContainer],
[self realNameInContainer]];
}
- (NSArray *) pathArrayToFolder
{
NSArray *basePathElements;
unsigned int max;
basePathElements = [[self ocsPath] componentsSeparatedByString: @"/"];
max = [basePathElements count];
return [basePathElements subarrayWithRange: NSMakeRange (2, max - 2)];
}
- (NSException *) setDavDisplayName: (NSString *) newName
{
NSException *error;
if ([newName length])
{
[self renameTo: newName];
error = nil;
}
else
error = [NSException exceptionWithHTTPStatus: 400
reason: @"Empty string"];
return error;
}
- (NSURL *) publicDavURL
{
NSMutableArray *newPath;
NSURL *davURL;
davURL = [self realDavURL];
newPath = [NSMutableArray arrayWithArray: [[davURL path] componentsSeparatedByString: @"/"]];
[newPath insertObject: @"public" atIndex: 3];
davURL = [[NSURL alloc] initWithScheme: [davURL scheme]
host: [davURL host]
path: [newPath componentsJoinedByString: @"/"]];
return davURL;
}
- (NSURL *) realDavURL
{
NSURL *realDavURL, *currentDavURL;
NSString *appName, *publicParticle, *path;
if (isSubscription)
{
appName = [[context request] applicationName];
if ([self isInPublicZone])
publicParticle = @"/public";
else
publicParticle = @"";
path = [NSString stringWithFormat: @"/%@/dav%@/%@/%@/%@/",
appName, publicParticle,
[self ownerInContext: nil],
[container nameInContainer],
[self realNameInContainer]];
currentDavURL = [self davURL];
realDavURL = [[NSURL alloc] initWithScheme: [currentDavURL scheme]
host: [currentDavURL host]
path: path];
[realDavURL autorelease];
}
else
realDavURL = [self davURL];
return realDavURL;
}
- (GCSFolder *) ocsFolder
{
GCSFolder *folder;
SOGoUser *user;
NSString *userLogin;
if (!ocsFolder)
{
ocsFolder = [self ocsFolderForPath: [self ocsPath]];
user = [context activeUser];
userLogin = [user login];
if (!ocsFolder
&& [userLogin isEqualToString: [self ownerInContext: context]]
&& [user canAuthenticate]
&& [self folderIsMandatory]
&& [self create])
ocsFolder = [self ocsFolderForPath: [self ocsPath]];
[ocsFolder retain];
}
if ([ocsFolder isNotNull])
folder = ocsFolder;
else
folder = nil;
return folder;
}
- (BOOL) create
{
NSException *result;
result = [[self folderManager] createFolderOfType: [self folderType]
withName: displayName
atPath: ocsPath];
if (!result
&& [[context request] handledByDefaultHandler])
[self sendFolderAdvisoryTemplate: @"Addition"];
return (result == nil);
}
- (NSException *) delete
{
NSException *error;
SOGoUserSettings *us;
NSMutableDictionary *moduleSettings;
// We just fetch our displayName since our table will use it!
[self displayName];
if ([nameInContainer isEqualToString: @"personal"])
error = [NSException exceptionWithHTTPStatus: 403
reason: @"folder 'personal' cannot be deleted"];
else
error = [[self folderManager] deleteFolderAtPath: ocsPath];
if (!error)
{
us = [[SOGoUser userWithLogin: owner] userSettings];
moduleSettings = [us objectForKey: [container nameInContainer]];
[self removeFolderSettings: moduleSettings
withReference: [self folderReference]];
[us synchronize];
[[SOGoCache sharedCache] removeValueForKey: ocsPath];
if ([[context request] handledByDefaultHandler])
[self sendFolderAdvisoryTemplate: @"Removal"];
}
return error;
}
- (void) _ownerRenameTo: (NSString *) newName
{
GCSChannelManager *cm;
EOAdaptorChannel *fc;
NSURL *folderLocation;
NSString *sql;
cm = [GCSChannelManager defaultChannelManager];
folderLocation
= [[GCSFolderManager defaultFolderManager] folderInfoLocation];
fc = [cm acquireOpenChannelForURL: folderLocation];
if (fc)
{
#warning GDLContentStore should provide methods for renaming folders
sql
= [NSString stringWithFormat: (@"UPDATE %@ SET c_foldername = '%@'"
@" WHERE c_path = '%@'"),
[folderLocation gcsTableName],
[newName stringByReplacingString: @"'" withString: @"''"],
ocsPath];
[fc evaluateExpressionX: sql];
[cm releaseChannel: fc];
// sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'",
// uidColumnName, [self uid]];
}
}
- (void) _subscriberRenameTo: (NSString *) newName
{
if ([newName length])
[self setFolderPropertyValue: newName
inCategory: @"FolderDisplayNames"];
}
- (void) renameTo: (NSString *) newName
{
#warning SOGoFolder should have the corresponding method
[displayName release];
displayName = nil;
if (activeUserIsOwner)
[self _ownerRenameTo: newName];
else
[self _subscriberRenameTo: newName];
}
/* Returns an empty string to indicate that the filter is empty and nil when
the query should not even be performed. */
- (NSString *) aclSQLListingFilter
{
NSString *filter, *login;
NSArray *roles;
SOGoUser *activeUser;
activeUser = [context activeUser];
login = [activeUser login];
if (activeUserIsOwner
|| [[self ownerInContext: nil] isEqualToString: login]
|| ([activeUser respondsToSelector: @selector (isSuperUser)]
&& [activeUser isSuperUser]))
filter = @"";
else
{
roles = [self aclsForUser: login];
if ([roles containsObject: SOGoRole_ObjectViewer]
|| [roles containsObject: SOGoRole_ObjectEditor])
filter = @"";
else
filter = nil;
}
return filter;
}
- (NSString *) componentSQLFilter
{
return nil;
}
- (NSArray *) toOneRelationshipKeys
{
NSArray *records, *names;
NSString *sqlFilter, *compFilter;
EOQualifier *aclQualifier, *componentQualifier, *qualifier;
sqlFilter = [self aclSQLListingFilter];
if (sqlFilter)
{
if ([sqlFilter length] > 0)
aclQualifier = [EOQualifier qualifierWithQualifierFormat: sqlFilter];
else
aclQualifier = nil;
compFilter = [self componentSQLFilter];
if ([compFilter length] > 0)
{
componentQualifier
= [EOQualifier qualifierWithQualifierFormat: compFilter];
if (aclQualifier)
{
qualifier = [[EOAndQualifier alloc] initWithQualifiers:
aclQualifier,
componentQualifier,
nil];
[qualifier autorelease];
}
else
qualifier = componentQualifier;
}
else
qualifier = aclQualifier;
records = [[self ocsFolder] fetchFields: childRecordFields
matchingQualifier: qualifier];
if (![records isNotNull])
{
[self errorWithFormat: @"(%s): fetch failed!", __PRETTY_FUNCTION__];
return nil;
}
if ([records isKindOfClass: [NSException class]])
return records;
names = [records objectsForKey: @"c_name" notFoundMarker: nil];
[childRecords release];
childRecords = [[NSMutableDictionary alloc] initWithObjects: records
forKeys: names];
}
else
names = [NSArray array];
return names;
}
- (NSDictionary *) _recordForObjectName: (NSString *) objectName
{
NSArray *records;
EOQualifier *qualifier;
NSDictionary *record;
qualifier
= [EOQualifier qualifierWithQualifierFormat:
[NSString stringWithFormat: @"c_name='%@'", objectName]];
records = [[self ocsFolder] fetchFields: childRecordFields
matchingQualifier: qualifier];
if (![records isKindOfClass: [NSException class]]
&& [records count])
record = [records objectAtIndex: 0];
else
record = nil;
return record;
}
- (void) removeChildRecordWithName: (NSString *) childName
{
[childRecords removeObjectForKey: childName];
}
- (Class) objectClassForComponentName: (NSString *) componentName
{
[self subclassResponsibility: _cmd];
return Nil;
}
- (Class) objectClassForContent: (NSString *) content
{
[self subclassResponsibility: _cmd];
return Nil;
}
- (id) createChildComponentWithRecord: (NSDictionary *) record
{
Class klazz;
klazz = [self objectClassForComponentName:
[record objectForKey: @"c_component"]];
return [klazz objectWithRecord: record inContainer: self];
}
- (id) createChildComponentWithName: (NSString *) newName
andContent: (NSString *) newContent
{
Class klazz;
NSDictionary *record;
unsigned int now;
NSNumber *nowNumber;
klazz = [self objectClassForContent: newContent];
now = [[NSCalendarDate calendarDate] timeIntervalSince1970];
nowNumber = [NSNumber numberWithUnsignedInt: now];
record = [NSDictionary dictionaryWithObjectsAndKeys: newName, @"c_name",
newContent, @"c_content",
nowNumber, @"c_creationdate",
nowNumber, @"c_lastmodified", nil];
return [klazz objectWithRecord: record inContainer: self];
}
- (id) lookupName: (NSString *) key
inContext: (WOContext *) localContext
acquire: (BOOL) acquire
{
id obj;
NSDictionary *record;
WORequest *request;
obj = [super lookupName: key inContext: localContext acquire: acquire];
if (!obj)
{
record = [childRecords objectForKey: key];
if (!record)
{
record = [self _recordForObjectName: key];
if (record)
[childRecords setObject: record forKey: key];
}
if (record)
obj = [self createChildComponentWithRecord: record];
else
{
request = [localContext request];
if ([[request method] isEqualToString: @"PUT"])
{
obj = [self createChildComponentWithName: key
andContent: [request contentAsString]];
[obj setIsNew: YES];
}
}
if (obj)
[[SOGoCache sharedCache]
registerObject: obj withName: key inContainer: self];
}
return obj;
}
- (void) deleteEntriesWithIds: (NSArray *) ids
{
unsigned int count, max;
NSEnumerator *names;
NSString *currentID, *currentName;
SOGoContentObject *deleteObject;
max = [ids count];
for (count = 0; count < max; count++)
{
currentID = [ids objectAtIndex: count];
names = [[currentID componentsSeparatedByString: @"/"] objectEnumerator];
deleteObject = (SOGoContentObject *)self;
while ((currentName = [names nextObject]))
{
deleteObject = [deleteObject lookupName: currentName
inContext: context
acquire: NO];
}
if (![deleteObject isKindOfClass: [NSException class]])
{
if ([deleteObject respondsToSelector: @selector (prepareDelete)])
[deleteObject prepareDelete];
[deleteObject delete];
}
}
}
- (NSString *) davCollectionTag
{
NSCalendarDate *lmDate;
lmDate = [[self ocsFolder] lastModificationDate];
return [NSString stringWithFormat: @"%d",
(lmDate ? (int)[lmDate timeIntervalSince1970]
: -1)];
}
- (BOOL) userIsSubscriber: (NSString *) subscribingUser
{
SOGoUser *sogoUser;
NSDictionary *moduleSettings;
NSArray *folderSubscription;
sogoUser = [SOGoUser userWithLogin: subscribingUser];
moduleSettings = [[sogoUser userSettings]
objectForKey: [container nameInContainer]];
folderSubscription = [moduleSettings objectForKey: @"SubscribedFolders"];
return [folderSubscription containsObject: [self folderReference]];
}
- (BOOL) subscribeUserOrGroup: (NSString *) theIdentifier
reallyDo: (BOOL) reallyDo
response: (WOResponse *) theResponse
{
NSMutableDictionary *moduleSettings, *folderShowAlarms;
NSMutableArray *folderSubscription;
NSString *subscriptionPointer;
NSMutableArray *allUsers;
SOGoUserSettings *us;
NSDictionary *dict;
SOGoUser *sogoUser;
BOOL rc;
int i;
dict = [[SOGoUserManager sharedUserManager] contactInfosForUserWithUIDorEmail: theIdentifier];
if ([[dict objectForKey: @"isGroup"] boolValue])
{
SOGoGroup *aGroup;
aGroup = [SOGoGroup groupWithIdentifier: theIdentifier
inDomain: [[context activeUser] domain]];
allUsers = [NSMutableArray arrayWithArray: [aGroup members]];
// We remove the active user from the group (if present) in order to
// not subscribe him to their own resource!
[allUsers removeObject: [context activeUser]];
}
else
{
sogoUser = [SOGoUser userWithLogin: theIdentifier roles: nil];
if (sogoUser)
allUsers = [NSArray arrayWithObject: sogoUser];
else
allUsers = [NSArray array];
}
rc = NO;
// This is consumed by SOGo Integrator during folder subscription since v24.0.6
if (theResponse)
[theResponse appendContentString: [self displayName]];
for (i = 0; i < [allUsers count]; i++)
{
sogoUser = [allUsers objectAtIndex: i];
us = [sogoUser userSettings];
moduleSettings = [us objectForKey: [container nameInContainer]];
if (!(moduleSettings
&& [moduleSettings isKindOfClass: [NSMutableDictionary class]]))
{
moduleSettings = [NSMutableDictionary dictionary];
[us setObject: moduleSettings forKey: [container nameInContainer]];
}
folderSubscription = [moduleSettings objectForKey: @"SubscribedFolders"];
subscriptionPointer = [self folderReference];
// We used to set "show alarms" for any type of folder, so we remove it
// here and let the subclass handle it (SOGoAppointmentFolder).
folderShowAlarms = [moduleSettings objectForKey: @"FolderShowAlarms"];
if (folderShowAlarms)
[folderShowAlarms removeObjectForKey: subscriptionPointer];
if (reallyDo)
{
if (!(folderSubscription
&& [folderSubscription isKindOfClass: [NSMutableArray class]]))
{
folderSubscription = [NSMutableArray array];
[moduleSettings setObject: folderSubscription
forKey: @"SubscribedFolders"];
}
[self setFolderPropertyValue: [self _displayNameFromSubscriber]
inCategory: @"FolderDisplayNames"
settings: us];
[folderSubscription addObjectUniquely: subscriptionPointer];
}
else
{
[self removeFolderSettings: moduleSettings
withReference: subscriptionPointer];
[folderSubscription removeObject: subscriptionPointer];