2 Copyright (C) 2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSNull.h>
24 #import <Foundation/NSTimeZone.h>
25 #import <Foundation/NSUserDefaults.h>
26 #import <Foundation/NSValue.h>
27 #import <NGObjWeb/WOApplication.h>
28 #import <NGObjWeb/WOContext.h>
29 #import <NGObjWeb/WORequest.h>
30 #import <NGObjWeb/SoObject.h>
31 #import <NGExtensions/NSNull+misc.h>
33 #import "AgenorUserDefaults.h"
34 #import "LDAPUserManager.h"
35 #import "SOGoDateFormatter.h"
36 #import "SOGoObject.h"
37 #import "SOGoPermissions.h"
38 #import "NSArray+Utilities.h"
42 static NSTimeZone *serverTimeZone = nil;
43 static NSString *fallbackIMAP4Server = nil;
44 static NSString *defaultLanguage = nil;
45 static NSString *superUsername = nil;
46 static NSURL *AgenorProfileURL = nil;
47 static BOOL acceptAnyUser = NO;
49 NSString *SOGoWeekStartHideWeekNumbers = @"HideWeekNumbers";
50 NSString *SOGoWeekStartJanuary1 = @"January1";
51 NSString *SOGoWeekStartFirst4DayWeek = @"First4DayWeek";
52 NSString *SOGoWeekStartFirstFullWeek = @"FirstFullWeek";
54 @interface NSObject (SOGoRoles)
56 - (NSArray *) rolesOfUser: (NSString *) uid;
60 @implementation SOGoUser
64 NSString *tzName, *nsUsername;
68 ud = [NSUserDefaults standardUserDefaults];
71 tzName = [ud stringForKey: @"SOGoServerTimeZone"];
74 serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
75 [serverTimeZone retain];
77 if (!AgenorProfileURL)
79 profileURL = [ud stringForKey: @"AgenorProfileURL"];
80 AgenorProfileURL = [[NSURL alloc] initWithString: profileURL];
82 if (!fallbackIMAP4Server)
83 ASSIGN (fallbackIMAP4Server,
84 [ud stringForKey: @"SOGoFallbackIMAP4Server"]);
87 ASSIGN (defaultLanguage, [ud stringForKey: @"SOGoDefaultLanguage"]);
89 ASSIGN (defaultLanguage, @"English");
93 nsUsername = [ud stringForKey: @"SOGoSuperUsername"];
94 if ([nsUsername length] > 0)
95 ASSIGN (superUsername, nsUsername);
98 acceptAnyUser = ([[ud stringForKey: @"SOGoAuthentificationMethod"]
99 isEqualToString: @"bypass"]);
102 + (SOGoUser *) userWithLogin: (NSString *) newLogin
103 roles: (NSArray *) newRoles
107 user = [[self alloc] initWithLogin: newLogin roles: newRoles];
115 if ((self = [super init]))
121 currentPassword = nil;
128 - (id) initWithLogin: (NSString *) newLogin
129 roles: (NSArray *) newRoles
135 || [newLogin isEqualToString: @"anonymous"]
136 || [newLogin isEqualToString: @"freebusy"])
140 um = [LDAPUserManager sharedUserManager];
141 realUID = [[um contactInfosForUserWithUIDorEmail: newLogin]
142 objectForKey: @"c_uid"];
144 self = [super initWithLogin: realUID roles: newRoles];
151 [currentPassword release];
152 [userDefaults release];
153 [userSettings release];
156 [dateFormatter release];
160 - (void) setCurrentPassword: (NSString *) newPassword
162 ASSIGN (currentPassword, newPassword);
165 - (NSString *) currentPassword
167 return currentPassword;
170 - (id) _fetchFieldForUser: (NSString *) field
172 NSDictionary *contactInfos;
175 um = [LDAPUserManager sharedUserManager];
176 contactInfos = [um contactInfosForUserWithUIDorEmail: login];
178 return [contactInfos objectForKey: field];
181 - (void) _fetchAllEmails
183 allEmails = [self _fetchFieldForUser: @"emails"];
189 cn = [self _fetchFieldForUser: @"cn"];
195 // - (NSString *) fullEmail
197 // return [[LDAPUserManager sharedUserManager] getFullEmailForUID: login];
200 // - (NSString *) primaryEmail
203 // [self _fetchAllEmails];
205 // return [allEmails objectAtIndex: 0];
208 - (NSArray *) allEmails
211 [self _fetchAllEmails];
216 - (NSString *) systemEmail
219 [self _fetchAllEmails];
221 return [allEmails lastObject];
224 - (BOOL) hasEmail: (NSString *) email
227 [self _fetchAllEmails];
229 return [allEmails containsCaseInsensitiveString: email];
240 // - (NSString *) primaryMailServer
242 // return [[self userManager] getServerForUID: [self login]];
245 // - (NSArray *) additionalIMAP4AccountStrings
247 // return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
250 // - (NSArray *) additionalEMailAddresses
252 // return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
255 // - (NSDictionary *) additionalIMAP4AccountsAndEMails
257 // return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
260 - (NSURL *) freeBusyURL
265 - (SOGoDateFormatter *) dateFormatterInContext: (WOContext *) context
272 dateFormatter = [SOGoDateFormatter new];
273 [dateFormatter setLocale: [context valueForKey: @"locale"]];
274 ud = [self userDefaults];
275 format = [ud stringForKey: @"ShortDateFormat"];
277 [dateFormatter setShortDateFormat: format];
278 format = [ud stringForKey: @"LongDateFormat"];
280 [dateFormatter setLongDateFormat: format];
281 format = [ud stringForKey: @"TimeFormat"];
283 [dateFormatter setTimeFormat: format];
286 return dateFormatter;
291 - (NSUserDefaults *) userDefaults
295 = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
297 fieldName: @"c_defaults"];
302 - (NSUserDefaults *) userSettings
306 = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
308 fieldName: @"c_settings"];
313 - (NSString *) language
320 language = [[self userDefaults] stringForKey: @"Language"];
321 if (![language length])
323 context = [[WOApplication application] context];
324 bLanguages = [[context request] browserLanguages];
325 if ([bLanguages count] > 0)
326 language = [bLanguages objectAtIndex: 0];
328 if (![language length])
329 language = defaultLanguage;
336 - (NSTimeZone *) timeZone
338 NSString *timeZoneName;
342 timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
343 if ([timeZoneName length] > 0)
344 userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
346 userTimeZone = serverTimeZone;
347 [userTimeZone retain];
353 - (NSTimeZone *) serverTimeZone
355 return serverTimeZone;
359 - (NSArray *) mailAccounts
361 #warning should be implemented with the user defaults interfaces
362 NSMutableDictionary *mailAccount, *identity;
363 NSMutableArray *identities;
364 NSString *name, *fullName;
371 mailAccount = [NSMutableDictionary dictionary];
372 name = [NSString stringWithFormat: @"%@@%@", login, fallbackIMAP4Server];
373 [mailAccount setObject: login forKey: @"userName"];
374 [mailAccount setObject: fallbackIMAP4Server forKey: @"serverName"];
375 [mailAccount setObject: name forKey: @"name"];
377 identities = [NSMutableArray array];
378 mails = [self allEmails];
380 for (i = 0; i < [mails count]; i++)
382 identity = [NSMutableDictionary dictionary];
383 fullName = [self cn];
384 if (![fullName length])
386 [identity setObject: fullName forKey: @"fullName"];
387 [identity setObject: [mails objectAtIndex: i] forKey: @"email"];
389 if (i == 0) [identity setObject: [NSNumber numberWithBool: YES] forKey: @"isDefault"];
390 [identities addObject: identity];
393 [mailAccount setObject: identities forKey: @"identities"];
395 mailAccounts = [NSMutableArray new];
396 [mailAccounts addObject: mailAccount];
403 @interface SOGoMailIdentity : NSObject
408 NSString *organization;
411 NSString *sentFolderName;
413 NSString *draftsFolderName;
414 NSString *templatesFolderName;
422 - (void) setName: (NSString *) _value;
425 - (void) setEmail: (NSString *) _value;
426 - (NSString *) email;
428 - (void) setReplyTo: (NSString *) _value;
429 - (NSString *) replyTo;
431 - (void) setOrganization: (NSString *) _value;
432 - (NSString *) organization;
434 - (void) setSignature: (NSString *) _value;
435 - (NSString *) signature;
436 - (BOOL) hasSignature;
438 - (void) setVCard: (NSString *) _value;
439 - (NSString *) vCard;
442 - (void) setSentFolderName: (NSString *) _value;
443 - (NSString *) sentFolderName;
445 - (void) setSentBCC: (NSString *) _value;
446 - (NSString *) sentBCC;
448 - (void) setDraftsFolderName: (NSString *) _value;
449 - (NSString *) draftsFolderName;
451 - (void) setTemplatesFolderName: (NSString *) _value;
452 - (NSString *) templatesFolderName;
456 - (NSArray *) allIdentities
461 identities = [mailAccounts objectsForKey: @"identities"];
463 return [identities flattenedArray];
466 - (NSDictionary *) primaryIdentity
468 NSDictionary *defaultAccount;
471 defaultAccount = [mailAccounts objectAtIndex: 0];
473 return [[defaultAccount objectForKey: @"identities"] objectAtIndex: 0];
476 - (NSString *) messageForwarding
478 NSString *messageForwarding;
481 = [[self userDefaults] stringForKey: @"MessageForwarding"];
482 if (![messageForwarding length])
483 messageForwarding = @"attached";
485 return messageForwarding;
490 // TODO: those methods should check whether the traversal stack in the context
491 // already contains proper folders to improve caching behaviour
493 - (id) homeFolderInContext: (id) _ctx
495 /* Note: watch out for cyclic references */
496 // TODO: maybe we should add an [activeUser reset] method to SOPE
499 folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
501 return [folder isNotNull] ? folder : nil;
503 folder = [[WOApplication application] lookupName: [self login]
506 if ([folder isKindOfClass:[NSException class]])
509 [(WOContext *)_ctx setObject: ((folder)
512 forKey: @"ActiveUserHomeFolder"];
516 // - (id) schedulingCalendarInContext: (id) _ctx
518 // /* Note: watch out for cyclic references */
521 // folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
522 // if (folder != nil)
523 // return [folder isNotNull] ? folder : nil;
525 // folder = [self homeFolderInContext:_ctx];
526 // if ([folder isKindOfClass:[NSException class]])
529 // folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
530 // if ([folder isKindOfClass:[NSException class]])
533 // [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
534 // forKey:@"ActiveUserCalendar"];
538 - (NSArray *) rolesForObject: (NSObject *) object
539 inContext: (WOContext *) context
541 NSMutableArray *rolesForObject;
544 rolesForObject = [NSMutableArray new];
545 [rolesForObject autorelease];
547 sogoRoles = [super rolesForObject: object inContext: context];
549 [rolesForObject addObjectsFromArray: sogoRoles];
551 if ((superUsername && [login isEqualToString: superUsername])
552 || [[object ownerInContext: context] isEqualToString: login])
553 [rolesForObject addObject: SoRole_Owner];
554 if ([object isKindOfClass: [SOGoObject class]])
556 sogoRoles = [(SOGoObject *) object aclsForUser: login];
558 [rolesForObject addObjectsFromArray: sogoRoles];
561 return rolesForObject;